将 Jenkins 构建参数传递给管道节点 [英] Pass Jenkins build parameters to pipeline nodes
问题描述
我创建了一个新的 Jenkins 管道.管道(当前)使用名为 VAR_A
的单个布尔选项进行参数化.我的管道脚本是:
I created a new Jenkins pipeline. The pipeline is (currently) parametrized with a single boolean option named VAR_A
. My pipeline script is:
node ('windows') {
echo "$VAR_A"
bat 'env'
}
当我在选中 VAR_A
的情况下手动构建项目时,如预期的那样回显true".但是,环境变量列表不显示 VAR_A=true
.
When I manually build the project with VAR_A
checked, "true" is echoed, as expected. The list of environment variables, however, does not show VAR_A=true
.
如果我将调用包装在 withEnv
块中,我可以让 env
显示 VAR_A
:
I am able to get env
to show VAR_A
if I wrap the call in a withEnv
block:
node ('windows') {
echo "$VAR_A"
withEnv(["VAR_A=$VAR_A"]) {
bat 'env'
}
}
我将使用比这更多的参数,因此不需要单独指定每个参数.有没有办法将所有构建参数转移到节点的环境中?
I will more parameters than this, so specifying each parameter individually is not desired. Is there a way to transfer all build parameters to a node's environment?
推荐答案
关键是在 Pipeline 脚本中,作业参数不会像常规作业那样自动注入环境.每个参数都成为 Pipeline 脚本的一个变量 binding.因此,您可以通过名称直接访问它们.
The point is that in Pipeline scripts the job parameters are not injected into the environment automatically as for regular jobs. Each parameter becomes a variable of the Pipeline script binding. Therefore you can access them directly by name.
在您的示例中,echo "$VAR_A"
变量替换由 groovy 在脚本上下文中执行(请参阅 关于字符串插值的 Groovy 文档).这就是您在 bat
输出中看不到它的原因.
In your example echo "$VAR_A"
variable substitution is performed by groovy in the context of your script (see Groovy doc on strings interpolation). That's why you don't see it in the bat
output.
对于您要注入的每个参数,您需要添加如下一行:env.VAR_A = VAR_A
在脚本的开头.它可以在 node
块之外,因为 env
在整个脚本中是全局的.
For each parameter you want to inject you need to add a line like this:
env.VAR_A = VAR_A
in the beginning of your script. It can be outside of node
block because env
is global within the whole script.
另外,还有一种方法可以将所有脚本变量,包括参数甚至 Pipeline 内置变量,即 steps
添加到环境中.不幸的是,它需要一些白名单才能在沙箱中运行:
Alternatively there is a way to add all the script vars, including parameters and even Pipeline built-in vars i.e. steps
into the environment. Unfortunately it will require some whitelisting to run in a sandbox:
@NonCPS
def populateEnv(){ binding.variables.each{k,v -> env."$k" = "$v"} }
populateEnv()
示例:VAR_A 是一个参数.脚本正文:
Example: VAR_A is a parameter. Script body:
def AAAA = 1 // such a definition doesn't put variable in the binding
BBBB = 2 // creates a binding variable. Absolutely the same behavior as for a job parameter.
@NonCPS
def populateEnv(){ binding.variables.each{k,v -> env."$k" = "$v"} }
populateEnv() // at this point injection happens
CCCC = 3 // created after the injection hence it won't appear in env.
node ('windows') {
bat 'env'
}
在 bat
输出中,您将找到 VAR_A
和 BBBB
.
In the bat
output you will find VAR_A
and BBBB
.
IMO 除非您的工作定义了数十个参数,否则首选 env.VAR_A = VAR_A
方法,因为它更简单、直接且无需批准.
IMO unless your job has tens of parameters defined env.VAR_A = VAR_A
approach is preferred as more simple, straightforward, and requiring no approvals.
这篇关于将 Jenkins 构建参数传递给管道节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!