qwerty
qwerty

Reputation: 3869

Calling multiple downstream jobs from an upstream Jenkins pipeline job

I have two pipeline based jobs

Parent_Job (has string parameters 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 (has string parameters project & version)

pipeline {
    agent any

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

Parent job output

[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

I expected the downstream job to be called twice, (for project1 and project2) but its invoked only once (for project2)

Is there something obviously wrong with this script?

Upvotes: 1

Views: 1116

Answers (1)

biruk1230
biruk1230

Reputation: 3156

It seems that the problem with wait: true option enabled for build job step. If you change it to wait: false it will execute 2 times. I tried it on this test pipeline:

@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)
                }    
            }
        }
    }
}

Upvotes: 2

Related Questions