Jenkins Pipeline - Trigger jobs sequentially inside a loop

Viewed 617

I have a Jenkins pipeline where I trigger a single job with different parameters. The number of parameters may also change which changes the number of times the job needs to be triggered. This is why I have the build job in a for loop. Here's what the code looks like:

pipeline{

    stages{
        stage('Setup'){
            steps{
                script {
                    for (int i=0; i<list_one.size(); i++ ) {
                    def index_i = i
                        for (int j=0; j<list_two.size(); j++) {
                            def index_j = j
                            stage ("${list_one[i]} ${list_two[j]}") {
                                sh "echo 'index_i: ${index_i}'"
                                sh "echo 'index_j: ${index_j}'"
                                build job: 'Downstream Job', parameters: [
                                    string(name: 'some_param', value: "${list_one[index_i]}")]
                            }
                        }
                    }
                }
            }
        }
    }
}

When I run this pipeline, it only runs once for the first iteration of both the loops. However when I remove the build job line, the pipeline runs for all the values in the list. I'm perplexed by why this would be and would like some assistance in the matter.

1 Answers
Related