I have a Jenkins DSL pipeline where I create dynamical some steps
, and I was wondering how can I run all the steps in parallel
.
Since I use script
to split the string into array an iterate, parallel
seems to complain, since it has to be between stage
tags
Here a code example
stages {
stage('BuildAll') {
script {
parallel {
"${names}".split(",").each { name->
stage(name) {
sh "env | grep -i NODE_NAME"
}
}
}
}
}
}
CodePudding user response:
Because you are running the parallel
function inside the script
directive you must use the scripted syntax for the parallel function:
Takes a map from branch names to closures and an optional argument failFast which > will terminate all branches upon a failure in any other branch:
parallel firstBranch: { // do something }, secondBranch: { // do something else }, failFast: true|false
So you can use the collectEntries method to iterate over your list and generate the Map that will be passed to the parallel function. Something like:
stages {
stage('BuildAll') {
steps {
script {
parallel names.split(',').collectEntries { name ->
["Execution ${name}": { // Map key is the branch name
// Following code will be executed in parallel for each branch
stage(name) {
sh "env | grep -i NODE_NAME"
}
}]
}
}
}
}
}
Another option is to define the map and then call parallel:
stages {
stage('BuildAll') {
steps {
script {
def executions = names.split(',').collectEntries { name ->
["Execution ${name}": {
stage(name) {
sh "env | grep -i NODE_NAME"
}
}]
}
parallel executions
}
}
}
}