在管道作业中设置每个节点的环境变量 [英] Setting a per-node environment variable in a pipeline job

查看:144
本文介绍了在管道作业中设置每个节点的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jenkins管道看起来像这样(请原谅一些小的语法错误):

My Jenkins pipeline looks something like this (please forgive small syntax errors):

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.

My intention is to have one node with ENVVAR=foo and one with ENVVAR=bar.

我实际上看到的是,当env命令运行时,两个节点上都设置了ENVVAR=bar.

What I actually see is that when the env command runs, ENVVAR=bar is set on both nodes.

根据本教程,"[[特殊变量env]是当前节点上的环境变量."所以我希望这能奏效.

According to this tutorial, "properties of [the special variable env] are environment variables on the current node." So I'd expect this to work.

推荐答案

在本教程的后面,它说:

Much later on in the tutorial, it says:

在这种情况下,请勿使用env:

Do not use env in this case:

env.PATH = "${mvnHome}/bin:${env.PATH}"

因为环境变量的覆盖仅限于对管道运行是全局的,而不是对当前线程(以及代理)而言是局部的.但是,您可以使用如上所述的withEnv步骤.

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步骤中.

Looks like an ugly limitation of the DSL. It works wrapping the stage in a withEnv step.

这篇关于在管道作业中设置每个节点的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆