Q:

How to set environment variables in Jenkins?

I would like to be able to do something like:
AOEU=$(echo aoeu)

and have Jenkins set AOEU=aoeu.
The Environment Variables section in Jenkins doesn't do that. Instead, it sets AOEU='$(echo aoeu)'.
How can I get Jenkins to evaluate a shell command and assign the output to an environment variable?
Eventually, I want to be able to assign the executor of a job to an environment variable that can be passed into or used by other scripts.

A:

This can be done via EnvInject plugin in the following way:

Create an "Execute shell" build step that runs:
echo AOEU=$(echo aoeu) > propsfile

Create an Inject environment variables build step and set "Properties File Path" to propsfile.

Note: This plugin is (mostly) not compatible with the Pipeline plugin.

A:

The simplest way
You can use EnvInject plugin to injects environment variables at build startup. For example:

How you know it's working

A:

In my case, I needed to add the JMETER_HOME environment variable to be available via my Ant build scripts across all projects on my Jenkins server (Linux), in a way that would not interfere with my local build environment (Windows and Mac) in the build.xml script. Setting the environment variable via Manage Jenkins - Configure System - Global properties was the easiest and least intrusive way to accomplish this. No plug-ins are necessary.

The environment variable is then available in Ant via:
<property environment="env" />
<property name="jmeter.home" value="${env.JMETER_HOME}" />

This can be verified to works by adding:
<echo message="JMeter Home: ${jmeter.home}"/>

Which produces:

JMeter Home: ~/.jmeter

