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 ?
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
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With