Dynamically defining parallel steps in declarative jenkins pipeline
If you want to use dynamic parallel block with declarative pipeline script, you have to apply two changes to your Jenkinsfile:
- You have to define
running_set
as aMap
like["task 1": { somefunc()}, "task 2": { somefunc2() }]
- keys from this map are used as parallel stages names - You have to pass
running_set
toparallel
method insidescript {}
block
Here is what updated Jenkinsfile could look like:
def somefunc() {
echo 'echo1'
}
def somefunc2() {
echo 'echo2'
}
running_set = [
"task1": {
somefunc()
},
"task2": {
somefunc2()
}
]
pipeline {
agent none
stages{
stage('Run') {
steps {
script {
parallel(running_set)
}
}
}
}
}
And here is what it looks like in Blue Ocean UI:
It is not obvious. But Szymon's way can be very straightforward.
pipeline {
agent none
stages{
stage('Run') {
steps {
script {
parallel([
'parallelTask1_Name': {
any code you like
},
'parallelTask2_Name': {
any other code you like
},
... etc
])
}
}
}
}
}