jenkins -列出每个并行分支的当前阶段名称

e7arh2l6  于 2023-01-12  发布在  Jenkins
关注(0)|答案(1)|浏览(151)

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

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

如何知道每个内部阶段运行的是哪个分支
例如,运行printMyStage()的预期结果为:

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

y4ekin9u1#

您只需在阶段中执行以下操作即可获得阶段名称。

echo "${env.STAGE_NAME}"
    • 更新**
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
    • 产出**
[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

相关问题