詹金斯 - 列出每个并行分支的阶段

jenkins - list stages for each parallel branch

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

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('dvp_hal_builder'){
        stage('A'){
            sh 'echo 1'
        }
        stage('B'){
           "error"
        }
    }
}
build_jobs['2'] = {
    node('dvp_hal_builder'){
        sh 'echo 2'
    }
}
build_jobs['3'] = {
    node('dvp_hal_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() 中实现了这个。它似乎适用于示例代码。如果阶段有嵌套的子阶段,它也会 return 这些,这可能不是你想要的。

示例代码:

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: $branchRes\n" +
                     "  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]