Marking upstream jobs as failed in jenkins without finger printing

Marking upstream jobs as failed in jenkins without finger printing

14 February 2014

This script not longer works and fails with: cannot change build result while in COMPLETED

Please DISREGARD this article.

Pipeline

I use the Build Pipeline Plugin and the Groovy Postbuild Plugin to allow me to set all builds upstream to failed in a downstream job failed.

I have the following pipeline:

(Root)->(Top)->(Down)

When the (Down) job fails I want to mark upstream jobs (Top) and (Root) as failed.

I did this using groovy in the post build and best of all; NO fingerprinting required!

Anyway, the script is below. Get copying and modifying!

/**
* Will mark all upstream builds as failed if the current job is
* UNSTABLE or FAILED.
*
* Fingerprinting not required.
* Author:Colin Williamson
*/

import hudson.model.*

void log(msg) {
  manager.listener.logger.println(msg)
}

def failRecursivelyUsingCauses(cause) {
     if (cause.class.toString().contains("UpstreamCause")) {
        def projectName = cause.getUpstreamProject()
        def number = cause.getUpstreamRun().number
        upstreamJob = hudson.model.Hudson.instance.getItem(projectName)
        if(upstreamJob) {
             upbuild = upstreamJob.getBuildByNumber(number)
             if(upbuild) {
                 log("Setting to failed for " + projectName + " - " + number)
                 upbuild.setResult(hudson.model.Result.FAILURE)
                 upbuild.save()
                 // fail other builds
                 for (upCause in cause.upstreamCauses) {
                     failRecursivelyUsingCauses(upCause)
                 }
             }
        } else {
            log("No Upstream job found for " + projectName);
        }
    }
}

if(manager.build.result.isWorseOrEqualTo(hudson.model.Result.UNSTABLE)) {
    log("Must fail upstream builds");
    for (cause in manager.build.causes) {
        failRecursivelyUsingCauses(cause)
    }
}
else {
    log("Success - not changing");
}
CI Jenkins-CI