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

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

问题描述

如何报告声明性管道所处的阶段 失败了?在失败块中,我想获取 failedStage.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?'
        }
    }
}

推荐答案

PipelineVisitor 是一个很好的方法.但是,如果您只想查看错误,那么利用 FlowGraphTable 可能会更好.

PipelineVisitor is a fine approach. However, if you want to see just the errors, then leveraging FlowGraphTable might be even better.

下面提供了每个失败步骤的映射列表,并遍历下游作业.我发现它非常有用.

The following provides a list of maps for each failed steps, and traverses the downstream jobs as well. I find it to be pretty useful.

您需要使用共享库来避免安全沙箱警告/批准

You'll want to use a shared library to avoid the security sandbox warnings / approvals

List<Map> getStepResults() {
    def result = []
    WorkflowRun build = currentBuild()
    FlowGraphTable t = new FlowGraphTable(build.execution)
    t.build()
    for (def row in t.rows) {
        if (row.node.error) {
            def nodeInfo = [
                    'name': "${row.node.displayName}",
                    'url': "${env.JENKINS_URL}${row.node.url}",
                    'error': "${row.node.error.error}",
                    'downstream': [:]

            ]
            if (row.node.getAction(LogStorageAction)) {
                nodeInfo.url += 'log/'
            }

            for (def entry in getDownStreamJobAndBuildNumber(row.node)) {
                nodeInfo.downstream["${entry.key}-${entry.value}"] = getStepResults(entry.key, entry.value)
            }
            result << nodeInfo
        }
    }
    log(result)
    return result
}

Map getDownStreamJobAndBuildNumber(def node) {
    Map downStreamJobsAndBuilds = [:]
    for (def action in node.getActions(NodeDownstreamBuildAction)) {
        def result = (action.link =~ /.*/(?!/)(.*)/runs/(.*)//).findAll()
        if (result) {
            downStreamJobsAndBuilds[result[0][1]] = result[0][2]
        }
    }
    return downStreamJobsAndBuilds
}

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

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