我们在一些工作中进行动态创建并行步骤。感谢这个帖子,我找到了如何动态创建具有参数映射以在并行步骤中使用的方法。
但是现在我想重用用于创建这些并行步骤的代码的某些部分。为此,我觉得我需要对闭包进行柯里化。
然而,柯里化似乎不能正常工作。在闭包内引用循环变量(valueCopy)会做正确的事情(如此提到),但柯里化不会像我期望的那样工作。
我做错了什么吗?这只是不受支持的(尚未支持)吗?有没有解决办法?这可能是 Jenkins Pipeline 中的一个 bug 吗?
希望有人知道为什么这不起作用和/或如何使其起作用。
Jenkins 版本:LTS (2.32.1) 和截至 2017/01/19 的最新插件更新。
解决方案:
升级到 Pipeline: Groovy 插件版本 2.40 后,一切都按预期工作了。
执行的 Pipeline 脚本:
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