Search code examples
jenkinsjenkins-pipelinepipeline

Jenkins: Aborting a build with a catchError()


In my Jenkins scripted pipeline, I am executing 1 stage with some tasks and then sending Slack messages. I want to send these Slack messages if the previous stage passes or fails and to do this I am using a catchError() and it works exactly as I need to. My problem arises when I want to abort the build.

If I abort the build, I want to exit the build entirely and not send the Slack messages, but my catchError() is catching this abort and then continuing the pipeline and sending the messages.

stage('first stage with catchError') {
    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
        // Do some stuff here and continue the build if anything fails
    }
}

stage('second stage that send slack messages) {
    // Send some Slack messages based on the data produced in the first stage and based on if the tasks were a success or they failed
}

I thought I could use the catchInterruptions option in the catchError() to abort the entire build (here is a link for the docs for this function), but that clearly hasn't resulted in the behaviour I want. What shall I do instead?


Solution

  • I was just being stupid and misunderstanding the documentation. I just needed to set the catchInterruptions option to true (which is the default value).

        catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: true) {
            // Do some stuff here and continue the build if anything fails
        }
    

    OR (because true is the default value)

        catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
            // Do some stuff here and continue the build if anything fails
        }