如何编写在并行作业中包含顺序作业的动态声明管道

时间:2019-05-18 16:46:45

标签: jenkins-pipeline jenkins-declarative-pipeline

我正在尝试编写一个声明性管道代码,该代码接受地图并创建管道。我可以实现顺序阶段或并行阶段,但在制作包含并行阶段内部顺序阶段的管道时遇到问题。

输入数据将为Map。映射中的每个列表应并行运行,并且列表中与每个键对应的项目应按顺序运行。

示例数据:[1:[11,12],2:[21,22],3:[31,32]]

输出应为图像。有人可以给个主意吗?

enter image description here

下面是我尝试过的代码。

def stageData = [1:[11,12], 2:[21,22], 3:[31,32]];

def getDeployStages1(stageData){
    Map deployStages = [:]
    stageData.each{ key, stgValue ->
        List stgs = []
        stgValue.each{ value ->           
            deployStages.put("${value}", {
                echo "${value}"
            })
        }        
    }
    return deployStages;
}

def getDeployStages2(stageData){
    Map deployStages = [:]
    stageData.each{ key, stgValue ->
        List stgs = []
        stgValue.each{ value ->
            stgs.add(stage("${value}"){
               echo "${value}"
            })           
        }
        deployStages.put("${key}", stgs)
    }
    return deployStages;
}

pipeline {
    agent any
    stages {
        stage ("deploy1") {
            steps {
                script {
                     parallel getDeployStages1(stageData)
                }
            }
        }
        stage ("deploy2") {
            steps {
                script {
                    parallel getDeployStages2(stageData)
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据to this documentation,您可以通过这种方式嵌套舞台

pipeline {
    agent none

    stages {
        stage("build and deploy on Windows and Linux") {
            parallel {
                stage("windows") {
                    agent {
                        label "windows"
                    }
                    stages {
                        stage("build") {
                            steps {
                                bat "run-build.bat"
                            }
                        }
                        stage("deploy") {
                            when {
                                branch "master"
                            }
                            steps {
                                bat "run-deploy.bat"
                            }
                        }
                    }
                }

                stage("linux") {
                    agent {
                        label "linux"
                    }
                    stages {
                        stage("build") {
                            steps {
                                sh "./run-build.sh"
                            }
                        }
                        stage("deploy") {
                             when {
                                 branch "master"
                             }
                             steps {
                                sh "./run-deploy.sh"
                            }
                        }
                    }
                }
            }
        }
    }
}

这应导致以下流程 enter image description here

要在您的情况下应用此方法,可以简化函数以仅返回需要顺序的元素(只是值)。

pipeline {
    agent any
    stages {
        stage ("parallel") {
        parallel {
        stage ("deploy1") {
            stages {
                 def list = getDeployStages1(stageData)
                 for (int i=0; i < list.size(); i++) {
                      stage(i) {
                          echo("${list[i]}")
                      }
            }
        }
        stage ("deploy2") {
            stages {
                //similar
            }
        }
        }
    }
}