Jenkins Pipeline访问环境变量 [英] Jenkins Pipeline accessing environment variables
问题描述
我正在尝试在Jenkins中使用DSL管道.我认为如果可以将项目名称用作脚本的一部分,那就太好了.
I'm trying to use DSL pipelines in Jenkins. I thought it'd be nice if I could use the project name as part of my script.
git credentialsId: 'ffffffff-ffff-ffff-ffff-ffffffffffffff',\
url: "${repo_root}/${JOB_NAME}.git"
我得到了错误:
groovy.lang.MissingPropertyException: \
No such property: JOB_NAME for class: groovy.lang.Binding
我以为我遵循了这些方向,并且他们将JOB_NAME
作为变量之一.
I thought I followed these directions, and they mention JOB_NAME
as one of the variables.
我决定尝试:
sh 'env'
在我的DSL中,并且打印出来:
in my DSL, and this prints out:
JOB_NAME = foo-bar
这是我所期望的.
另一个博客提及:>
环境变量的使用
我们有两种获取价值的方法.由于Groovy与Java的紧密关系,在启动过程中-D=
传递的属性我们可以理解为System.getProperty("key")
.
Usage of environment variables
We have two ways to get their value. The properties passed by-D=
during the startup we could read asSystem.getProperty("key")
thanks to the Groovy's strong relation with Java.
以Java方式读取普通环境变量是System.getenv("VARIABLE")
...
Reading normal environment variables in Java way is the System.getenv("VARIABLE")
...
让我们尝试一下:
println "JOB_NAME = " + System.getenv('JOB_NAME');
现在,我得到了:
java.lang.NullPointerException: Cannot get property 'System' on null object
空对象?但是,我看到JOB_NAME
是一个环境变量!
Null object? But, I can see that JOB_NAME
is an environment variable!
如何在管道作业中将$JOB_NAME
读入DSL脚本中.我正在尝试管道作业,当我获得该工作时,将使它成为具有Jenkinsfile
的多分支管道.
How do I read in the $JOB_NAME
into a DSL script in a Pipeline job. I am trying a Pipeline job, and when I get that working will make this a Multibranch Pipeline with a Jenkinsfile
.
推荐答案
所有环境变量均可使用env
访问,例如${env.JOB_NAME}
.
All environment variables are accessible using env
, e.g. ${env.JOB_NAME}
.
这篇关于Jenkins Pipeline访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!