Defining a failed stage in the Jenkins declarative pipeline

How to report the stage at which the declarative pipeline failed? In fault tolerance, I want to get failStage.name and report it (ultimately for easing).

pipeline { agent { label 'master'} stages { stage('Ok') { steps { echo 'do thing' } } stage('NotOK') { steps { sh 'make fail' } } } post { always { echo 'ok' } failure { echo 'Failed during Which Stage?' } } } 
+10
source share
3 answers

Instead of adding a post section at each step, I found some solution that should not work in the declarative pipeline from my point of view, but it works. All you need to do is override stage :

 def stage(String name, Closure cl) { echo "Stage: ${name}" try { cl() } catch (Exception e) { // I needed to save failed stage and message for parent pipeline job // so I saved them in environment variables, otherwise it can be saved // in global variables if (!env.FAILED_STAGE) { env.FAILED_STAGE = name env.FAILED_MESSAGE = e.getMessage() } } } pipeline { options { timestamps() } agent { label 'master' } stages { stage('First stage') { steps { //Any steps are working script { sh "echo first" } } } stage('Second stage') { steps { echo "second" } } stage('Fail stage') { steps { error "failed" } } stage('Final stage') { steps { build "Other job" } } } post { failure { echo "Failed stage: ${env.FAILED_STAGE}" echo "Error message: ${env.FAILED_MESSAGE}" } } } 

The strangest thing for me is that after the failure of the stage, the other stages are skipped, as it should be. Here is the conclusion:

 14:05:14 Stage: First stage [Pipeline] script [Pipeline] { [Pipeline] sh 14:05:14 + echo first 14:05:14 first [Pipeline] } [Pipeline] // script [Pipeline] echo 14:05:14 Stage: Second stage [Pipeline] echo 14:05:14 second [Pipeline] echo 14:05:14 Stage: Fail stage [Pipeline] error [Pipeline] error [Pipeline] echo 14:05:14 Stage: Final stage Stage "Final stage" skipped due to earlier failure(s) [Pipeline] echo 14:05:14 Stage: Declarative: Post Actions [Pipeline] echo 14:05:14 Failed stage: Fail stage [Pipeline] echo 14:05:14 Error message: failed [Pipeline] } [Pipeline] // timestamps [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: failed Finished: FAILURE 
0
source

You can use the post directive at every step to act on failure with specific actions and notifications.

This is not entirely ideal, as if you want you to have to repeat it at all stages, but I don’t think you can access your stage name dynamically, so it is really verbos and hardcoded. Perhaps you could reorganize this to use the library.

 pipeline { agent { label 'master'} stages { stage('Ok') { steps { echo 'do thing' } post { failure { echo 'FAILED (in stage OK - should not happen :))' } } } stage('NotOK') { steps { sh 'make fail' } post { failure { echo 'FAILED (in stage NotOK)' } } } } post { always { echo 'COMPLETED (global)' } failure { echo 'FAILED (global)' } } } 
+3
source

You can use try catch and notify slack in the catch part.

0
source

Source: https://habr.com/ru/post/1269689/


All Articles