I started using Jenkins declarative pipelines. Now I want to have the same email notification behavior as defined in the Usage of the Mailer plugin:
- Every failed build triggers a new e-mail.
- A successful build after a failed (or unstable) build triggers a new e-mail, indicating that a crisis is over.
- An unstable build after a successful build triggers a new e-mail, indicating that there's a regression.
- Unless configured, every unstable build triggers a new e-mail, indicating that regression is still there.
I read about Notifications in Pipelines, but it does not notify based on above rules. Plus it does not contain part of the console output in the message body in case of a build failure.
Does anybody know how to do this in declarative pipeline?
Yes you can only if you want to have external function inside step block.
Different Types of Jenkins CI/CD Pipelines. Scripted Pipeline. Declarative Pipeline.
With the following code you can use the mailer plugin in the post section. This provides the expected behaviour:
pipeline {
agent any
stages {
stage('test') {
steps {
script {
// change to 'UNSTABLE' OR 'FAILED' to test the behaviour
currentBuild.result = 'SUCCESS'
}
}
}
}
post {
always {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "[email protected]",
sendToIndividuals: true])
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With