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:

  1. You have to define running_set as a Map like ["task 1": { somefunc()}, "task 2": { somefunc2() }] - keys from this map are used as parallel stages names
  2. You have to pass running_set to parallel method inside script {} 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:

enter image description here


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
                    ])
                }
            }
        }
    }
}