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

ぐ巨炮叔叔 提交于 2021-02-08 11:03:53

问题


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"
                        }
                    }
                }
            }

        }
    }
}

回答1:


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


来源:https://stackoverflow.com/questions/59261562/jenkinshow-to-achieve-parallel-dynamic-stages-in-jenkins-declarative-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!