Marking upstream Jenkins/Hudson as failed if downstream job fails

后端 未结 4 1351
-上瘾入骨i
-上瘾入骨i 2021-02-18 22:46

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

4条回答
  •  清歌不尽
    2021-02-18 23:03

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

提交回复
热议问题