【问题标题】:Sequential stages within parallel pipeline in JenkinsJenkins中并行管道中的顺序阶段
【发布时间】:2020-01-10 21:02:47
【问题描述】:

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

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

但是,当我添加一个串行阶段时,又名:

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

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

为了增加攻击性,我看到了执行的子步骤。如何让我的子步骤显示为阶段?

另外,如果有一种方法可以使声明式管道具有动态阶段(顺序和并行),我完全赞成。我找到了you can do static sequential stages,但不知道如何在不返回脚本管道的情况下使其动态化。

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    这里是你可以做你想做的事情的方法

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

    【讨论】:

    • 有什么方法可以让prepareStage 动态化,即循环运行 3 个阶段?
    • 看起来您无法重新启动单个阶段,只能重新启动所有“我的阶段”
    • 那是因为你只能在顶级舞台上这样做[jenkins.io/doc/book/pipeline/running-pipelines/…
    • 请注意,AFAIU,没有一个阶段有分配的节点。因此,您可能需要在 node{} 中包含阶段。
    猜你喜欢
    • 2016-08-20
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2023-04-02
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多