Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know which stage of jenkins pipeline has failed

Tags:

In my Jenkins pipelines I generally use post declarative function to send me an email incase the pipeline has failed.

A simple syntax of the post function is as under:

post {     failure {         mail to: '[email protected]',              subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",              body: "Something is wrong with ${env.BUILD_URL}"     } } 

In the above email, I also want to mention which stage (lets say the pipeline has 5 to 6 stages) of the pipeline has failed. How can I do that? Any help is much appreciated.

An extension to the above requirement will be to provide the user with the actual error log (of the stage that has failed) also as a part of the failure notification email.

Idea is, when a user receives a failure notification from jenkins, he should know which stage of the pipeline has failed along with the error log.

Thanks in advance.

like image 321
Yash Avatar asked May 18 '18 12:05

Yash


People also ask

How do you get failed stage name in Jenkins pipeline?

STAGE_NAME in a post stage the result will be Declarative: Post Actions . Instead, you will need to store the stage name in a variable in all stages. So once a stage fails - Jenkins will not continue with the next stages and therefore you will have the "failed" stage name.

How do I know if Jenkins build failed?

In Jenkins, in the pipeline where failure occurred, in the pane, select the latest build, and click Console Output. On the Console Output page, check the logs to find the reason for the failure. If required, update the parameters in the deployment input configuration file.

What are the three fundamental stages of a Jenkins pipeline?

That is, the build, test, and deploy processes all come together in a stage. Generally, a stage block is used to visualize the Jenkins pipeline process. A step is nothing but a single task that executes a specific process at a defined time.


1 Answers

There is a variable called env.STAGE_NAME which you can use. However, in your case you will probably need to store the stage name in a different variable, because when you get the env.STAGE_NAME in a post stage the result will be Declarative: Post Actions. Instead, you will need to store the stage name in a variable in all stages. So once a stage fails - Jenkins will not continue with the next stages and therefore you will have the "failed" stage name.

Here's an example:

def FAILED_STAGE  pipeline {     agent { label "master" }     stages {         stage("Stage 1") {             steps {                 script {                     FAILED_STAGE=env.STAGE_NAME                     echo "stage 1"                 }             }         }         stage("Stage 2") {             steps {                 script {                     FAILED_STAGE=env.STAGE_NAME                     echo "stage 2"                     error "failed for some reason."                 }             }         }         stage("Stage 3") {             steps {                 script {                     FAILED_STAGE=env.STAGE_NAME                     echo "stage 3"                 }             }         }     }     post {         failure {             echo "Failed stage name: ${FAILED_STAGE}"         }     } } 

There might be a better way to do it, but I haven't found it so far.

Regarding the logs - As of JENKINS-40526 you could possibly use the API and get the log file from there, but I am not sure you can get the parameters you need from within the pipeline. The other solution would be to use emailext and email the entire build log file:

emailext attachLog: true, body: '', compressLog: true, subject: 'Build failed.', to: '[email protected]'

like image 65
tftd Avatar answered Sep 28 '22 16:09

tftd