Hudson/Jenkins notification only on consecutive failures Hudson/Jenkins notification only on consecutive failures jenkins jenkins

Hudson/Jenkins notification only on consecutive failures


Daniel, I believe your best bet is to script it yourself. I had a similar problem, and came up with a three-line solution on my own without any experience with Java/Groovy.

First off, you need a way to determine that a build has failed. See my problem for the solution.

Second, you need to store the number of failed builds somewhere. The file in the project workspace is the obvious location. Use this snippet as a base:

def f = new File(manager.build.getWorkspace().getRemote() + '/GroovyFailedBuildsCount.txt')f.createNewFile()f.write(text)

And third, you need to send an email. Off the top of my head you could mark the first failed builds as unstable, and when the limit is reached, mark the build as failed, and have the email-ext plugin to send email notifications only on failed builds.

Groovy getting started guide has been a great help for me.


An alternative implementation:

  1. Use the Disable Job plugin to disable the monitored job after a certain number of failures.
  2. Create a monitoring job that checks if the monitored job is disabled. If the job is disabled, then fail the monitoring build and send an email.
  3. (Optionally) Disable the monitoring job to keep it from sending you emails.

This has the happy side effect of also notifying you if someone manually disables your job.

To get a list of disabled jobs whose title contains "title filter":

curl -sS http://jenkins.example.com/api/json \  | jq '.jobs[] | select(.name | contains("title filter")) | select(.color=="disabled") | .name'


Jenkins notification only on consecutive unstables

With Jenkins v. 2.65 and Groovy Postbuild Plugin v. 2.3.1

  1. set your Jenkins notification plugin(mail/slack etc.) to send messages only if build failed.

  2. Add this Groovy code as Job Postbuild Action

    // debugging:// manager.build.@result = hudson.model.Result.SUCCESS;final int threshold = 2;String jobName = manager.build.project.name;def f = new File('/tmp/' + jobName + '_GroovyUnstableBuildsCount.txt');// debugging:// manager.createSummary("info.gif").appendText('path: ' + f.toString(), false, false, false, "red")if(!f.exists()){ // file don't exists    f.createNewFile();}// debugging:// String fileContent = f.text;// manager.createSummary("info.gif").appendText('fileContent: ' + fileContent, false, false, false, "red")if (manager.build.result.isBetterOrEqualTo(hudson.model.Result.SUCCESS)) {    f.write("0");} else if (manager.build.result == hudson.model.Result.UNSTABLE) {        int oldValue = 0;        if(f.text != null && !f.text.empty && f.text != ''){          oldValue = f.text.toInteger();        }    int newValue = oldValue + 1;    if(newValue > threshold){ // trigger send message        // set build to fail        manager.build.setResult(hudson.model.Result.FAILURE);        f.write("0"); // reset counter    } else { // increase count        f.write(newValue.toString());    }}