I have a Jenkins Pipeline where I am doing deployment, running automated tests and then posting the results to the Test Management System.
If Deploy stage fails, I don't want to go ahead with Run Tests and Post Results stages.
If Run Tests fail, I still want to go ahead and post the pass failed test results to the Test Management System.
On each stage, I want to trigger an email that the respective stage failed.
pipeline {
agent { label 'my-agent' }
stages {
stage('Deploy') {
steps {
// carry out deployment
}
post {
failure {
// send email that deployment failed
}
}
}
stage('Run Tests') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// carry out run
}
}
post {
failure {
// send email that run tests failed
}
}
}
stage('Post Results') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// post the results to the test management system
}
}
post {
failure {
// send email that posting results encountered error
}
}
}
}
}
The problem:
The email triggers for Deploy and Run Tests are working fine. However, when Run Tests has failures; even though the results are successfully posted to the Test Management System, the control is entering into failure part of the post for Post Results stage.
I tried making the buildResult
as SUCCESS and stageResult
as FAILURE. However, the control is not going into the failure part of even the same stage.
What changes do I need to make to avoid sending email for the Post Test failure even if it passes but the earlier Run Tests has failed?
CodePudding user response:
This is not ideal solution. But I have solved the above issue by shifting the post results error email trigger from the post section to a separate stage.
The post result execution stage has touch import_success
statement at the end. It gets executed only when the posting of results is successful.
The post results error email trigger stage has a when clause to check existence of the import_success file to send the email (trigger email when import_success file doesn't exist).
Here is the final pipeline,
pipeline {
agent { label 'my-agent' }
stages {
stage('Deploy') {
steps {
// carry out deployment
}
post {
failure {
// send email that deployment failed
}
}
}
stage('Run Tests') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// carry out run
}
}
post {
failure {
// send email that run tests failed
}
}
}
stage('Post Results') {
steps {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
// post the results to the test management system
sh 'touch import_success' // this is executed only when post results is successful
}
}
}
stage ('Post Results Error Email Trigger') {
when {
not {
expression {
fileExists 'import_success'
}
}
}
steps {
// send email that posting results encountered error
}
}
}
}