When writing a script that is run by Jenkins Groovy Plugin as a build step (Execute System Groovy Script) one can specify 'variable bindings'. The helpline says: Define varibale bingings (in the properties file format). Spefified variables can be addressed from the script. [sic] How do I access those variables from the script? They are not set as environment variables for the build, neither are they present among System properties.
Asked
Active
Viewed 1.7k times
2 Answers
11
this.getBinding().getVariables()
or simply binding.variables

malenkiy_scot
- 16,415
- 6
- 64
- 87
-
Do you know how to access the environment variables in the script? – John Powel Nov 28 '12 at 16:07
-
@Newbie, `binding.varaibles` is a [java.util.Map](http://docs.oracle.com/javase/6/docs/api/java/util/Map.html) object (keyed by variable names). – malenkiy_scot Dec 23 '12 at 10:56
-
3If you have a binding like `foo=bar` in your variable bindings, you can get `bar` by running `binding.variables.get('foo')`. – The Unknown Dev Feb 10 '16 at 17:14
1
I wasn't able to use binding.variables directly, I only got listener
, build
, launcher
and out
from binding.variables.
Instead I was able to use build.environment(listener)
to retrieve the environment variables as suggested in the responses to this question:
Access to build environment variables from a groovy script in a Jenkins build step ( Windows)
def config = new HashMap()
config.putAll(binding.variables)
def logger = config['out']
def envvars = new HashMap()
envvars.putAll(build.getEnvironment(listener))
def myvar= envvars['myvar']
This might have been different for me since I am only looking for system-wide environmental variables:
(checked) Prepare an environment for the run \ Keep Jenkins Environment Variables \ Keep Jenkins Build Variables

Community
- 1
- 1

saranicole
- 2,093
- 1
- 23
- 23