jenkins - 在 Jenkins 管道中获取步骤 ID 以链接到 BlueOcean 或管道步骤 View (flowGraphTable)

标签 jenkins jenkins-pipeline jenkins-blueocean

给定一个运行一系列步骤的 Jenkins 管道,其中一些在 parallel 内块,有没有办法在管道内获得给定步骤或最近步骤的 Flow id?

什么是流 ID?如果您查看流水线作业的运行,您可以看到指向 flowGraphTable/ 的“流水线步骤”链接。 .那里有指向特定工作步骤的链接,例如 execution/node/113/ .这些似乎代表一个 FlowNode .

有没有办法从管道中获取这些 ID,用于生成链接等?

特别是我想获得一个链接到我的并行分支的子流,以便我可以链接到它们的 BlueOcean View 。 (内置的 Jenkins View 没有用,因为它没有显示子树)。

我可以看到 BlueOcean 链接对应于/execution/链接,它们具有相同的 id 值。如果我的管道分支是 myjob/9/execution/node/78/然后在 blueocean 上它会是 jobname/9/pipeline/78 .

但是,如果我想使用构建摘要插件或类似插件来生成链接并将它们添加到构建结果页面,我该如何获得该 ID?

最佳答案

我在一个类似的用例中苦苦挣扎,并设法找到了一个对我有用的解决方案。此 https://issues.jenkins-ci.org/browse/JENKINS-28119可能是关于这个问题的有趣读物。它最终为我指明了一个好的方向。

除了流水线和流水线阶段 View 插件,我还必须在我们的 Jenkins 服务器上安装 HTTP 请求插件 (https://wiki.jenkins.io/display/JENKINS/HTTP+Request+Plugin) 和流水线实用步骤插件(用于解析 JSON,https://wiki.jenkins.io/display/JENKINS/Pipeline+Utility+Steps+Plugin)。我不确定可能需要哪些其他插件。

这是我的工作示例,仅缺少正在评估的阶段:

#!groovy

pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                script {
                    def responseRun = httpRequest(
                        //consoleLogResponseBody: true,
                        contentType: 'APPLICATION_JSON',
                        httpMode: 'GET',
                        url: BUILD_URL + 'wfapi',
                        validResponseCodes: '200'
                    )
                    def runJson = readJSON text: responseRun.getContent()
                    def headNodeUrl = ''
                    runJson.stages.each {
                        if (it.name.toString() == 'Stage node label') {
                            // Found head node: it.id
                            headNodeUrl = BUILD_URL + 'execution/node/' + it.id.toString() + '/'
                        }
                    }
                    def responseNode = httpRequest(
                        contentType: 'APPLICATION_JSON',
                        httpMode: 'GET',
                        url: headNodeUrl + 'wfapi',
                        validResponseCodes: '200'
                    )
                    def nodeJson = readJSON text: responseNode.getContent()
                    def execNodeUrl = ''
                    nodeJson.stageFlowNodes.each {
                        if (it.name.toString() == 'Execution node label') {
                            // Found execution node: it.id
                            execNodeUrl = BUILD_URL + 'execution/node/' + it.id.toString() + '/log/'
                        }
                    }
                    echo execNodeUrl
                }
            }
        }
    }
}
BUILD_URL是一个全局环境变量,由 Jenkins 提供,我假设。在我的完整脚本中,我有一个 stage('Stage node label') { ... }包含声明 bat label: 'Execution node label', script: ...其日志 URL 将被构建和打印 echo .

结果是一个类似 http://myjenkinsserver.org:8080/job/some_folder/job/my_job_name/181/execution/node/50/log/ 的 URL

我认为使用each在我的示例中可能并不理想,因为我无法在第一场比赛后中止它。我也没有设法封装 httpRequestreadJSON进入类方法或其他东西,因为我无法弄清楚 readJSON 的返回类型.任何提示表示赞赏。

我希望这有帮助。

干杯

关于jenkins - 在 Jenkins 管道中获取步骤 ID 以链接到 BlueOcean 或管道步骤 View (flowGraphTable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54287643/

相关文章:

git - 如何有条件地跳过 Jenkins for CI 的 Maven 测试

java - 如何用 Jenkins 触发端点( Controller )

Jenkins 声明性管道使用 When 条件作为分支名称

jenkins-pipeline - 共享库 "vars"文件夹结构 - 我可以添加子文件夹吗?

jenkins - 无法使用变量在 jenkins 管道中的字符串匹配后替换行

Jenkins Pipeline sh 显示名称/标签

git - LockedFileException : failed to lock file config. 写锁

Android 工具看不到我的单元测试

jenkins - Jenkins BlueOcean 中的 "Branch indexing"事件是什么

jenkins - BlueOcean 使用 SSH key 连接