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.
For some reason
sudo su - jenkins
does not log me tojenkins
user, I ended up using different approach.I was successful setting the global env variables using using jenkins
config.xml
at/var/lib/jenkins/config.xml
(installed in Linux/ RHEL) - without using external plugins.I simply had to stop jenkins add then add
globalNodeProperties
, and then restart.Example, I'm defining variables
APPLICATION_ENVIRONMENT
andSPRING_PROFILES_ACTIVE
tocontinious_integration
below,Normally you can configure Environment variables in Global properties in Configure System.
However for dynamic variables with shell substitution, you may want to create a script file in Jenkins HOME dir and execute it during the build. The SSH access is required. For example.
sudo su - jenkins
orsudo su - jenkins -s /bin/bash
Create a shell script, e.g.:
In Jenkins Build (Execute shell), invoke the script and its variables before anything else, e.g.
There is Build Env Propagator Plugin which lets you add new build environment variables, e.g.
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 thebuild.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:
This can be verified to works by adding:
Which produces:
Try Environment Script Plugin (GitHub) which is very similar to EnvInject. It allows you to run a script before the build (after SCM checkout) that generates environment variables for it. E.g.
and in your script, you can print e.g.
FOO=bar
to the standard output to set that variable.Example to append to an existing
PATH
-style variable:So you're free to do whatever you need in the script - either
cat
a file, or run a script in some other language from your project's source tree, etc.You can use Environment Injector Plugin to set environment variables in Jenkins at job and node levels. Below I will show how to do it at job level.
Manage Jenkins > Manage Plugins
and install the plugin.Configure
screenAdd build step
inBuild
section and selectInject environment variables
If you need to define a new environment variable depending on some conditions (e.g. job parameters) you can refer to this answer.