Jenkins Pipeline 访问环境变量 [英] Jenkins Pipeline accessing environment variables
问题描述
我正在尝试在 Jenkins 中使用 DSL 管道.我认为如果我可以将项目名称用作我的脚本的一部分会很好.
git credentialsId: 'ffffffff-ffff-ffff-ffff-ffffffffffffff',网址:${repo_root}/${JOB_NAME}.git"
我得到错误:
groovy.lang.MissingPropertyException: 没有这样的属性:类的 JOB_NAME:groovy.lang.Binding
我以为我关注了 这些方向,他们提到 JOB_NAME
作为变量之一.
我决定试试:
sh 'env'
在我的 DSL 中,打印出来:
JOB_NAME = foo-bar
这是我所期望的.
另一个博客提及:p><块引用>
环境变量的使用
我们有两种方法来获得它们的价值.-D=
在启动期间传递的属性我们可以读为 System.getProperty("key")
,这要归功于 Groovy 与 Java 的密切关系.
用Java方式读取普通环境变量是System.getenv("VARIABLE")
...
让我们试试这个:
println "JOB_NAME = " + System.getenv('JOB_NAME');
现在,我明白了:
java.lang.NullPointerException:无法在空对象上获取属性系统"
空对象?但是,我可以看到 JOB_NAME
是一个环境变量!
如何将 $JOB_NAME
读入管道作业中的 DSL 脚本.我正在尝试一个管道工作,当我开始工作时,它将使它成为一个带有 Jenkinsfile
的多分支管道.
所有环境变量都可以使用 env
访问,例如${env.JOB_NAME}
.
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"
I get the error:
groovy.lang.MissingPropertyException:
No such property: JOB_NAME for class: groovy.lang.Binding
I thought I followed these directions, and they mention JOB_NAME
as one of the variables.
I decided to try:
sh 'env'
in my DSL, and this prints out:
JOB_NAME = foo-bar
which is what I expect.
Another blog mentions:
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.Reading normal environment variables in Java way is the
System.getenv("VARIABLE")
...
Let's try this:
println "JOB_NAME = " + System.getenv('JOB_NAME');
Now, I get:
java.lang.NullPointerException: Cannot get property 'System' on null object
Null object? But, I can see that JOB_NAME
is an environment variable!
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
.
All environment variables are accessible using env
, e.g. ${env.JOB_NAME}
.
这篇关于Jenkins Pipeline 访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!