2

I am using the build flow plugin to run tasks in parallel in Jenkins. Initially it was this that works:

parallel (
    { build("jobX", param: params["inputVal1"])
    },
    {build("jobX",  param: params["inputVal2"])
    }
)

However, my need now requires me to write this in some kind of loop since the number of jobs is dynamic. I want to do something like this (conceptually):

parallel
(
    for(int i=1; i<=numOfJobs; i++)
    {
        build("jobX", param: params["inputVal" + i])
    }
)

There is an answer provided in Jenkins Buildflow plugin: how to make variable numbers of jobs in parallel?, but it does not suit my need exactly.

Community
  • 1
  • 1
techjourneyman
  • 1,701
  • 3
  • 33
  • 53
  • http://stackoverflow.com/questions/31387614/parallel-jobs-using-build-flow-plugin-with-loop-in-jenkins ? – tim_yates Dec 01 '15 at 17:10
  • Thanks @tim_yates. I am having trouble writing the Groovy syntax here since I want to loop deterministically based on an integer value but not on a List. Something like this following the answer in the indicated URL above: `parallel numJobs.each { index -> { -> build("jobX", param: params["inputVal" + index]) } }`. But this does not work either. Could you help with the syntax here? – techjourneyman Dec 01 '15 at 18:17

1 Answers1

6

You'll need something like:

parallel((1..numOfJobs).collect { index ->
    { -> build("job${index}", param: params["inputVal" + index]) }
})
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks @tim_yates. I did it with a slight variation, but this works. – techjourneyman Dec 01 '15 at 21:56
  • This is actually missing parentheses around the whole expressions… it should be `parallel ((1..numOfJobs).collect { index -> { -> build("job${index}", param: params["inputVal" + index]) } })` – Clintm Jan 24 '18 at 21:08