Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline accessing environment variables

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.

like image 333
David W. Avatar asked Jul 26 '16 20:07

David W.


People also ask

How do you pass an environment variable in Jenkins pipeline?

Like if I need to inject a value within Steps/Script section, in Jenkins pipeline, I can define globally in the environment variables or using Jenkins project Configure General Check mark Prepare an environment for the run Check mark Keep Jenkins environment variables I can provide the environment variable in the ...

Where does Jenkins get environment variables?

The path to become a Jenkins expert An easy way to obtain the Jenkins environment variables list from your local installation is to append env-vars. html to the server's URL. For a locally hosted Jenkins server, the URL would be: http://localhost:8080/env-vars.html.

Are Jenkins parameters environment variables?

The parameters are available as environment variables. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env. FOO} ) can access these values.


2 Answers

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

like image 125
Krzysztof Krasoń Avatar answered Oct 06 '22 09:10

Krzysztof Krasoń


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

like image 40
Tim Keating Avatar answered Oct 06 '22 10:10

Tim Keating