Search code examples
jenkinsgroovydeploymentjenkins-pipelinejenkins-groovy

How to run a few Jenkins jobs in parallel based on an array of stages?


I have a pipeline that should run 3 different jobs in parallel based on a static array of my components. This is my code:

pipeline {
    agent any

    parameters {
        ...
    }

    stages {
        stage('Deployment Jobs') {
            steps {
                script {
                    parallel ['X', 'Y', 'Z'].collectEntries { value ->
                        ["Deploy ${value}": {
                            build job: "${value}_deploy",
                                    parameters: [
                                            ...
                                    ],
                                    wait: true
                        }]
                    }
                }
            }
        }
    }
}

I am getting the error:

groovy.lang.MissingPropertyException: No such property: parallel for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:251)
    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)

What am I missing?


Solution

  • Not sure why Jenkins doesn't like the way you create the Closures. Try the following with slight modifications to your code.

    pipeline {
        agent any
    
        parameters {
            ...
        }
    
        stages {
            stage('Deployment Jobs') {
                steps {
                    script {
                        def parallelMap = ['X', 'Y', 'Z'].collectEntries { value ->
                            ["Deploy ${value}": {
                                build job: "${value}_deploy",
                                        parameters: [
                                                ...
                                        ],
                                        wait: true
                            }]
                        }
                        parallel parallelMap
                    }
                }
            }
        }
    }