Jenkins中并行管道中的顺序阶段 [英] Sequential stages within parallel pipeline in Jenkins

查看:28
本文介绍了Jenkins中并行管道中的顺序阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jenkins 中有一个动态脚本流水线,它有许多并行阶段,但在每个阶段中,都有多个串行步骤.我浪费了几天时间试图让它工作:无论我尝试什么,所有串行子阶段都集中在一个阶段!这是我现在拥有的:

节点(){阶段(并行演示"){//并行运行步骤的规范示例//我们将在其中存储步骤的地图def stepToRun = [:]对于 (int i = 1; i < 5; i++) {stepsToRun["Step${i}"] = { 节点 {回声开始"睡觉 1回声完成"}}}//实际上并行运行这些步骤//并行将地图作为参数并行步骤ToRun}}

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

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

节点(){阶段(并行演示"){//并行执行步骤//我们将在其中存储步骤的地图def stepToRun = [:]对于 (int i = 1; i < 5; i++) {stepsToRun["Step${i}"] = { 节点 {阶段1") {回声开始1"睡觉 1回声完成1"}阶段(2"){回声开始2"睡觉 1回声完成2"}}}}//实际上并行运行这些步骤//并行将地图作为参数并行步骤ToRun}}

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

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

另外,如果有一种方法可以使声明式管道具有动态阶段(顺序和并行),我完全赞成.我发现

I have a dynamic scripted pipeline in Jenkins that has many parallel stages, but within each stage, there are multiple serial steps. I have wasted several days trying to make it work: no matter what I try, all serial substages are lumped into one stage! Here is what I have now:

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

It gets me this beautiful parallel pipeline:

However, the moment I add a serial stage, aka:

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

I get this ugly thing, which looks exactly the same:

To add to the offense, I see the sub-steps executed. How can I get my sub-steps show up as stages?

Also, if there is a way to have dynamic stages (sequential and parallel) with the declarative pipeline, I'm all for it. I found you can do static sequential stages but have little clue how to make it dynamic without going back to scripted pipelines.

解决方案

Here is how you can do something like you want

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

这篇关于Jenkins中并行管道中的顺序阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆