我有多个并行分支工作,每个分支包含多个阶段。
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) 我需要一种在每个版本末尾找到分支名称及其状态的方法。
[Pipeline] { (Branch: 1) - status ?
[Pipeline] { (Branch: 2) - status ?
[Pipeline] { (Branch: 3) - status ?
如果每个阶段都不符合我的需要,请获取状态。
答案 0 :(得分:2)
IMO最简单的方法是使用BlueOcean插件中的PipelineNodeGraphVisitor
查询类型为FlowNodeWrapper.NodeType.PARALLEL
的所有节点。这些是分支。
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”的输出(打开控制台日志以查看它):
Branch results:
[id:8, displayName:1, result:SUCCESS]
[id:9, displayName:2, result:FAILURE]
[id:10, displayName:3, result:UNSTABLE]
similar answer to get stage results还列出了BlueOcean API的替代方案。