在对问题Jenkins pipeline (parallel && dynamically)?的公认答案中,这些是一个别名变量,乍看之下似乎没有用,但删除却违反了要求。为什么会这样?
这是要求的代码
properties([
parameters([
string(name: 'countTotal', defaultValue: '3')
])
])
def stages = [failFast: true]
for (int i = 0; i < params.countTotal.toInteger(); i++) {
def vmNumber = i //alias the loop variable to refer it in the closure
stages["deployVM ${vmNumber}"] = {
stage("deployVM ${vmNumber}") {
sh "echo p1; sleep 12s; echo phase${vmNumber}"
}
}
}
node() {
parallel stages
}
为什么需要变量 vmNumber ?如果我们省略它,将创建3个阶段,但所有阶段的名称均为 deployVM 3