JENKINS - Build In Variables

 Jenkins provides a variety of built-in environment variables that can be used in your Jenkins Pipelines to access information about the build environment, the build process, and the system. Here is a comprehensive list of the most commonly used built-in environment variables, along with their descriptions:

Common Built-in Jenkins Environment Variables

  1. BUILD_ID

    • Description: A unique identifier for the current build. It is the same as BUILD_NUMBER.
    • Example: 2024
  2. BUILD_NUMBER

    • Description: The current build number, which is an incrementing integer starting from 1.
    • Example: 42
  3. BUILD_DISPLAY_NAME

    • Description: The display name of the current build, often including the build number.
    • Example: #42
  4. BUILD_TAG

    • Description: A unique identifier for the current build in the format job_name_build_number.
    • Example: jenkins-MyJob-42
  5. BUILD_URL

    • Description: The URL to the build's web page.
    • Example: http://localhost:8080/job/MyJob/42/
  6. JOB_NAME

    • Description: The name of the job being executed.
    • Example: MyJob
  7. JOB_BASE_NAME

    • Description: The base name of the job, without the folder path.
    • Example: MyJob
  8. JOB_URL

    • Description: The URL to the job's web page.
    • Example: http://localhost:8080/job/MyJob/
  9. WORKSPACE

    • Description: The absolute path of the workspace directory assigned to the build.
    • Example: /var/lib/jenkins/workspace/MyJob
  10. NODE_NAME

    • Description: The name of the node (agent) on which the current build is running. For the master node, this is "master".
    • Example: slave1
  11. NODE_LABELS

    • Description: A space-separated list of labels assigned to the node.
    • Example: linux docker
  12. EXECUTOR_NUMBER

    • Description: The unique number that identifies the current executor.
    • Example: 0
  13. JENKINS_HOME

    • Description: The path to the Jenkins home directory.
    • Example: /var/lib/jenkins
  14. JENKINS_URL

    • Description: The URL to the Jenkins instance.
    • Example: http://localhost:8080/
  15. JENKINS_SERVER_COOKIE

    • Description: A unique identifier for the Jenkins server, used to avoid cross-server CSRF issues.
    • Example: b83a3cfc
  16. GIT_COMMIT

    • Description: The commit hash of the current Git revision.
    • Example: a1b2c3d4
  17. GIT_BRANCH

    • Description: The name of the Git branch currently being built.
    • Example: main
  18. GIT_URL

    • Description: The URL of the Git repository.
    • Example: https://github.com/example/repo.git
  19. SVN_REVISION

    • Description: The Subversion revision number of the build.
    • Example: 1234
  20. SVN_URL

    • Description: The URL of the Subversion repository.
    • Example: http://svn.example.com/repo
  21. BRANCH_NAME

    • Description: The name of the branch being built (often used in Multibranch Pipeline jobs).
    • Example: feature-branch

Example of Using Environment Variables in a Jenkins Declarative Pipeline

Here is an example Jenkins Declarative Pipeline that prints out some of these environment variables:


pipeline {

    agent any


    stages {

        stage('Print Environment Variables') {

            steps {

                script {

                    echo "BUILD_ID: ${env.BUILD_ID}"

                    echo "BUILD_NUMBER: ${env.BUILD_NUMBER}"

                    echo "JOB_NAME: ${env.JOB_NAME}"

                    echo "WORKSPACE: ${env.WORKSPACE}"

                    echo "NODE_NAME: ${env.NODE_NAME}"

                    echo "JENKINS_URL: ${env.JENKINS_URL}"

                    echo "GIT_COMMIT: ${env.GIT_COMMIT}"

                    echo "GIT_BRANCH: ${env.GIT_BRANCH}"

                    echo "BRANCH_NAME: ${env.BRANCH_NAME}"

                }

            }

        }

    }

}


Comments