Dynamically defining parallel steps in declarative jenkins pipeline

pbn picture pbn · Apr 10, 2018 · Viewed 9.1k times · Source

I try to parallelize dynamically defined set of functions as follows:

def somefunc() {
    echo 'echo1'
}

def somefunc2() {
    echo 'echo2'
}

running_set = [
    { somefunc() },
    { somefunc2() }
]

pipeline {
    agent none
    stages{
        stage('Run') {
            steps {
                parallel(running_set)                
            }

        }
    }
}

And what I end up with is:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 17: No "steps" or "parallel" to execute within stage "Run" @ line 17, column 9.
           stage('Run') {

Although steps are defined within stage 'Run'. Anyway what I would like to achieve running is a dynamically defined set of functions to execute in parallel.

Answer

Szymon Stepniak picture Szymon Stepniak · Apr 10, 2018

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