Jenkins Template plugin, builder templates and environment variables

Stephen Connolly's picture

DEAR STEPHEN:

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

DEAR JD:
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.

Yuck-face​
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 master process runs in, which is what the Java Runtime returns.
So we need to get the environment of the build…​

Face smile​
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.​

Blog Categories: 

Add new comment