gpt4 book ai didi

jenkins - 用于并行执行的 Currying groovy CPS 闭包

转载 作者:行者123 更新时间:2023-12-04 04:24:29 26 4
gpt4 key购买 nike

我们在一些工作中做了一些并行步骤的动态创建。感谢 this thread我发现了如何使用参数动态创建 map 以在并行步骤中使用。

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

然而,currying 似乎无法正常工作。在闭包内引用循环变量 (valueCopy) 是正确的 (as mentioned here),但柯里化(Currying)并没有达到我的预期。

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

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

Jenkins:LTS (2.32.1) 和截至 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

最佳答案

我不确定是柯里化(Currying)还是 for 循环,但需要将此函数标记为 NonCPS,如下所述:https://github.com/jenkinsci/pipeline-examples/blob/master/docs/BEST_PRACTICES.md#groovy-gotchas

本质上,这样做:

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

我认为这是你的 for 循环,但无论如何,只要你不使用经典的“C 风格”循环,你就需要将你的函数标记为 NonCPS。

关于jenkins - 用于并行执行的 Currying groovy CPS 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com