Jenkins 并行管道中的顺序阶段

Ole*_*siy 10 jenkins jenkins-groovy jenkins-pipeline

我在 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
    }
}
Run Code Online (Sandbox Code Playgroud)

它给我带来了这个美丽的并行管道:

在此输入图像描述

然而,当我添加串行阶段时,又名:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

另外,如果有一种方法可以通过声明性管道实现动态阶段(顺序和并行),我完全赞成。我发现您可以执行静态顺序阶段,但几乎不知道如何使其动态而无需返回脚本化管道。

Ser*_*ers 13

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

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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 请注意,AFAIU,所有阶段都没有分配的节点。因此,您可能需要将这些阶段包含在“node{}”中。 (3认同)
  • 这是因为您只能在顶级阶段执行此操作[https://jenkins.io/doc/book/pipeline/running-pipelines/#restart-from-a-stage] (2认同)