Search code examples
jenkinsgroovyjenkins-pipeline

Why does my try/catch block not work when triggering a Jenkins pipeline job?


I have the following code, where I trigger a pipeline job in a multi-branch pipeline setup. All the variables are defined.

    // *** This is NOT the issue! ***
    // Force a scan to the mulit-branch pipeline.
    build job: buildJobForFeatureBranch,
          wait: false
    sleep(10)

    // *** This is the issue! ***
    // We intentionally do this twice. If it's a newly scanned
    // feature branch, the first call to it will fail because
    // of a Jenkins multi-branch pipeline bug where not all the
    // parameters are setup up yet.
    try {
      build job: cm.ftBuildJob,
            parameters: [
              string(name: "VERSION_PASSEDIN", value: srcBranch),
              string(name: "UPLOAD_ARTIFACTS", value: "true"),
              string(name: "DEBUG_LEVEL", value: "0")
            ],
            wait: true
    }
    catch(Exception e) {
      build job: cm.ftBuildJob,
            parameters: [
              string(name: "VERSION_PASSEDIN", value: srcBranch),
              string(name: "UPLOAD_ARTIFACTS", value: "true"),
              string(name: "DEBUG_LEVEL", value: "0")
            ],
            wait: true
    }

However, my pipeline just fails after the first call to the build job plugin. What's the correct way to do this? TIA.


Solution

  • You have a boolean parameter called propagate and as stipulated in Build Step Plugin, the default state is disabled(false).

    try {
     build job: cm.ftBuildJob,
           parameters: [
             string(name: "VERSION_PASSEDIN", value: srcBranch),
             string(name: "UPLOAD_ARTIFACTS", value: "true"),
             string(name: "DEBUG_LEVEL", value: "0")
           ],
           wait: true,
           propagate : true //Adding this line should do the job
    }
    

    Keep me updated on your result