Marking upstream Jenkins/Hudson as failed if downstream job fails
Asked Answered
I

4

19

I am using Parameterized Trigger Plugin to trigger a downstream build.

How do I specify that my upstream job should fail if the downstream fails? The upstream job is actually is dummy job with parameters being passed to the downstream.

Iguanodon answered 19/6, 2011 at 17:2 Comment(0)
K
19

Make sure you are using the correct step to execute your downstream jobs; I discovered that since I was executing mine as a "post build step", I didn't have the "Block until the triggered projects finish their builds" option. Changing that to "build task" as opposed to a "post build task", allowed me to find the options you are looking for within the Parameterized Trigger Plugin.

enter image description here

Kono answered 13/2, 2013 at 15:50 Comment(2)
only thing I'd add is that a blocked job still occupies a build executor so for long pipelines you need to bare that in mind. Thanks a lot for this answer though - been trying to do the same myself and didn't even think about using a Build Step.Ranger
this uses "postbuild-task" plugin then ?Paredes
M
5

this code will mark the upstream build unstable/failed based on downstream job status.

/*************************************************
Description: This script needs to put in Groovy 
Postbuild plugin of Jenkins as a Post Build task.
*************************************************/

import hudson.model.*

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

def failRecursivelyUsingCauses(cause) {
     if (cause.class.toString().contains("UpstreamCause")) {
        def projectName = cause.upstreamProject
        def number = cause.upstreamBuild
        upstreamJob = hudson.model.Hudson.instance.getItem(projectName)
        if(upstreamJob) {
             upbuild = upstreamJob.getBuildByNumber(number)
             if(upbuild) {
                 log("Setting to '" + manager.build.result + "' for Project: " + projectName + " | Build # " + number)
                 //upbuild.setResult(hudson.model.Result.UNSTABLE)
                 upbuild.setResult(manager.build.result);
                 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("****************************************");
    log("Must mark upstream builds fail/unstable");
    def thr = Thread.currentThread()
    def build = thr.executable
    def c = build.getAction(CauseAction.class).getCauses()

    log("Current Build Status: " + manager.build.result);
    for (cause in c) {
        failRecursivelyUsingCauses(cause)
    }
    log("****************************************");
}
else {
    log("Current build status is: Success - Not changing Upstream build status");
}
Malti answered 5/2, 2015 at 3:39 Comment(2)
Is it possible to make it mark the upstream jobs as SUCCESS in case of a rebuild of the failing downstream job?Hemia
Is it possible to change the status even after the parent upstream job is COMPLETE?Plaque
E
1

Have a look at the following response: Fail hudson build with groovy script. You can get access to the upstream job and fail its build BUT... be careful with the fact that Hudson/Jenkins post-build actions right now do not allow to specify any ordering: if your groovy script is specified besides other post-build actions, and those actions affect the result of the build (i.e.: parsing of test results), then you won't be able to update the status of the upstream job if Jenkins decides to run them after your groovy script.

Erminois answered 30/6, 2011 at 20:52 Comment(0)
K
0

Under Build step configure Trigger/Call builds on other projects, choose the downstream job. Select "Block until triggered project finish their build". Save the default settings under it. This settings will make upstream job failed is downstream is failed.

Koby answered 12/5, 2019 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.