jenkins - 如何在 Jenkins 声明式管道的阶段中对同一函数进行并行调用

标签 jenkins groovy parallel-processing jenkins-pipeline jenkins-declarative-pipeline

在声明性管道中并行执行函数的正确方法是什么?

此论坛上的多个帖子建议您构建一个数组,并将该数组传递给“parallel”或将一个函数传递给“parallel”

Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?

Simple parallel execution in Jenkins for an array

我已经多次尝试让它工作,但它总是串行运行

我认为问题是在我构建数组时正在评估函数,甚至在我到达该阶段的“并行”步骤之前。

我无法从官方文档中得出解决方案。

https://jenkins.io/blog/2017/09/25/declarative-1/

这是我希望它的工作方式,但这可能吗?

    pipeline {

        agent {
            label "l1" && "l2"
        }

        stages {

            stage ('Prepare Data') {
                // Some code that creates that data object
                // data is an array of maps
                data
            }

            stage ('Build') {
                script {
                    def stepsForParallel = [:]
                    data.each { d ->
                        // name is a key in the data map
                        stepsForParallel["${d['name']}"] = {
                            node {
                                stage("${d['name']}") {
                                    stepsForParallel[execDownStreamJob("DownStreamJobName", d)] = {
                                        println("Executing DownstreamJob")
                                    }
                                }
                            }
                        }
                    }
                    parallel stepsForParallel
                }
            }

        }
    }
}

下面的函数实际上不在这个问题的范围内,但我添加了它以防它提供一些值(value)。在这篇文章中对此有最好的解释:https://stackoverflow.com/a/42248825/5006720

// job is a string
// jobParameters is a hashmap
def execDownStreamJob(job, jobParameters) {

    // Some parsing takes place on hashMap and creates the p object 
    // 'p' is passed to the 'build job' function as a parameter

    def downStreamJobResult = build job: job, parameters: p, propagate: false

    // Some try/catch logic

}

最佳答案

下面这段代码真的很奇怪

因为您正在尝试分配 stepsForParallel 两次

    stepsForParallel["${d['name']}"] = {
        node {
            stage("${d['name']}") {
                stepsForParallel[execDownStreamJob("DownStreamJobName", d)] = {
                    println("Executing DownstreamJob")
                }
            }
        }
    }

我相信它应该看起来像这样(现在无法测试):

    script {
        def stepsForParallel = data.collectEntries{ d ->
            ["${d.name}", 
                {
                    stage("${d.name}") {
                        println "DownstreamJob ${d.name} start")
                        execDownStreamJob("DownStreamJobName", d)
                        println "DownstreamJob ${d.name} end")
                    }
                }
            ]
        }
        parallel stepsForParallel
    }

关于jenkins - 如何在 Jenkins 声明式管道的阶段中对同一函数进行并行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60026104/

相关文章:

groovy - 在 Groovy/Spock 中断言调用方法不会被执行

f# - F# 中的并行存在函数

c# - 队列到 ConcurrentQueue

linux - 无法从 Jenkins 访问 git repo

hudson - Jenkins 构建的基于提交的 View

jenkins - 在 Jenkins Workflow 插件中获取输入批准者用户名

performance - 可伸缩性和加速之间的区别?

xml - XPath 和 Jenkins Plot 插件

android - 在 Jenkins 中显示 Android Lint 结果

python - 标准化特定语言的发布/工具组