Environment variables are a powerful feature in Jenkins pipelines that allow you to manage and pass configuration data dynamically. They can be used to store sensitive information, such as API keys, or to configure different environments (e.g., development, staging, production).
Key Concepts
- Environment Variables: Variables that are set in the environment in which Jenkins jobs run. They can be used to pass data between different stages of a pipeline.
- Global Environment Variables: Variables that are available to all jobs and pipelines within Jenkins.
- Job-Specific Environment Variables: Variables that are specific to a particular job or pipeline.
- Built-in Environment Variables: Predefined variables provided by Jenkins, such as
BUILD_NUMBER
,JOB_NAME
, etc.
Setting Environment Variables
Global Environment Variables
Global environment variables can be set in the Jenkins configuration:
- Navigate to
Manage Jenkins
>Configure System
. - Scroll down to the
Global properties
section. - Check the
Environment variables
checkbox. - Add the desired environment variables.
Job-Specific Environment Variables
Job-specific environment variables can be set within a pipeline script using the environment
directive.
pipeline { agent any environment { MY_VAR = 'Hello, World!' API_KEY = credentials('my-api-key') } stages { stage('Example') { steps { echo "MY_VAR is ${env.MY_VAR}" echo "API_KEY is ${env.API_KEY}" } } } }
Built-in Environment Variables
Jenkins provides several built-in environment variables that can be used in your pipelines:
Variable | Description |
---|---|
BUILD_NUMBER |
The current build number |
JOB_NAME |
The name of the job |
WORKSPACE |
The workspace directory for the job |
BUILD_URL |
The URL of the current build |
GIT_COMMIT |
The commit hash of the current Git revision |
Practical Example
Let's create a simple pipeline that demonstrates the use of environment variables.
pipeline { agent any environment { GREETING = 'Hello' NAME = 'Jenkins' } stages { stage('Print Greeting') { steps { script { def message = "${env.GREETING}, ${env.NAME}!" echo message } } } stage('Use Built-in Variables') { steps { echo "This is build number ${env.BUILD_NUMBER} of job ${env.JOB_NAME}" } } } }
Explanation
- Environment Directive: Sets the
GREETING
andNAME
variables. - Print Greeting Stage: Constructs a message using the environment variables and prints it.
- Use Built-in Variables Stage: Prints the build number and job name using built-in environment variables.
Practical Exercises
Exercise 1: Setting and Using Environment Variables
- Create a new pipeline job in Jenkins.
- Use the following pipeline script:
pipeline { agent any environment { ENVIRONMENT = 'development' VERSION = '1.0.0' } stages { stage('Print Environment') { steps { echo "Running in ${env.ENVIRONMENT} environment, version ${env.VERSION}" } } } }
- Run the job and observe the output.
Solution:
The output should display the environment and version as set in the environment
directive.
Exercise 2: Using Credentials as Environment Variables
- Add a new secret text credential in Jenkins with the ID
my-secret
. - Create a new pipeline job with the following script:
pipeline { agent any environment { SECRET = credentials('my-secret') } stages { stage('Print Secret') { steps { echo "The secret is ${env.SECRET}" } } } }
- Run the job and observe the output.
Solution:
The output should display the secret value stored in the Jenkins credentials.
Common Mistakes and Tips
-
Mistake: Forgetting to use the
env
prefix when accessing environment variables.- Tip: Always use
env.VARIABLE_NAME
to access environment variables in pipeline scripts.
- Tip: Always use
-
Mistake: Hardcoding sensitive information in the pipeline script.
- Tip: Use Jenkins credentials to securely manage sensitive information.
Conclusion
In this section, we covered the basics of using environment variables in Jenkins pipelines. We learned how to set global and job-specific environment variables, use built-in environment variables, and securely manage sensitive information using Jenkins credentials. Understanding and effectively using environment variables is crucial for creating flexible and maintainable Jenkins pipelines. In the next section, we will explore pipeline best practices to further enhance your Jenkins skills.
Jenkins: From Beginner to Advanced
Module 1: Introduction to Jenkins
Module 2: Jenkins Basics
- Jenkins Dashboard Overview
- Creating and Running Jobs
- Understanding Jenkins Pipelines
- Using Jenkins Plugins
Module 3: Jenkins Pipelines
Module 4: Advanced Jenkins Pipelines
- Pipeline Stages and Steps
- Parallel Execution in Pipelines
- Using Environment Variables
- Pipeline Best Practices
Module 5: Jenkins Administration
Module 6: Integrating Jenkins
- Integrating with Version Control Systems
- Integrating with Build Tools
- Integrating with Testing Tools
- Integrating with Deployment Tools