获取 jenkinsfile 并行分支运行状态 [英] Getting jenkinsfile parallel branch run status
问题描述
我有多个并行分支工作,每个分支包含许多阶段.
I got multiple parallel branches job, each branch contains numerous stages.
def build_jobs = [:]
build_jobs['1'] = {
stage ('A'){}
stage ('B'){}
}
build_jobs['2'] = {
stage ('A'){}
stage ('B'){}
}
build_jobs['3'] = {
stage ('A'){}
stage ('B'){}
}
parallel build_jobs
通过 API,我只能找到单独的阶段状态和整个构建状态.(使用/api/json 和/wfapi)我需要一种方法来在每个构建结束时到达分支名称及其状态.
Thru the API I can find only separate stage status and whole build status.(using /api/json and /wfapi) I need a way to reach at the end of each build the branch name and it status.
[Pipeline] { (Branch: 1) - status ?
[Pipeline] { (Branch: 2) - status ?
[Pipeline] { (Branch: 3) - status ?
如果每个阶段都不支持我的需求,则获取状态.
Getting status if each stage doesn't support my needs.
推荐答案
IMO 最简单的方法是使用 PipelineNodeGraphVisitor
来自 BlueOcean 插件,用于查询 FlowNodeWrapper.NodeType 类型的所有节点.并行代码>.这些是树枝.
IMO the easiest way is to use PipelineNodeGraphVisitor
from BlueOcean plugin to query all nodes of type FlowNodeWrapper.NodeType.PARALLEL
. These are the branches.
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import io.jenkins.blueocean.rest.impl.pipeline.PipelineNodeGraphVisitor
import io.jenkins.blueocean.rest.impl.pipeline.FlowNodeWrapper
@NonCPS
List getBranchResults( RunWrapper build ) {
def visitor = new PipelineNodeGraphVisitor( build.rawBuild )
def branches = visitor.pipelineNodes.findAll{ it.type == FlowNodeWrapper.NodeType.PARALLEL }
return branches.collect{ branch -> [
id: branch.id,
displayName: branch.displayName,
result: "${branch.status.result}",
]}
}
node {
def build_jobs = [:]
build_jobs['1'] = {
stage ('A'){ echo 'Success' }
stage ('B'){ echo 'Success' }
}
build_jobs['2'] = {
stage ('A'){ echo 'Success' }
stage ('B'){ error 'Error' }
}
build_jobs['3'] = {
stage ('A'){ echo 'Success' }
stage ('B'){ warnError( message: 'Unstable' ){ error 'Error' } }
}
try {
parallel build_jobs
}
finally {
def results = getBranchResults( currentBuild )
echo "Branch results:
" + results.join('
')
}
}
最后一个'echo'的输出(打开控制台日志查看):
Output of last 'echo' (open console log to see it):
Branch results:
[id:8, displayName:1, result:SUCCESS]
[id:9, displayName:2, result:FAILURE]
[id:10, displayName:3, result:UNSTABLE]
获取舞台结果的类似答案还列出了 BlueOcean API 的替代方案.
A similar answer to get stage results also lists an alternative to BlueOcean API.
这篇关于获取 jenkinsfile 并行分支运行状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!