在编写 Jenkins Pipeline 脚本时,从并行步骤访问变量是否安全?文档对此并不清楚。
例如,这个管道代码从并行分支修改了一个公共计数器和队列:
def donecount = 0;
def work = [6,5,4,3,2,1,0]
def branches = [:]
for (int i = 0; i < 3; i++) {
branches["worker-${i}"] = {
while (true) {
def item = null
try {
item = work.remove(0)
} catch (java.lang.IndexOutOfBoundsException e) {
break
}
echo "Working for ${item} seconds"
sleep time:item
donecount += 1
}
}
}
branches.failFast = true
parallel branches
echo "Completed ${donecount} tasks"