简体   繁体   English

Jenkins Pipeline访问环境变量

[英]Jenkins Pipeline accessing environment variables

I'm trying to use DSL pipelines in Jenkins. 我正在尝试在Jenkins中使用DSL管道。 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. 我以为我遵循了这些指示 ,他们提到JOB_NAME是其中一个变量。

I decided to try: 我决定尝试:

sh 'env'

in my DSL, and this prints out: 在我的DSL中,打印出来:

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 as System.getProperty("key") thanks to the Groovy's strong relation with Java. 由于Groovy与Java的密切关系,在启动期间由-D=传递的属性我们可以读作System.getProperty("key")

Reading normal environment variables in Java way is the System.getenv("VARIABLE") ... 以Java方式读取常规环境变量是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! 但是,我可以看到JOB_NAME是一个环境变量!

How do I read in the $JOB_NAME into a DSL script in a Pipeline job. 如何将$JOB_NAME读入Pipeline作业中的DSL脚本。 I am trying a Pipeline job, and when I get that working will make this a Multibranch Pipeline with a Jenkinsfile . 我想一个流水线作业,当我拿到工作将使这一个多枝管道与Jenkinsfile

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

Okay this really vexed me for a while today. 好吧,今天真的让我烦恼了一会儿。 Ultimately, I was being done in by a couple of things: 最终,我做了几件事:

  • Single-quoted strings in Groovy mean "don't evaluate variables," just like it does in bash Groovy中的单引号字符串意味着“不评估变量”,就像它在bash中一样
  • Using $ interpolation is completely unnecessary if you're just referencing the variable, so you can just do env.JOB_NAME . 如果您只是引用变量,则完全不需要使用$ interpolation,因此您可以执行env.JOB_NAME

This SO question proved to be the one that helped me crack the code: Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT 这个问题被证明是帮助我破解代码的问题: Jenkins Workflow Checkout访问BRANCH_NAME和GIT_COMMIT

Indeed just use ${env.JOB_NAME} to access a known variable. 确实只需使用${env.JOB_NAME}来访问已知变量。

However if you need to access environment variable where name is given by another variable (dynamic access), just use env["your-env-variable"] . 但是,如果您需要访问其他变量(动态访问)给出名称的环境变量,只需使用env["your-env-variable"]

I had the problem where I configured 3 environment variables (in Jenkins -> Administer -> Configure System -> Environment variables ), let's name them ENV_VAR_1 , ENV_VAR_2 , ENV_VAR_3 . 我遇到了配置3个环境变量的问题(在Jenkins -> Administer -> Configure System -> Environment variables ),我们将它们命名为ENV_VAR_1ENV_VAR_2ENV_VAR_3 Now I want to dynamically access them, I can do as such : 现在我想动态访问它们,我可以这样做:

def envVarName = "ENV_VAR_" + count  // Suppose count is initialized in a loop somewhere above...

def value = env[envVarName]  // Will be resolved to env["ENV_VAR_1"] depending on count value

My environment variables in Jenkins configuration look like this : 我在Jenkins配置中的环境变量如下所示:

在此输入图像描述

I had an issue with this not working. 我有一个问题,这不起作用。 The globally set properties/environment variables were only available inside a node step. 全局设置的属性/环境变量仅在node步骤中可用。 It's a bug in version 2.4 of Pipeline plugin. 这是Pipeline插件2.4版中的一个错误。 Upgrade to 2.5 if you face this issue and your global properties will be available anywhere in the script. 如果您遇到此问题,请升级到2.5,并且您的全局属性将在脚本中的任何位置可用。 I've posted this to the Jenkins wiki here with the test script I used. 我已为这詹金斯维基这里与测试脚本我使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM