获取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 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:\n" + results.join('\n')
}
}
最后一个'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屋!