Use Jenkins environment variables in Pipeline on MacOS

Mayank Jain
3 min readMar 17, 2024

--

Hello Folks, In this blog we will learn how to use environment variables in Jenkins pipeline.

In Jenkins, environment variables are essentially named settings that store specific values. These variables can be used throughout your Jenkins jobs and pipelines, allowing you to configure your builds and workflows without hardcoding values directly in your scripts. This makes your Jenkins configuration more flexible and reusable.

Use Cases of Jenkins environment Variable:

Improved Maintainability: Instead of repeating the same values throughout your Jenkinsfile or job configurations, you can define them as environment variables. This makes it easier to update a value in one place and have it reflected everywhere it’s used.

Enhanced Security: Sensitive information, like passwords or API keys, can be stored as credentials within Jenkins and referenced through environment variables. This keeps your sensitive data out of your code and build scripts, improving security.

A key benefit of using environment variables is their ability to customize build steps based on the environment or specific conditions. For instance, imagine you have different configurations for development (dev), testing (test), and production (prod) environments. Here’s how environment variables can help:

Environment-Specific Configurations: Define environment variables like BUILD_ENV (set to "dev", "test", or "prod") depending on the Jenkins job configuration.

Conditional Build Steps: Within your Jenkinsfile or job configuration, use conditional statements based on the environment variable value. For example:

if (env.BUILD_ENV == 'dev') {
// Use development database connection string
} else if (env.BUILD_ENV == 'test') {
// Use testing database connection string
} else {
// Use production database connection string
}

By leveraging environment variables, you can automate adjustments to build steps based on the environment, making your Jenkins pipelines more adaptable and streamlined.

You can see all the environment list by opening below the link:

~ http://localhost:8080/env-vars.html/

Note: Make sure your Jenkins service is up and running before opening the above link.

Now let check how we can use the environment variable in our pipeline. Add the below code in your pipeline script.

pipeline{
agent any

stages{
stage('firstStage'){
steps{
echo "Hello Jenkins Variables"
}
}
stage('EnvironmentVariables'){
steps{
echo "BUILD ID: "+ env.BUILD_ID
echo env.JOB_NAME
echo env.JENKINS_HOME
}
}
}
}

The above script will have 2 stages, the first stage name firststage will just print the Hello Jenkins Variables and the second stage name EnvironmentVariables will print the Build ID, Job Name and Home path of Jenkins. Apply and Save the job.

Now run the Jenkins job and check the console output. You will able to see the similar output printed in the screen as show in below screenshot.

Congratulation you just learned how to use Jenkins environment variables in jenkins pipeline.

Learn how to execute shell script within Jenkins Pipeline on MacOS here.

Learn how to install and configure Jenkins on MacOS here.

Learn how to set up the Puppet Master and Agent in a virtual box environment here.

Learn RHEL8 custom AMI creation using RHEL Image Builder here.

Learn how to remove the old Linux kernel from RHEL8/CentOS8 here.

Learn how to log commands of all users runs in Linux here.

--

--

Mayank Jain

Skilled DevOps Engineer with 7.5+ years of hands-on experience supporting, automating, & optimizing mission critical deployments in cloud.