Send mail via Jenkins CI email-ext plugin not until a job fails n times

The Jenkins email-ext plugin is very helpful when you want to be informed about your builds with the help of an email. There are many triggers available for which state you want to get a notification. But when you want to receive an email not until e.g. the 5th build is failed you need to add your own groovy script to the trigger ‚Script – After Build‘.

The following script will send a mail first when a previous amount of builds are worse than the given threshold ‚hudson.model.Result.SUCCESS‘. In the example below an email is triggered only when a maximum number of 5 builds one after another has been aborted or failed.

A possible use case: Jenkins as a monitoring system that runs WebDriver tests every 5 min.

def max = 5;
def item = jenkins.model.Jenkins.instance.getItem(this.binding.build.project.name);

def threshold = hudson.model.Result.SUCCESS;
def threshCount = 0;

if(this.binding.build.result.isWorseThan(threshold)) {
    def builds = item.getBuilds();
    def count = 0;
    for (build in builds) {
        if (count <= max && threshCount < max && build.result.isWorseThan(threshold)) {
            threshCount++;
        } else {
            println(threshCount + " of max. " + max + " builds [aborted|failed].");
            break;
        }
        count++;
    }
}
// Boolean decides if a mail has to be triggered
threshCount >= max;
Script - After Build trigger of the Jenkins CI email-ext plugin

Script – After Build trigger of the Jenkins CI email-ext plugin

Advertisements
Dieser Beitrag wurde unter Jenkins abgelegt und mit , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s