简体   繁体   中英

jenkins - list curent stages name for each parallel branch

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

How I can know for each inner stage which branch I am running

For example, expected result for running printMyStage() will be:

branch 1 stage A
branch 1 stage B
branch 2
branch 3 stage A
branch 3 stage B

You can simply do the following within the stage to get the stage name.

echo "${env.STAGE_NAME}"

Update

def build_jobs = [:]
build_jobs['1'] = {
    node{
        stage('A'){
            sh 'echo 1'
            printMyStage()
        }
        stage('B'){
           printMyStage()
           "error"
        }
    }
}
build_jobs['2'] = {
    node{
        printMyStage()
        sh 'echo 2'
    }
}
build_jobs['3'] = {
    node{
        stage('A'){
            printMyStage()
            sh 'echo 3'
        }
        stage('B'){
            printMyStage()
        }
    }
}

def printMyStage(){
    echo "The current stage: ${env.STAGE_NAME}"
}

parallel build_jobs

Output

[Pipeline] Start of Pipeline
[Pipeline] parallel
[Pipeline] { (Branch: 1xx)
[Pipeline] { (Branch: 2)
[Pipeline] { (Branch: 3)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/2
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/2@2
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/2@3
[Pipeline] {
[Pipeline] {
[Pipeline] {
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] echo
The current stage: null
[Pipeline] sh
[Pipeline] sh
[Pipeline] echo
The current stage: A
[Pipeline] sh
+ echo 2
2
[Pipeline] }
+ echo 1
1
[Pipeline] // node
[Pipeline] }
+ echo 3
3
[Pipeline] echo
The current stage: A
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
[Pipeline] echo
The current stage: B
[Pipeline] }
[Pipeline] echo
The current stage: B
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // parallel
[Pipeline] End of Pipeline
Finished: SUCCESS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM