确定Jenkins声明式管道中的失败阶段 [英] Determine Failed Stage in Jenkins Declarative Pipeline

查看:91
本文介绍了确定Jenkins声明式管道中的失败阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何报告声明式的阶段管道失败了吗?在fail块中,我想获取failureStage.name并报告(最终报告为松弛).

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).

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

推荐答案

您可以在每个阶段使用post指令,通过特定的操作和通知对失败采取行动.

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

这篇关于确定Jenkins声明式管道中的失败阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆