gpt4 book ai didi

Jenkins 管道 : Shouldn't closures resolve variables the same inside parallel as outside?

转载 作者:行者123 更新时间:2023-12-05 00:52:45 25 4
gpt4 key购买 nike

我在尝试在 Jenkins 管道脚本中执行代码时遇到了一些令人困惑的行为。闭包内的值具有一些意想不到的值。我已经看到对局部变量技巧的引用,据说可以解决这个问题,但它对我不起作用。

简化示例:创建 3 个作业 'a'、'b'、'c',打印它们的参数 - 传入并复制到本地。首先并行执行作业;然后在外部并行执行以比较结果。 “说”的定义是因为 println 在我的 Jenkins 中给出了访问异常(我不是管理员)。

def say(s) {println(s)} // println gives exception inside create_jobs

def 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”(对于嵌套的“说”,它是正确的,但主体却是错误的);在并行之外,它按预期获得 'a' 和 'b'。

我误会了吗?

Linux 上的 Jenkins 2.32.2,管道:Grooby 2.27

最佳答案

这是管道的错误。见 ticket .

关于 Jenkins 管道 : Shouldn't closures resolve variables the same inside parallel as outside?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42416854/

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