Reputation: 986
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 ?
Upvotes: 4
Views: 14298
Reputation: 2214
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
}
Upvotes: 10
Reputation: 986
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
}
Upvotes: -1