2
votes

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.

1
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?naspras

1 Answers

6
votes

You'll need something like:

parallel((1..numOfJobs).collect { index ->
    { -> build("job${index}", param: params["inputVal" + index]) }
})