用于并行执行的 Currying groovy CPS 闭包 [英] Currying groovy CPS closure for parallel execution

查看:23
本文介绍了用于并行执行的 Currying groovy CPS 闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在某些工作中动态创建并行步骤.感谢 this thread 我发现如何使用参数动态创建地图以在并行步骤中使用.

We do some dynamic creation of parallel steps in some of our jobs. Thanks to this thread I found how to dynamically create the map with parameters for use in the parallel step.

但是现在我想重用用于创建这些并行步骤的部分代码.为此,我觉得我需要咖喱闭包.

However now I wanted to reuse parts of the code which is used to create those parallel steps. For this I feel that I would need to curry the closures.

但是,currying 似乎无法正常工作.在闭包内引用循环变量(valueCopy)是正确的(as described here) 但currying并没有达到我的预期.

However currying seems not to work correctly. Referencing the loop variable (valueCopy) inside the closure does the right thing (as mentioned here) but currying does not do what I would expect.

我是不是做错了什么,这只是(尚)不支持吗,有什么解决方法吗?这可能是 Jenkins 管道中的错误吗?

Am I doing something wrong, is this just not (yet) supported, are there any workarounds? Is this probably a bug in Jenkins pipeline?

希望任何人都知道为什么这不起作用和/或如何使它起作用.

Hope anybody has a clue why this doesn't work and/or how to get it working.

Jenkins:LTS (2.32.1) &截至 2017/01/19 的最新插件更新.

Jenkins: LTS (2.32.1) & latest plugin updates as of 2017/01/19.

升级到 Pipeline 后:Groovy 插件 2.40 版现在一切正常.

流水线脚本执行:

def echoSome(val) {
    echo val
}

def buildClosures() {
    def someList = ["1", "2", "3"]
    def closures = [:]
    for (value in someList) {
        final valueCopy = value

        closures[value] = {val ->
                echo valueCopy.toString()
                echo val.toString()
            }.curry(value)
    }
    closures
}

parallel buildClosures()

输出:

[Pipeline] parallel
[Pipeline] [1] { (Branch: 1)
[Pipeline] [2] { (Branch: 2)
[Pipeline] [3] { (Branch: 3)
[Pipeline] [1] echo
[1] 1
[Pipeline] [1] echo
[1] 3
[Pipeline] [1] }
[Pipeline] [2] echo
[2] 2
[Pipeline] [2] echo
[2] 3
[Pipeline] [2] }
[Pipeline] [3] echo
[3] 3
[Pipeline] [3] echo
[3] 3
[Pipeline] [3] }
[Pipeline] // parallel
[Pipeline] End of Pipeline
Finished: SUCCESS

预期输出:

[Pipeline] parallel
[Pipeline] [1] { (Branch: 1)
[Pipeline] [2] { (Branch: 2)
[Pipeline] [3] { (Branch: 3)
[Pipeline] [1] echo
[1] 1
[Pipeline] [1] echo
[1] 1
[Pipeline] [1] }
[Pipeline] [2] echo
[2] 2
[Pipeline] [2] echo
[2] 2
[Pipeline] [2] }
[Pipeline] [3] echo
[3] 3
[Pipeline] [3] echo
[3] 3
[Pipeline] [3] }
[Pipeline] // parallel
[Pipeline] End of Pipeline
Finished: SUCCESS

推荐答案

发现用最新的管道:Groovy 插件 (2.40) 与至少 Jenkins 版本 2.60.3 结合使用(尽管插件主页声明您至少需要 Jenkins 2.73.3)一切都按预期工作.

Found that with the latest Pipeline: Groovy plugin (2.40) combined with at least Jenkins Version 2.60.3 (works although the plug-ins homepage states that you need at least Jenkins 2.73.3) everything works as expected.

这篇关于用于并行执行的 Currying groovy CPS 闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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