Search code examples
loopsgroovyjenkinsparallel-processingjenkins-build-flow

Using loop to run parallel builds using build-flow plugin in Jenkins


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.


Solution

  • You'll need something like:

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