Jenkins Pipeline:闭包是否不应该在并行内部和外部解析变量?
我在尝试在Jenkins管道脚本中执行代码时遇到一些令人困惑的行为。闭包内部的值带有一些意外值。我已经看到了对局部变量技巧的引用,该技巧据说可以解决此问题,但是它对我不起作用。
简化示例:创建3个作业’a’,’b’,’c’,以打印其参数-
传入并复制到本地。首先并行执行作业;然后在外部并行执行以比较结果。之所以定义“说”,是因为println在我的Jenkins中提供了访问异常(我不是管理员)。
def say(s) {println(s)} // println gives exception inside create_jobsdef create_jobs() {
def map = [:] // needed for parallel
def jobrunfn = { jobid -> // return a closure that prints hello from job
def pid = "$jobid"
return {
def xsay = { s -> say("[$pid] $s") }
xsay "HELLO from $pid"
}
}
map['a'] = jobrunfn('a')
map['b'] = jobrunfn('b')
map['c'] = jobrunfn('c')
return map
}
def jobs = create_jobs()
parallel(jobs) // for Jenkins pipeline only - not groovy interp
for (j in jobs) { jobs[j.key]() } // groovy interp - parallel not available
输出-在并行中,“ pid”有时为“ c”,应为“ a”或“ b”:
[Pipeline] parallel[Pipeline] [a] { (Branch: a)
[Pipeline] [b] { (Branch: b)
[Pipeline] [c] { (Branch: c)
[Pipeline] [a] echo
[a] [a] HELLO from c
[Pipeline] [a] }
[Pipeline] [b] echo
[b] [b] HELLO from c
[Pipeline] [b] }
[Pipeline] [c] echo
[c] [c] HELLO from c
[Pipeline] [c] }
[Pipeline] // parallel
[Pipeline] echo
[a] HELLO from a
[Pipeline] echo
[b] HELLO from b
[Pipeline] echo
[c] HELLO from c
[Pipeline] End of Pipeline
Finished: SUCCESS
该参数始终是传入的最后一个值(不知道为什么,但是它是一致的,并且我知道您需要局部变量技巧。)
但是,局部变量技巧在并行内部无法始终如一地工作:即使局部变量有时也为“ c”(对于嵌套的“ say”来说,它正确,但主体错误)。在并行之外,它得到预期的“
a”和“ b”。
我误会了吗
Linux上的Jenkins 2.32.2,管道:Grooby 2.27
回答:
这是管道的错误。看票。
以上是 Jenkins Pipeline:闭包是否不应该在并行内部和外部解析变量? 的全部内容, 来源链接: utcz.com/qa/407638.html