【问题标题】:Reuse parallel stage in declarative Jenkinsfile在声明性 Jenkinsfile 中重用并行阶段
【发布时间】:2021-03-26 10:23:50
【问题描述】:

我有一个声明式 Jenkins 管道,其中包含多个并行运行的相同阶段。我想重用这些部分,但到目前为止,我所能达到的最好效果如下:

stage('Deploy & E2E') {
    parallel {
        stage('E2E 1') {
            agent { ... }
            environment { ... }
            steps {
                script { runE2ESteps() }
            }
            post { ... }
        }
        stage('E2E 2') {
            agent { ... }
            environment { ... }
            steps {
                script { runE2ESteps() }
            }
            post { ... }
        }
        stage('Other unrelated stage, not wanting reuse') {
            agent { ... }
            environment { ... }
            steps {
                // something else
            }
            post { ... }
        }
    }
}

我能够部分重用位,方法是将步骤提取到一个方法中,但在 agentenvironmentpost 块中仍有相当多的重复.理想情况下,我想写如下内容:

stage('Deploy & E2E') {
    parallel {
        script {
            for (int i = 0; i < 5; i++) {
                stage('E2E ${i}') {
                    agent { ... }
                    environment { ... }
                    steps { ... }
                    post { ... }
                }
            }
        }
        stage('Other unrelated stage, not wanting reuse') {
            agent { ... }
            environment { ... }
            steps {
                // something else
            }
            post { ... }
        }
    }
}

但显然我不能在声明性土地上执行此操作 - Jenkins 似乎非常挑剔您在文件中的各个点被允许拥有的确切内容。

我也尝试了其他一些方法,但都没有奏效:

  • 在 E2E 块中使用 matrix 似乎很有希望,但你不能将 matrix 嵌套在 parallel 中,所以这没有成功(因为我们希望并行运行其他不相关的阶段E2E 步骤)
  • 使用内联parallel 命令。这并没有达到我想要的效果,因为我需要每个阶段都在自己的代理中运行(而不是在同一个代理中并行)。

我附上了管道的外观图 - 它现在的行为完全符合我的要求,只是重复的代码比我想要的要多。如果我们想改变 E2E 的并行性,目前需要复制+粘贴或删除一段代码,而不是(理想情况下)在某处更改一个数字。

也许这是不可能的,但我想我不妨扔掉一个帖子,看看有没有我遗漏的想法。

【问题讨论】:

    标签: jenkins jenkins-declarative-pipeline


    【解决方案1】:
    stage('Deploy & E2E') {
        parallel {
            script {
            stage('Other unrelated stage, not wanting reuse') {
                agent { ... }
                environment { ... }
                steps {
                    // something else
                }
                post { ... }
            }
                for (int i = 0; i < 5; i++) {
                    stage('E2E ${i}') {
                        agent { ... }
                        environment { ... }
                        steps { ... }
                        post { ... }
                    }
                }
            }
        }
    }
    

    试试看

    【讨论】:

    • 不,很遗憾,这遇到了我的方法遇到的同样问题:org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 364: Expected a stage @ line 364, column 17. script { ^ WorkflowScript: 363: No stages specified @ line 363, column 13. parallel { ^ WorkflowScript: 363: No stages specified @ line 363, column 13. parallel { ^ 看来你不能在parallel 中拥有script :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2019-06-17
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多