Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 19, 2024
In this tutorial, we’ll show the different ways to set and use environment variables in Jenkins.
To learn more about Jenkins and Pipelines, refer to our intro to Jenkins.
We can set global properties by navigating to “Manage Jenkins -> Configure System -> Global properties option”.
Let’s first check the “Environment variables” checkbox and then add the variables and their respective values inside the “List of Variables” section:
This is one of the easiest and least intrusive ways to set environment variables.
We can set environment variables globally by declaring them in the environment directive of our Jenkinsfile.
Let’s see how to set two variables, DISABLE_AUTH and DB_ENGINE:
Jenkinsfile (Declarative Pipeline)
pipeline {
//Setting the environment variables DISABLE_AUTH and DB_ENGINE
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'mysql'
}
}
This approach of defining the variables in the Jenkins file is useful for instructing the scripts; for example, a Make file.
We can install and use the EnvInject plugin to inject environment variables during the build startup.
In the build configuration window, we select the “Inject environment variables” option in the “Add build step” combo box.
We can then add the required environment variables in the properties content text box.
For example, we can specify the user profile:

Now, we can use any of our environment variables by surrounding the name in ${}:
echo "Database engine is ${DB_ENGINE}"
In this article, we saw how to set and use environment variables in Jenkins.