简体   繁体   中英

Marking upstream Jenkins/Hudson as failed if downstream job fails

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.

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.

在此输入图像描述

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");
}

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 (ie: 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM