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(;

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

if( {
    def builds = item.getBuilds();
    def count = 0;
    for (build in builds) {
        if (count <= max && threshCount < max && build.result.isWorseThan(threshold)) {
        } else {
            println(threshCount + " of max. " + max + " builds [aborted|failed].");
// 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

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:

Du kommentierst mit Deinem Abmelden /  Ändern )

Google Foto

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


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


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

Verbinde mit %s