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.
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.
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.
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.
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]'
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