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.