If you don't want to use a for loop and want the generated pipeline to be executed in parallel, here is an answer.
def jobs = ["JobA", "JobB", "JobC"] def parallelStagesMap = jobs.collectEntries { ["${it}" : generateStage(it)] } def generateStage(job) { return { stage("stage: ${job}") { echo "This is ${job}." } } } pipeline { agent none stages { stage('non-parallel stage') { steps { echo 'This stage will be executed first.' } } stage('parallel stage') { steps { script { parallel parallelStagesMap } } } } }
Note that all generated stages will be executed into 1 node. If you want to execute the generated stages in different nodes:
def agents = ['master', 'agent1', 'agent2'] // enter valid agent name in array. def generateStage(nodeLabel) { return { stage("Runs on ${nodeLabel}") { node(nodeLabel) { echo "Running on ${nodeLabel}" } } } } def parallelStagesMap = agents.collectEntries { ["${it}" : generateStage(it)] } pipeline { agent none stages { stage('non-parallel stage') { steps { echo 'This stage will be executed first.' } } stage('parallel stage') { steps { script { parallel parallelStagesMap } } } } }
You can of course add more than 1 parameters and can use collectEntries for 2 parameters.
Please remember that the return in generateStage is a must.