Goto to the /job/<project>/configure screen. In "Build Environment" section check "Inject environment variables to the build process"
Environment variables can be defined using NAME = VALUE syntax. To access the variable value you can use these three methods $env.NAME , $NAME or ${NAME} There are no differences between these methods.
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.
You can use EnvInject plugin to injects environment variables at build startup. For example:
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
In my case, I had configure environment variables using the following option and it worked-
Manage Jenkins -> Configure System -> Global Properties -> Environment Variables -> Add
You can try something like this
stages {
stage('Build') {
environment {
AOEU= sh (returnStdout: true, script: 'echo aoeu').trim()
}
steps {
sh 'env'
sh 'echo $AOEU'
}
}
}
You can use Environment Injector Plugin to set environment variables in Jenkins at job and node levels. Below I will show how to set them at job level.
Manage Jenkins > Manage Plugins
and install the plugin.Configure
screenAdd build step
in Build
section and select Inject environment variables
If you need to define a new environment variable depending on some conditions (e.g. job parameters), then you can refer to this answer.
EnvInject Plugin aka (Environment Injector Plugin) gives you several options to set environment variables from Jenkins configuration.
By selecting Inject environment variables to the build process
you will get:
Properties File Path
Properties Content
Script File Path
Script Content
and finally Evaluated Groovy script
.
Evaluated Groovy script
gives you possibility to set environment variable based on result of executed command:
execute
method: return [HOSTNAME_SHELL: 'hostname'.execute().text,
DATE_SHELL: 'date'.execute().text,
ECHO_SHELL: 'echo hello world!'.execute().text
]
Groovy
code: return [HOSTNAME_GROOVY: java.net.InetAddress.getLocalHost().getHostName(),
DATE_GROOVY: new Date()
]
(More details about each method could be found in build-in help (?))
Unfortunately you can't do the same from Script Content
as it states:
Execute a script file aimed at setting an environment such as creating folders, copying files, and so on. Give the script file content. You can use the above properties variables. However, adding or overriding environment variables in the script doesn't have any impacts in the build job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With