Determine Failed Stage in Jenkins Declarative Pipeline

前端 未结 4 1127
执笔经年
执笔经年 2020-11-29 11:26

How do I report the stage in which a declarative pipeline failed? In the fail block, I want to get failedStage.name and report it (eventually to slack).

pip         


        
4条回答
  •  Happy的楠姐
    2020-11-29 11:51

    You can use a post directive in each stage, to act on failure with specific actions and notifications.

    It's not exactly ideal as if you want that in all stages you'd have to repeat it though, and I don't think you can access your stage name dynamically, so it's really verbos and hard-coded. You could probably refactor that to use a library though.

    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)'
            }
        }
    }
    

提交回复
热议问题