简体   繁体   中英

Dynamically defining parallel steps in declarative jenkins pipeline

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.

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM