Search code examples
jenkinsjenkins-pipeline

Declarative pipeline: don't send post/failure notifications if build was aborted


I'm trying to set up notifications on declarative pipeline failures as described here:
https://jenkins.io/doc/pipeline/tour/post/

post {
    failure {
        emailext (
            subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
            body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
                <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        )
       }
    }
}

Is there a way to not send emails if a build was aborted?

In "old" scripted pipelines I caught the FlowInterruptedException to achieve this.

catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
    echo "the job was cancelled or aborted"
    currentBuild.result = 'ABORTED'    
}

Solution

  • This has been fixed by https://issues.jenkins-ci.org/browse/JENKINS-43339 There was a bug which set the currentBuild.result to FAILURE instead of ABORTED.

    Make sure to restart jenkins after the update.