Open In App

How To Set Environment Variables In Jenkins ?

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jenkins is a widely used open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) processes. One powerful feature of Jenkins is its capability to configure environment variables, which are important for various functions like defining parameters, paths, and settings required by numerous jobs and build processes. In this article, we are going to discuss how we can set environment variables in Jenkins to enhance the efficiency of your automation workflows.

What are environmental variables?

Environment variables are key-value pairs that contain information about the system environment. In Jenkins, those variables may be configured globally or at the job level, providing us with a way to customize and parameterize our builds.

Here are some key elements of environmental variables that are described below in detail:

1. Naming and Structure

  • Environment variables consist of a name and a value, generally known as a key-value pair.
  • The name is mostly in uppercase, and it is conventionally written in uppercase letters with underscores separating every word (for example, PATH, JAVA_HOME).
  • The value is the data associated with the variable, which can be a string, a numeric value, or a more complex data type.

2. Scope

Environment variables can have different kinds of scopes:

  • Global: As the name suggests, they are available globally, affecting all processes and users.
  • User-specific: They are limited to a particular user environment.
  • Process-Specific: Valid only for the duration of a particular process.

3. Common Environment Variables:

  • PATH: The path specifies directories where the operating system needs to look for executable files.
  • HOME or USERPROFILE: It represents the user’s home directory.
  • JAVA_HOME: It points towards the installation directory of the Java Development Kit (JDK).
  • TMP, or TEMP: Specifies the temporary directory for storing files.

4. Accessing Environment Variables:

In maximum programming languages, gaining access to environment variables is straightforward Process.

Bash (Unix/Linux): In unix/linux it can accessed through this command:

echo $VARIABLE_NAME

PowerShell (Windows): In Windows it can accessed through this command:

$env:VARIABLE_NAME

Python: In Python it can accessed through this command:

os.Environ['VARIABLE_NAME']

JavaScript (Node.Js): In Javascript it can accessed through this command:

process.env.VARIABLE_NAME

Role of Environment Variable in Jenkins:

Environment variables play important roles in Jenkins via providing a flexible and dynamic manner to manage configurations, parameters, and settings across different stages of the continuous integration and continuous delivery of (CI/CD) pipeline. Here are numerous key roles that environment variables play in Jenkins:

  • Configuration Flexibility: Environment variables assist you to define configuration parameters that can be easily modified with out altering the underlying code or job configurations. This flexibility is critical for adapting to different environments, which includes development, testing, and production.
  • Parameterization: Jenkins environment variables allow parameterization of builds. By defining variables, you could create more flexible and reusable jobs that adapt to varying instances. This is in particular beneficial while deploying to different servers, systems, or when running jobs with different configurations.
  • Securing Sensitive Information: Jenkins presents a way to store sensitive data, including API keys, passwords, or acess tokens, securely using environment variables. This guarantees that sensitive data is not hardcoded in scripts or configuration documents, enhancing security and minimizing the risk from security threats.
  • Build Versioning: Environment variables may be used to manage build version numbers dynamically. By incorporating variables into versioning schemes, you may make sure consistency across builds and deployments. This is in particular essential for tracking changes and debugging in a CI/CD environment.

How to set environment variables in Jenkins?

Here is the step by step guide to set environment variable in jenkins. It involves various steps discussed in detailed as follow:

Step 1: Login to your Jenkins Dashboard

The first step is to login to your Jenkin dashboard by your User id and password

login-to-jenkin

Login to your Jenkins Dashboard

Step 2: Navigate to Manage Jenkins

In the Jenkins dashboard look for the Manage Jenkins option on the left side of the panel and click on it.

Click-on-Manage-Jenkins

Click on Manage Jenkins

Now, after that select the first option Configure System to configure global settings and path configuration.

configure-system

Click on the Configure System

After this you will redirected to configuration page, Just Scroll down to set Environment Variable in Jenkins.

configuration-page

Step 3: Configure Environment Variable:

When you will Scroll down you will see Global Properties section under which you have enable environment variable option by selecting the Checkbox.

enable-environment-variable

Enable Environment Variable

Step 4: Add Name and Value

Now, Pass the Name and Value (Path) accordingly as per the requirement and also it must be noted that you can add multiple environment variable, then click on apply button to save changes.

add-name-and-values

Add Names and Values

Step 5: Test Environment Variable

This is one of the most important steps to test whether the environment variable is configured properly or not. We can test it out by just choose configure option.

click-on-configure

Click on Configure

Now we are going to run a command, then click apply to save changes

echo $Variable Name 
execute-command

execute command

Now choose Build now from left side of the panel

click-on-build-now

click on build now

test-environment-variable

Test environment variable

Step 6: Console Output

You can see that we have successfully tested and set environment variable in jenkins.

console-output

Conclusion

Setting environment variables in Jenkins presents a powerful mechanism for customizing and parameterizing your builds. Whether you’re configuring global variables, job-specific variables, or the using plugins, leveraging environment variables increases the flexibility and maintainability of your automation workflows. By understanding these configurations will assist you optimize your Jenkins setup and streamline your CI/CD process.

How to set environment variables in Jenkins – FAQ’s

Are there any plugins for handling Environment variables in Jenkins?

Yes, Jenkins provides plugins like the EnvInject Plugin that permits you to inject environment variables into the build process. Install the plugin from “Manage Jenkins” > “Manage Plugins” and configure it within your job’s build steps.

Can environment variables be used for conditional execution in Jenkins?

Yes, environment variables can be used for conditional execution in Jenkins pipelines. By checking the values of particular variables, you can control the flow of the pipeline and execute different actions based on the environment.

How can I pass environment variables between Jenkins jobs?

Environment variables may be pass between Jenkins jobs via defining them globally or with the usage plugins like the Parameterized Trigger Plugin or the Copy Artifact Plugin. This allow communication and data sharing between different jobs within the same Jenkins instance.

How can I troubleshoot issues associated with Environment variables in Jenkins?

If you come across issues with Environment variables in Jenkins, take a look at the configuration settings for typos or errors. Review the Jenkins job logs for any messages associated with environment variables. Additionally, make sure that plugins and dependencies are successfully configured.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads