Reputation: 16830
I am using following step in my pipeline jenkins job:
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '[email protected]', sendToIndividuals: true])
But no email was sent when the build failed (i.e. error). Any pointers why?
P.S. Emails can be sent from this server, I have tested that.
Upvotes: 6
Views: 16240
Reputation: 4276
Use Declarative Pipelines using new syntax, for example:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "[email protected]";
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
You can find more information in the Official Jenkins Site:
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/
Note that this new syntax make your pipelines more readable, logic and maintainable.
Upvotes: 18
Reputation: 1985
You need to manually set the build result to failure, and also make sure it runs in a workspace. For example:
try {
throw new Exception('fail!')
} catch (all) {
currentBuild.result = "FAILURE"
} finally {
node('master') {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '[email protected]', sendToIndividuals: true])
}
}
The plugin is checking currentBuild.result
for the status, and this isn't normally changed until after the script completes.
Upvotes: 0