Using Predefined Environment Variables
Jenkins provides a set of predefined environment variables that are available to every build. These variables provide information about the current build, job, and node, which can be useful for scripting and logic within your pipeline.
Jenkinsfile¶
Here is a Jenkinsfile that demonstrates how to access and print some of the common predefined environment variables.
pipeline {
agent any
options {
disableConcurrentBuilds()
disableResume()
buildDiscarder(logRotator(numToKeepStr: '10'))
timeout(time: 1, unit: 'HOURS')
}
stages {
stage ('Jenkins Predefined Environment Variables') {
steps {
sh """
echo JOB_NAME : ${JOB_NAME}
echo JOB_BASE_NAME : ${JOB_BASE_NAME}
echo BUILD_NUMBER : ${BUILD_NUMBER}
echo WORKSPACE : ${WORKSPACE}
echo JENKINS_HOME : ${JENKINS_HOME}
echo JENKINS_URL : ${JENKINS_URL}
echo BUILD_URL : ${BUILD_URL}
echo JOB_URL : ${JOB_URL}
echo NODE_NAME : ${NODE_NAME}
"""
}
}
}
post {
always {
deleteDir()
}
}
}
Explanation¶
The pipeline uses a shell script block (sh) with triple double-quotes (""") to allow multi-line strings and variable interpolation.
Common Variables¶
JOB_NAME: Name of the project of this build, such as "foo" or "foo/bar".JOB_BASE_NAME: Short name of the project of this build stripping off folder paths, such as "bar" for "foo/bar".BUILD_NUMBER: The current build number, such as "153".WORKSPACE: The absolute path of the directory where Jenkins has checked out the source code.JENKINS_HOME: The absolute path of the directory on the master node for Jenkins to store data.JENKINS_URL: Full URL of Jenkins, likehttp://server:port/jenkins/.BUILD_URL: Full URL of this build, likehttp://server:port/jenkins/job/foo/15/.JOB_URL: Full URL of this job, likehttp://server:port/jenkins/job/foo/.NODE_NAME: Name of the node the build is running on. Set to "master" for the Jenkins controller.
These variables are extremely useful for tagging Docker images, creating unique artifact names, notification scripts, and conditional logic.
Reference¶
๐ง Quick Quiz โ Build Information¶
Which environment variable would you use to get the unique number assigned to the current execution of the pipeline?
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here