gpt4 book ai didi

jenkins - Jenkins Pipeline访问环境变量

转载 作者:行者123 更新时间:2023-12-03 09:15:15 24 4
gpt4 key购买 nike

我正在尝试在Jenkins中使用DSL管道。我认为如果可以将项目名称用作脚本的一部分,那就太好了。

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

我以为我遵循了 these directions,他们提到 JOB_NAME作为变量之一。

我决定尝试:
sh 'env'

在我的DSL中,结果显示为:
JOB_NAME = foo-bar

这是我所期望的。

另一个 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 as System.getProperty("key") thanks to the Groovy's strong relation with Java.

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是一个环境变量!

如何在管道作业中将 $JOB_NAME读入DSL脚本中。我正在尝试管道工作,当我得到该工作时,将使它成为带有 Jenkinsfile的多分支管道。

最佳答案

所有环境变量都可以使用env访问,例如${env.JOB_NAME}

关于jenkins - Jenkins Pipeline访问环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com