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

huangapple go评论57阅读模式
英文:

jenkins - list curent stages name for each parallel branch

问题

我有一个代码,列出了我的流水线的所有分支和阶段。

如何知道每个内部阶段正在运行哪个分支?

例如,运行printMyStage()的预期结果将是:

分支1 阶段A
分支1 阶段B
分支2
分支3 阶段A
分支3 阶段B
英文:

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

答案1

得分: 1

你可以在阶段内部执行以下操作以获取阶段名称。

```groovy
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 "当前阶段:${env.STAGE_NAME}"
}

parallel build_jobs

输出

[Pipeline] Pipeline 开始
[Pipeline] parallel
[Pipeline] { (分支: 1xx)
[Pipeline] { (分支: 2)
[Pipeline] { (分支: 3)
[Pipeline] node
运行于 Jenkins 中的 /var/jenkins_home/workspace/2
[Pipeline] node
运行于 Jenkins 中的 /var/jenkins_home/workspace/2@2
[Pipeline] node
运行于 Jenkins 中的 /var/jenkins_home/workspace/2@3
[Pipeline] {
[Pipeline] {
[Pipeline] {
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] echo
当前阶段:null
[Pipeline] sh
[Pipeline] sh
[Pipeline] echo
当前阶段:A
[Pipeline] sh
+ echo 2
2
[Pipeline] }
+ echo 1
1
[Pipeline] // node
[Pipeline] }
+ echo 3
3
[Pipeline] echo
当前阶段:A
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
[Pipeline] echo
当前阶段:B
[Pipeline] }
[Pipeline] echo
当前阶段:B
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // parallel
[Pipeline] Pipeline 结束
已完成:成功
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 15:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定