Managing Environment Variables
1. Defining Global Environment Variables
| Variable Name | Value | Description |
|---|---|---|
| JENKINS_HOME | /var/lib/jenkins |
Root directory of Jenkins installation |
| JAVA_HOME | /usr/lib/jvm/java-11-openjdk |
Path to Java installation used by Jenkins |
| PATH | $PATH:/usr/local/bin |
Updated system PATH to include additional binaries |
Example: Setting Global Environment Variables via Jenkins UI
Manage Jenkins → Configure System → Global Tool Configuration
Note: These variables are available to all jobs and pipelines in the Jenkins instance.
2. Defining Pipeline Environment Variables
| Property | Values | Description |
|---|---|---|
| environment | key: value |
Defines environment variables for the pipeline or stage |
| environment | key: value |
Values can be literals or environment variable references |
Example: Setting Environment Variables in Pipeline
pipeline {
agent any
environment {
DB_URL = 'jdbc:postgresql://localhost:5432/mydb'
DB_USER = 'admin'
DB_PASSWORD = credentials('db-password')
}
stages {
stage('Build') {
steps {
sh 'echo "Database URL: ${DB_URL}"'
}
}
}
}
Example: Using Environment Variables in Script
pipeline {
agent any
environment {
NODE_VERSION = '18.17.0'
NPM_REGISTRY = 'https://registry.npmjs.org'
}
stages {
stage('Setup') {
steps {
sh "echo 'Using Node version: ${NODE_VERSION}'"
sh "npm config set registry ${NPM_REGISTRY}"
}
}
}
}
3. Defining Stage-Level Variables
| Property | Values | Description |
|---|---|---|
| stage.env | key: value |
Defines environment variables for the stage scope |
| stage.env.VAR_NAME | value |
Single environment variable in stage scope |
Example: Setting Stage-Level Variables
pipeline {
agent any
stages {
stage('Build') {
environment {
VAR1 = 'value1'
VAR2 = 'value2'
}
steps {
sh 'echo $VAR1'
}
}
}
}
Note: Stage-level variables override any global or job-level variables with the same name.
4. Using Built-In Variables
| Variable | Description | Example Value |
|---|---|---|
| BUILD_NUMBER | Unique number for each build | 123 |
| BUILD_ID | Build ID (timestamp + build number) | 2024-05-20_12:34:56 |
| JOB_NAME | Name of the job that triggered the build | my-project-build |
| WORKSPACE | Workspace directory for the build | /var/lib/jenkins/workspace/my-project-build |
| JENKINS_URL | URL of the Jenkins server | http://localhost:8080/jenkins/ |
| NODE_NAME | Name of the node where the build runs | master |
| EXECUTOR_NUMBER | Number of the executor running this build | 0 |
Example: Using Built-In Variables in a Script
echo "Build #${BUILD_NUMBER} of ${JOB_NAME} started at ${BUILD_ID}"
sh "echo Workspace: ${WORKSPACE}"
5. Accessing Git Variables
| Variable | Description | Example Value |
|---|---|---|
| GIT_URL | URL of the Git repository | https://github.com/example/repo.git |
| GIT_COMMIT | Full commit hash | a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 |
| GIT_BRANCH | Current branch name | main |
| GIT_TAG | Tag associated with the commit (if any) | v1.0.0 |
Example: Using Git Variables in a Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building branch: ${env.GIT_BRANCH}"
echo "Commit hash: ${env.GIT_COMMIT}"
}
}
}
}
6. Using Credentials as Variables
| Property | Value | Description |
|---|---|---|
| credentials.id | my-credential-id |
ID of the credential defined in Jenkins |
| credentials.username | admin |
Username for the credential |
| credentials.password | secret123 |
Password for the credential |
Example: Using Credentials in a Pipeline Script
pipeline {
agent any
environment {
DB_USER = credentials('my-credential-id')
}
stages {
stage('Example') {
steps {
sh 'echo "User is: $DB_USER"'
}
}
}
}
Note: Ensure that the credential ID matches exactly with the one configured in Jenkins Credentials Store.
Warning: Never hardcode credentials in pipeline scripts. Always use Jenkins' built-in credential management.
7. Setting Dynamic Variables
| Variable Type | Description | Usage Example | Notes |
|---|---|---|---|
| Environment Variable | Dynamic variable set at runtime, accessible via env.VAR_NAME |
def dynamicVar = env.DYNAMIC_VAR |
Can be set using environment block in Jenkinsfile |
| Build Parameter | Variable defined during build configuration, can be modified at runtime | def paramValue = params.MY_PARAM |
Use parameters block to define build parameters |
| Pipeline Variable | Variable defined within pipeline script scope, not persisted across builds | def pipelineVar = "value" |
Useful for temporary values within a single pipeline execution |
Example: Setting Dynamic Environment Variables in Jenkinsfile
pipeline {
agent any
environment {
DYNAMIC_VAR = "dynamic_value"
TIMESTAMP = "${new Date().format('yyyy-MM-dd HH:mm:ss')}"
}
stages {
stage('Print Variables') {
steps {
echo "Dynamic Variable: ${env.DYNAMIC_VAR}"
echo "Timestamp: ${env.TIMESTAMP}"
}
}
}
}
Note: Dynamic variables can be overridden by user input or external systems during pipeline execution.
8. Using Environment Block
| Property | Description | Example |
|---|---|---|
| environment | Defines environment variables for the build | |
Example: Setting Environment Variables
pipeline {
agent any
environment {
NODE_ENV = 'production'
DATABASE_URL = 'postgres://localhost:5432/mydb'
API_KEY = credentials('api-key')
}
stages {
stage('Build') {
steps {
sh 'echo $NODE_ENV'
sh 'echo $DATABASE_URL'
}
}
}
}
Note: Environment variables set in the
environment block are available to all steps in the pipeline and can be overridden by inline declarations.
Warning: Variables defined in the
environment block are not accessible in the script block unless explicitly exported.
9. Overriding Environment Variables
| Variable | Override Method | Notes |
|---|---|---|
| JENKINS_URL | System Property | Set via -Djenkins.url=http://localhost:8080 |
| JAVA_HOME | Environment Variable | Must point to valid JDK installation path |
| JENKINS_HOME | Environment Variable | Defines Jenkins configuration directory |
Example: Setting Environment Variables in Pipeline
pipeline {
environment {
MY_VAR = 'value'
ANOTHER_VAR = 'another_value'
}
stages {
stage('Example') {
steps {
sh 'echo $MY_VAR'
}
}
}
}
Note: Pipeline environment variables override system and job-level settings.
10. Using EnvInject Plugin
| Property | Value | Description |
|---|---|---|
| env.inject.envs | KEY1=value1,KEY2=value2 |
Environment variables to inject into the build process |
| env.inject.file | env.properties |
Path to a properties file containing environment variables |
| env.inject.script | echo $HOME |
Shell script to generate environment variables dynamically |
Note: The EnvInject plugin allows dynamic injection of environment variables at build time, useful for CI/CD workflows where variable values depend on runtime conditions.