If I have a pipeline where individual stages are allowed to fail, without failing the whole job, how can I add error handling to, for instance, send an email to an admin, when that stage fails? I've tried using post
failure
, but it doesn't work.
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
post {
failure {
echo 'Sending email to admin...'
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
I got this question in a comment and thought it was worth asking and answering as a proper question.