Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovyjenkins-cli

Jenkins:How to Achieve parallel dynamic stages in jenkins declarative pipeline


I am working on declarative pipeline. I am trying to achieve dynamic stages which should distribute the stages parallel with the agents defined. When i explored i learned how to achieve Dynamic sequential stages. Below is my sample code.

My problem now is, how to achieve parallel stages with agents i have. For example, if i have 3 agents all the 5 stages should run parallel in the agents parallel. I tried using parallel tests but not working. Please help me to improve further !

def learn
pipeline {
    agent none

    stages {
        stage('Dynamic Stages') {
          steps {
                script {
                    learn = ["1", "2", "3", "4", "5"]
                    for(int i=0; i < list.size(); i++) {

                        stage(list[i]){
                            echo "value: $i"
                        }
                    }
                }
            }

        }
    }
}

Solution

  • The following should run all stages in parallel. Jenkins will just take whatever node is available.

    def learn
    pipeline {
        agent none
    
        stages {
            stage('Dynamic Stages') {
              steps {
                    script {
                        learn = ["1", "2", "3", "4", "5"]
                        def builders = [:]
                        for(i in learn) {
                            def value = i // Need to bind the label variable before the closure - can't do 'for (i in learn)
                            builders[i] = {
                                node {
                                    echo "value: $i"
                                }
                            }
                        }
                        parallel builders
                    }
                }
            }
        }
    }