[workflow-plugin] Access to environment variables in flow.groovy

12,968 views
Skip to first unread message

Kent Johnson

unread,
Mar 27, 2015, 1:44:07 PM3/27/15
to jenkins...@googlegroups.com
I haven't seen anywhere guidance on how to access environment variables such as BUILD_NUMBER from a flow.groovy workflow definition script? I tried Timur's solution by passing in $BUILD_NUMBER as the build parameter but then it was interpreted as a string instead of pulling the build number from the shell environment.

My goal is to tag a Docker image with the build number for reproducibility and auditability reasons. I am using a workflow definition to store my JAR binary into a Docker container which then gets deployed to a Docker Host for testing and production use.

Is there currently any way pull the build number from the environment into a workflow definition script?

I tried running a shell command to echo the $BUILD_NUMBER to see if I might be able to echo it and then store it in a variable in my Groovy script. That didn't work and it gave me the a MissingPropertyException:

groovy.lang.MissingPropertyException: No such property: BUILD_NUMBER for class: groovy.lang.Binding
	at groovy.lang.Binding.getVariable(Binding.java:62)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:139)


Maybe there is something I am limited to by the fact I am in a Groovy sandbox.

This would be a nice feature if we could get it working.


Kent Johnson

unread,
Apr 1, 2015, 12:55:37 PM4/1/15
to jenkins...@googlegroups.com
This issue was resolved by Jesse Glick back in December of 2014 according to this JIRA: JENKINS-26194

All that is needed to access environment variables in the flow.groovy of a Workflow job in Jenkins is to use code like "env.BUILD_NUMBER" for the build number or for other environment variables.

The available environment variables for your jenkins installation can be viewed at:
http://[your-jenkins-server]:[your-jenkins-port-number]/env-vars.html

The list mine comes up with is as follows:

The following variables are available to shell scripts
BUILD_NUMBER
The current build number, such as "153"
BUILD_ID
The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
BUILD_DISPLAY_NAME
The display name of the current build, which is something like "#153" by default.
JOB_NAME
Name of the project of this build, such as "foo" or "foo/bar". (To strip off folder paths from a Bourne shell script, try: ${JOB_NAME##*/})
BUILD_TAG
String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". Convenient to put into a resource file, a jar file, etc for easier identification.
EXECUTOR_NUMBER
The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
NODE_NAME
Name of the slave if the build is on a slave, or "master" if run on master
NODE_LABELS
Whitespace-separated list of labels that the node is assigned.
WORKSPACE
The absolute path of the directory assigned to the build as a workspace.
JENKINS_HOME
The absolute path of the directory assigned on the master node for Jenkins to store data.
JENKINS_URL
Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)
BUILD_URL
Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)
JOB_URL
Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)
SVN_REVISION
Subversion revision number that's currently checked out to the workspace, such as "12345"
SVN_URL
Subversion URL that's currently checked out to the workspace.

 Here are a few of the top lines of my flow.groovy showing how to use these in a script:
node('master') {
 
// PULL IN ENVIRONMENT VARIABLES
 
// Jenkins makes these variables available for each job it runs
 
def buildNumber = env.BUILD_NUMBER
 
def workspace = env.WORKSPACE
 
def buildUrl = env.BUILD_URL
// PRINT ENVIRONMENT TO JOB
echo "workspace directory is $workspace"
echo "build URL is $buildUrl"

Yes, I think I could just echo "$env.BUILD_NUMBER" for those who are wondering. I didn't test doing so though I am sure it would work.

Thank you Jesse for pointing this feature out in that JIRA post! In his post he gave a pointer to his GitHub post to the TUTORIAL.MD file showing how to use the environment variables in a script.

Jesse Glick

unread,
Apr 1, 2015, 2:32:06 PM4/1/15
to jenkins...@googlegroups.com
On Wednesday, April 1, 2015 at 12:55:37 PM UTC-4, Kent Johnson wrote:
 I think I could just echo "$env.BUILD_NUMBER" for those who are wondering. I didn't test doing so though I am sure it would work.

Either:

echo env.BUILD_NUMBER

or

echo "${env.BUILD_NUMBER}"

(Not sure whether Groovy gives higher precedence to `$` or `.` so play it safe and use braces if inside a GString.)

BTW as of the 1.5 release, the live documentation (i.e., Snippet Generator) for environment variable handling has been consolidated into the help for the new withEnv step, which seemed the most appropriate place for it.

Matt Evans

unread,
May 24, 2016, 6:34:53 PM5/24/16
to Jenkins Users
I have tried many ways to bring variable in scope to a function, can you please clarify how the following code should be written to bring the variable "BRANCH_TEST" in scope?

   def BRANCH_TEST = "master"
   
   node {
       deploy()
   }
   
   def deploy(){
       echo env.BRANCH_TEST
   }

Console Output:
[Pipeline] echo
null

Craig Rodrigues

unread,
May 24, 2016, 7:03:06 PM5/24/16
to Jenkins Users
It took me a long time to figure that out, and it was not obvious to me.

--
Craig


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fa738334-df6c-48a6-987f-cf4299200cc8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Evans

unread,
May 24, 2016, 7:56:57 PM5/24/16
to Jenkins Users, rod...@freebsd.org
Thanks @Craig, works perfect!

ludovic SMADJA

unread,
May 25, 2016, 9:33:28 AM5/25/16
to jenkins...@googlegroups.com
I've deal with the same problem.

The variable is available as BRANCH_TEST (and not env.BRANCH_TEST). If you need env variables, you must use WithEnv keyword.

regards,
--


Ludovic SMADJA
R&D engineer - build process and development
JALIOS SA
http://www.jalios.com

Reply all
Reply to author
Forward
0 new messages