Jenkins中并行管道中的顺序阶段

时间:2020-01-10 21:02:47

标签: jenkins jenkins-pipeline jenkins-groovy

我在Jenkins中有一个动态的脚本管道,该管道具有许多并行阶段,但是在每个阶段中,都有多个串行步骤。我已经花了几天时间试图使它起作用:无论我如何尝试,所有串行子级都集中在一个级中! 这是我现在拥有的:

node () {
    stage("Parallel Demo") {
        // Canonical example to run steps in parallel

        // The map we'll store the steps in
        def stepsToRun = [:]

        for (int i = 1; i < 5; i++) {
            stepsToRun["Step${i}"] = { node {
                echo "start"
                sleep 1
                echo "done"
            }}
        }
        // Actually run the steps in parallel
        // parallel takes a map as an argument
        parallel stepsToRun
    }
}

这让我得到了一个漂亮的并行管道:

enter image description here

但是,在我添加一个连续舞台的那一刻,又名:

node () {
    stage("Parallel Demo") {
        // Run steps in parallel

        // The map we'll store the steps in
        def stepsToRun = [:]

        for (int i = 1; i < 5; i++) {
            stepsToRun["Step${i}"] = { node {
                stage("1") {
                    echo "start 1"
                    sleep 1
                    echo "done 1"
                }
                stage("2") {
                    echo "start 2"
                    sleep 1
                    echo "done 2"
                }                
            }}
        }
        // Actually run the steps in parallel
        // parallel takes a map as an argument
        parallel stepsToRun
    }
}

我得到了这个丑陋的东西,看起来完全一样:

enter image description here

要添加到进攻中,我看到了执行的子步骤。如何使子步骤显示为阶段?

此外,如果有一种方法可以使声明性管道具有动态阶段(顺序和并行),那么我全力以赴。我找到了you can do static sequential stages,但是几乎不知道如何使其动态化而又不返回脚本管道。

1 个答案:

答案 0 :(得分:8)

这是您可以按照自己的意愿做的事情

def stepsToRun = [:]

pipeline {
    agent none

    stages {
        stage ("Prepare Stages"){
            steps {
                script {
                    for (int i = 1; i < 5; i++) {
                        stepsToRun["Step${i}"] = prepareStage("Step${i}")
                    }   
                    parallel stepsToRun
                }
            }
        }
    }
}

def prepareStage(def name) {
    return {
        stage (name) {
            stage("1") {
                echo "start 1"
                sleep 1
                echo "done 1"
            }
            stage("2") {
                echo "start 2"
                sleep 1
                echo "done 2"
            }
        }
    }
}

enter image description here