user3920295
user3920295

Reputation: 909

How to fail master Jenkins pipeline job if downstream jobs fails

I have a Jenkins pipeline master job that triggers 2 jobs buildjob1 and buildjob2.

My master pipeline job is simple like this:

   stage ("Test") {


                build (job: buildJob1,
                             parameters: [
                                    string(name: 'A', value: "AA),
                                    string(name: 'B', value: "BB),

                             ],
                             propagate: false)


              build (job: buildJob2,
                       parameters: [
                           string(name: 'A', value: "AA"),
                      ],
                     propagate: false)


        }


    cleanWs()
}

I want both the downstream jobs to run and mark the master job unstable/failed if any of the jobs fail.
How do i do that through pipeline?

Upvotes: 0

Views: 1804

Answers (2)

Tobi
Tobi

Reputation: 434

In case the jobs dont rely on each other, use parallel with the FailFast parameter - and you have to remove the propagate option (or set it to true)

stage ("Test") {
    parallel buildJob1: {
        build (job: buildJob1,
            parameters: [
                string(name: 'A', value: "AA),
                string(name: 'B', value: "BB),
            ])
    }, buildJob2: {
        build (job: buildJob2,
            parameters: [
                string(name: 'A', value: "AA"),
            ])
    }, failFast: false
    }

cleanWs()
}

Edit: As you commented, that the Jobs can only run sequential, we have to manually inspect the job result and set the pipeline-job result by hand. This way, we can call as many sequential Jobs, as we like:

stage("buildjob1") {
    result = build (job: buildJob1,
        parameters: [
            string(name: 'A', value: "AA),
            string(name: 'B', value: "BB),
        ], propagate=false)
    // Inspect result
    if(result == 'FAILURE') {
        echo "buildjob1 failed"
        currentBuild.result = 'UNSTABLE' // of FAILURE
    }
}
stage("buildjob2") {
    result = build (job: buildJob2,
        parameters: [
            string(name: 'A', value: "AA"),
        ], propagate=false)
    // Inspect result
    if(result == 'FAILURE') {
        echo "buildjob2 failed"
        currentBuild.result = 'UNSTABLE' // of FAILURE
    }
}
stage("cleanup") {
    // Call your cleanup
    cleanWs()
}

Upvotes: 1

Hieu Huynh
Hieu Huynh

Reputation: 1083

You can wrap this stage inside a try catch, if have any error ==> make the current build status to FAILURE

try {
    stage("Test") {
      build (job: buildJob1,
             parameters: [
               string(name: 'A', value: "AA"),
               string(name: 'B', value: "BB"),
             ],
             propagate: false)
      build (job: buildJob2,
             parameters: [
               string(name: 'A', value: "AA"),
               string(name: 'B', value: "BB"),
             ],
             propagate: false)
    }
} catch (error) {
    currentBuild.result='FAILURE'
}

Upvotes: 0

Related Questions