4

I have a pipeline job which run some sequence of action (Eg; Build >> Run >> Report). I ahave put this sequence in a for loop as I can get a parameter how many times I should repeat the same sequence. Please find the sample code I have written.

for (int i = 0; i < <param_val>; ++i){
    node{
        stage('Build') {
            build 'Build'
        }
        stage('Run') {
           build 'Run'
        }
        stage('Reporting') {
           build 'Reporting'
        }
    }
}

Now my code is waiting for one complete sequence to happen and then continue to run the next sequence. That is time consuming. I have more slave agents and can run the sequence in parallel. How can I run each iteration of a for loop parallel?

I have thought of a solution : have a pipeline having the actual sequence

node{
        stage('Build') {
            build 'Build'
        }
        stage('Run') {
           build 'Run'
        }
        stage('Reporting') {
           build 'Reporting'
        }
    }

have another pipeline with the for loop which will trigger the 1st pipeline with wait: false:

for (int i = 0; i < <param_val>; ++i){
    build(job: 'pipeline-1', wait: false)
}

Does this work ? Or do we have a better option to do the same with single pipeline ?

2 Answers 2

10

Put the code inside the loop in a closure:

def oneNode = { c ->
    node {
        build job: 'Build', parameters: [string(name: 'Component', value: c)]
        build 'Run'
        build 'Reporting'
    }
}

Then make a map of all the closures you want to run simultaneously:

def jobs = [:]
def components = params.Componets.split(",")
for (int i = 0; i < components.size(); ++i) {
    def component = components[i].trim()
    jobs[component] = {
        oneNode(component)
    }
}

And finally use the parallel step with the resulting map:

stage('Build, run, report') {
    <the code from the above steps>
    parallel jobs
}
6
  • There is another issue now !!! Each iteration says calling parallelly , bui it seems to be same builds of the job. Not running more builds. Please see this console output. Scheduling project: setup Starting building: setup #79 Starting building: setup #79 Commented May 13, 2019 at 3:44
  • And I have a variable to be getting from the for loop.. which is also giving the last value. components = params.Componets.split(",") def jobs= [:] for (int i = 0; i < components.size(); ++i){ component = components[i].trim() jobs[i] = { oneNode() } } I will be using the component value while caling building 'Build' job. build job: 'Build', parameters: [string(name: 'Component', value: component)] How can I handle this? Commented May 13, 2019 at 3:48
  • @SreevalsaE you need to define parameters for oneNode. I updated the answer.
    – towel
    Commented May 13, 2019 at 8:59
  • 1
    I could get the code working.. For he first comment we shouldn't be using 'i' itself for extracting the value. If we use that way, it will give you only last value for 'i'. So I was not able to trigger jobs with different parameters. I had added def index = i and used index variable to extract and it is working fine. reffered to the section "Jobs In Parallel " in jenkins.io/doc/pipeline/examples Commented May 13, 2019 at 9:11
  • 1
    Oops, I forgot to update the key of jobs. Edited my answer again.
    – towel
    Commented May 13, 2019 at 9:16
-1

This worked for me.

def jobs = [:]
def components = params.Componets.split(",")
stage('Build, run, report') {
    for (int i = 0; i < components.size(); ++i) {
        def index = i
        def component = components[index].trim()
        jobs[i] = {
            node {
                build job: 'Build', parameters: [string(name: 'Component', value: component)]
                build 'Run'
                build 'Reporting'
            }
        }
    }
    parallel jobs
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.