这是一个非常令人讨厌的限制,但是当前您不能.each
在管道脚本中使用(如此处记录:https : //issues.jenkins-ci.org/browse/JENKINS-26481)
String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"
int top = arr.size()
echo "top is ${top}"
for (it in arr) {
echo "${it}"
}
如果实际需要并行执行,则代码将更像:
String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"
int top = arr.size()
echo "top is ${top}"
def stepsForParallel = [:]
for (int i = 0; i < arr.size(); i++) {
def it = arr[i]
def stepName = "running ${it}"
stepsForParallel[stepName] = { ->
echo "${it}"
}
}
parallel stepsForParallel
您链接到的另一个stackoverflow使用Build Flow插件,它是您正在使用的Pipeline插件的前身。这就是为什么相同的代码不起作用的原因。
在现代詹金斯(在2.150.1中进行测试)上面列出的原始错误是固定的,并且.each
可以工作。您可以为并行执行执行以下操作:
String[] arr = [ "one","two","three",'four','five' ]
def stepsForParallel = [:]
arr.each {
def stepName = "running ${it}"
stepsForParallel[stepName] = { ->
echo "${it}"
}
}
parallel stepsForParallel