38

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 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")...

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.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
David W.
  • 105,218
  • 39
  • 216
  • 337

4 Answers4

74

All environment variables are accessible using env, e.g. ${env.JOB_NAME}.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • That works. `JOB_NAME` was mentioned as a special Jenkins variable that suppose to be already defined. – David W. Jul 27 '16 at 15:06
15

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
  • Using $ interpolation is completely unnecessary if you're just referencing the variable, so you can just do 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

Tim Keating
  • 6,443
  • 4
  • 47
  • 53
6

Indeed just use ${env.JOB_NAME} to access a known variable.

However if you need to access environment variable where name is given by another variable (dynamic access), just use 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. 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 :

enter image description here

Pom12
  • 7,622
  • 5
  • 50
  • 69
  • 2
    This does not work, just tested it. Please give more details @Pom12. – de.la.ru Feb 24 '17 at 14:03
  • I'm currently using it succesfully for system defined environment variables, as I show in my edited post. What version of Jenkins are you using ? – Pom12 Feb 24 '17 at 14:33
  • 1
    It also allows setting the variables directly with `env[var]`. – Leon S. Mar 08 '17 at 12:17
  • I can confirm this worked with Jenkins ver. 2.46.1. Jenkins > Manage Jenkins > Configure System > Global properties > Environment variables. `name=COMMON_AUTOMATION_GIT_REPO` `value=c:/jenkins_local/shared_workspace/CommonAutomationGitRepo` Then within my groovy pipeline script: ` def commonRepo = env["COMMON_AUTOMATION_GIT_REPO"];` ` echo "Using common repo: ${commonRepo}"` which resulted in the output: `[Pipeline] echo` `Using common repo: c:/jenkins_local/shared_workspace/CommonAutomationGitRepo` – Spangen Apr 05 '17 at 14:46
  • This worked for me ```env["$variable"]``` – Skillz Aug 27 '21 at 14:48
1

I had an issue with this not working. The globally set properties/environment variables were only available inside a node step. It's a bug in version 2.4 of Pipeline plugin. Upgrade to 2.5 if you face this issue and your global properties will be available anywhere in the script. I've posted this to the Jenkins wiki here with the test script I used.

Mig82
  • 4,856
  • 4
  • 40
  • 63