I have been using the CloudBees Jenkins Enterprise plugin to create many helpful Builder templates for use as build steps in different build jobs. We also use the Environment Injector plugin to configure our build environment. Please can you help us to access the environment variables injected by the Environment Injector plugin (or any environment variables for that matter) from within our Groovy Script.
JD, Tennessee, USA
I'm glad to hear you are getting great use out of our plugins. I can certainly see how the accessing of the environment variables that are visible to the build could be confusing. For one thing everybody is overloading $ as the symbol to indicate that something needs to be evaluated. And to further confuse things even more, the actual Shell/Batch script gets to use $ for evaluating: the environment variables; the builder attributes; and any variables defined by the Groovy Script.
The first thing that a smart person might try and do is just get the environment from the Java Runtime, e.g.
<img alt="Yuck-face" src="http://lh6.ggpht.com/-Qvg5Uc5iEnU/UelDs0gJ3eI/AAAAAAAAAWY/LPP8DKVfCMU/face-yuck.png?imgmax=800" style="width: 48px; height: 48px; border-width: 1px; border-style: solid;"> def env = System.getenv()
But (as my Yuck-faced Emoticon hints) that will not work. The reason why this does not work is that each build has its own environment which must be distinct from the environment that the Jenkins controller process runs in, which is what the Java Runtime returns.
So we need to get the environment of the build...
<img alt="Face smile" src="http://lh4.ggpht.com/-fIHN5hHhXEE/UelDtX_1C-I/AAAAAAAAAWg/lw92J4t_J2c/face-smile.png?imgmax=800" style="width: 48px; height: 48px; border-width: 1px; border-style: solid;"> def env = build.environment
That is much better. That returns a Map of the environment current environment variables available to the build.
I hope this answered your question.
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.