从上游Jenkins管道作业调用多个下游作业

时间:2019-01-14 15:42:50

标签: jenkins jenkins-pipeline

我有两个基于管道的作业

Parent_Job(具有字符串参数project1和project2)

@NonCPS
def invokeDeploy(map) {
    for (entry in map) {
        echo "Starting ${entry.key}"
        build job: 'Child_Job', parameters: [
                            string(name: 'project', value: entry.key),
                            string(name: 'version', value: entry.value)
                        ], quietPeriod: 2, wait: true
        echo "Completed ${entry.key}"
    }
}
pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                script {
                    invokeDeploy(params)
                }    
            }
        }
    }
}

Child_Job(具有字符串参数项目和版本)

pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                script {
                    echo "${params.project} --> ${params.version}"
                }    
            }
        }
    }
}

父级作业输出

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Starting project2
[Pipeline] build (Building Child_Job)
Scheduling project: Child_Job
Starting building: Child_Job #18
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

我希望下游作业被调用两次(对于project1和project2),但仅被调用一次(对于project2)

此脚本明显存在问题吗?

1 个答案:

答案 0 :(得分:1)

似乎为wait: true步骤启用了build job选项的问题。如果将其更改为wait: false,它将执行2次。我在此测试管道上进行了尝试:

@NonCPS
def invokeDeploy(map) {
    for (entry in map) {
        echo "Starting ${entry.key}"
        build job: 'pipeline', quietPeriod: 2, wait: false
        echo "Completed ${entry.key}"
    }
}
pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                script {
                    def sampleMap = [first_job:'First', second_job:'Second']
                    invokeDeploy(sampleMap)
                }    
            }
        }
    }
}