问题
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
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.
回答1:
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')
}
}
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]
A similar answer to get stage results also lists an alternative to BlueOcean API.
来源:https://stackoverflow.com/questions/64155325/getting-jenkinsfile-parallel-branch-run-status