在管道作业中设置每个节点的环境变量
Setting a per-node environment variable in a pipeline job
我的 Jenkins 管道看起来像这样(请原谅小的语法错误):
def buildsToDo = "foo bar".tokenize()
def buildPlan = [:]
for (int i = 0; i < buildsToDo.size(); i ++) {
def tag = buildsToDo[i]
buildPlan[tag] = {
node(tag) {
env.ENVVAR = tag
stage("build " + tag) {
sh 'env'
}
}
}
}
parallel(buildPlan)
我的意图是让一个节点 ENVVAR=foo
和一个节点 ENVVAR=bar
.
我实际看到的是,当 env
命令运行时,两个节点上都设置了 ENVVAR=bar
。
根据 this tutorial,"properties of [the special variable env
] are environment variables on the current node." 所以我希望它能工作。
在教程的后面,它说:
Do not use env in this case:
env.PATH = "${mvnHome}/bin:${env.PATH}"
because environment variable overrides are limited to being global to a pipeline run, not local to the current thread (and thus agent). You could, however, use the withEnv step as noted above.
看起来像是 DSL 的丑陋限制。它可以在 withEnv
步中包裹舞台。
Jenkins 管道插件还很年轻,目前还远未稳定(我可以这么说)。
他们尝试应用的 CPS 概念也会在很多方面影响执行,到目前为止我已经有过一些令人兴奋的时刻(虽然它同时真的很棒,确实)
您可能想尝试按以下方式调整您的代码,运行 需要 'withEnv'
块中的命令。将 volatile 变量移出并行块也有帮助:
def buildsToDo = "foo bar".tokenize()
def buildPlan = [:]
for (int i = 0; i < buildsToDo.size(); i ++) {
def tag = buildsToDo[i]
buildPlan[tag] = {
node(tag) {
// Note environment is modified here !
withEnv(env + [ENVVAR=tag]) {
stage("build " + tag) {
sh 'env'
}
}
}
}
}
parallel(buildPlan)
我的 Jenkins 管道看起来像这样(请原谅小的语法错误):
def buildsToDo = "foo bar".tokenize()
def buildPlan = [:]
for (int i = 0; i < buildsToDo.size(); i ++) {
def tag = buildsToDo[i]
buildPlan[tag] = {
node(tag) {
env.ENVVAR = tag
stage("build " + tag) {
sh 'env'
}
}
}
}
parallel(buildPlan)
我的意图是让一个节点 ENVVAR=foo
和一个节点 ENVVAR=bar
.
我实际看到的是,当 env
命令运行时,两个节点上都设置了 ENVVAR=bar
。
根据 this tutorial,"properties of [the special variable env
] are environment variables on the current node." 所以我希望它能工作。
在教程的后面,它说:
Do not use env in this case:
env.PATH = "${mvnHome}/bin:${env.PATH}"
because environment variable overrides are limited to being global to a pipeline run, not local to the current thread (and thus agent). You could, however, use the withEnv step as noted above.
看起来像是 DSL 的丑陋限制。它可以在 withEnv
步中包裹舞台。
Jenkins 管道插件还很年轻,目前还远未稳定(我可以这么说)。 他们尝试应用的 CPS 概念也会在很多方面影响执行,到目前为止我已经有过一些令人兴奋的时刻(虽然它同时真的很棒,确实)
您可能想尝试按以下方式调整您的代码,运行 需要 'withEnv'
块中的命令。将 volatile 变量移出并行块也有帮助:
def buildsToDo = "foo bar".tokenize()
def buildPlan = [:]
for (int i = 0; i < buildsToDo.size(); i ++) {
def tag = buildsToDo[i]
buildPlan[tag] = {
node(tag) {
// Note environment is modified here !
withEnv(env + [ENVVAR=tag]) {
stage("build " + tag) {
sh 'env'
}
}
}
}
}
parallel(buildPlan)