列出每个并行分支的阶段



我得到的代码列出了我的管道的所有分支和阶段

def List getBranchResults() {
def visitor = new PipelineNodeGraphVisitor(currentBuild.rawBuild)
echo "${visitor}"
def branches = visitor.pipelineNodes.findAll{
it.type == FlowNodeWrapper.NodeType.PARALLEL 
}
def stages = visitor.pipelineNodes.findAll{
it.type == FlowNodeWrapper.NodeType.STAGE
}
echo "${stages}"
def results = branches.collect{
branch -> [ 
id: branch.id, 
displayName: branch.displayName, 
result: "${branch.status.result}",]
}
echo "Branch results:n" + results.join('n')
}
def build_jobs = [:]
def build_results
build_jobs['1'] = {
node('builder'){
stage('A'){
sh 'echo 1'
}
stage('B'){
"error"
}
}
}
build_jobs['2'] = {
node('builder'){
sh 'echo 2'
}
}
build_jobs['3'] = {
node('builder'){
stage('A'){
sh 'echo 3'
}
stage('B'){

}
}
}
parallel build_jobs
getBranchResults( )

我怎样才能把这两者联系起来?我想为它所暂存的每个分支打印。此外,我想打印每个分支的失败阶段(如果存在(

例如:

Branch results:
[id:15, displayName:1, result:SUCCESS]
Stage results for 1:
[id=55,displayName=A,type=STAGE]
[id=25,displayName=B,type=STAGE] - FAILURE

我们可以通过查询FlowNode.allEnclosingIds来关联分支和子阶段对于每个节点。如果此列表包含分支ID,则我们有一个子阶段。

我已经在下面的函数getStageResultsForBranch()中实现了这一点。它似乎与示例代码配合使用。如果阶段有嵌套的子阶段,它也会返回这些子阶段,这可能不是您想要的。

示例代码:

import io.jenkins.blueocean.rest.impl.pipeline.PipelineNodeGraphVisitor
import io.jenkins.blueocean.rest.impl.pipeline.FlowNodeWrapper
@NonCPS
def List getBranchResults() {
def visitor = new PipelineNodeGraphVisitor(currentBuild.rawBuild)
def branches = visitor.pipelineNodes.findAll{
it.type == FlowNodeWrapper.NodeType.PARALLEL 
}
def results = branches.collect{ branch -> [ 
id: branch.id, 
displayName: branch.displayName, 
result: "${branch.status.result}",
]}

return results
}
@NonCPS
def List getStageResultsForBranch( String branchId ) {
def visitor = new PipelineNodeGraphVisitor(currentBuild.rawBuild)
def childStages = visitor.pipelineNodes.findAll{ stage ->
stage.type == FlowNodeWrapper.NodeType.STAGE &&
stage.node.allEnclosingIds.contains( branchId )
}

def results = childStages.collect{ stage -> [ 
id: stage.id, 
displayName: stage.displayName, 
result: "${stage.status.result}",
]}

return results
}
node {
def build_jobs = [:]
def build_results

build_jobs['1'] = {
stage('1.A'){
sh 'echo 1'
}
stage('1.B'){
error 'an error'
}
}
build_jobs['2'] = {
sh 'echo 2'
}
build_jobs['3'] = {
stage('3.A'){
sh 'echo 3'
}
stage('3.B'){

}
}

try {
parallel build_jobs
}
finally {
stage('Final') {
for( branchRes in getBranchResults() ) {
def stageResults = getStageResultsForBranch( branchRes.id )
echo "BRANCH RESULT: $branchResn" +
"  STAGE RESULTS:n  ${stageResults.join('n  ')}"
}
}
}
}

(我已经删除了用于测试的构建作业节点(

输出:

BRANCH RESULT: [id:8, displayName:1, result:FAILURE]
STAGE RESULTS:
[id:12, displayName:1.A, result:SUCCESS]
[id:29, displayName:1.B, result:FAILURE]
BRANCH RESULT: [id:9, displayName:2, result:SUCCESS]
STAGE RESULTS:
BRANCH RESULT: [id:10, displayName:3, result:SUCCESS]
STAGE RESULTS:
[id:15, displayName:3.A, result:SUCCESS]
[id:21, displayName:3.B, result:NOT_BUILT]

相关内容

  • 没有找到相关文章

最新更新