I got code that list all branches and stages for my pipeline
def build_jobs = [:]
build_jobs['1'] = {
node('builder'){
stage('A'){
sh 'echo 1'
printMyStage()
}
stage('B'){
printMyStage()
"error"
}
}
}
build_jobs['2'] = {
node('builder'){
printMyStage()
sh 'echo 2'
}
}
build_jobs['3'] = {
node('builder'){
stage('A'){
printMyStage()
sh 'echo 3'
}
stage('B'){
printMyStage()
}
}
}
parallel build_jobs
at the beginning of the run I am getting the following prints:
[Pipeline] parallel
[Pipeline] { (Branch: 1)
[Pipeline] { (Branch: 2)
[Pipeline] { (Branch: 3)
How can I reach the variable that has the branch name so the printMyStage() function will print the branch it was running from?
For current code the output will be:
Branch: 1
Branch: 1
Branch: 2
Branch: 3
Branch: 3
I also tried to use PipelineNodeGraphVisitor( currentBuild.rawBuild ) but without success
You can get the head
FlowNodefor the current thread via undocumentedCpsThread.current().head. UsingFlowNode.getEnclosingBlocks()you can get the parent blocks and with some conditions you can determine the branch nodes.Complete scripted pipeline example with nested
parallelbranches:Output (the order is random because of parallelism):
The function
printMyStage()lists the names of all parent branches. If you only need immediate parent, usebranches[0].displayName.