Home > Net >  How to run jenkins jobs parallel based on an array of stages?
How to run jenkins jobs parallel based on an array of stages?

Time:11-02

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?

CodePudding user response:

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
                }
            }
        }
    }
}
  • Related