1. checkout: General SCM

This is a special step that allows to run checkouts using any configuration options offered by any Pipeline-compatible SCM plugin. To use a concrete SCM implementations, just install the corresponding plugin and check if it is shown in the list below. Then select the SCM to use from the dropdown list and configure it as needed.

This step returns a Map of any variables the SCM plugin would set in a Freestyle job, so if your SCM is git, you can do:

    def scmVars = checkout scm
    def commitHash = scmVars.GIT_COMMIT

    // or

    def commitHash = checkout(scm).GIT_COMMIT

2. git environment

Here are the eleven Jenkins Git environment variables:

GIT_COMMIT – a reference to the current Git commit’s secure hash algorithm (SHA) GIT_COMMITTER_NAME or GIT_AUTHOR_NAME – the name used when new Git commits are issued GIT_COMMITTER_EMAIL or GIT_AUTHOR_EMAIL – the email address used when new Git commits are issued GIT_COMMITTER_DATE or GIT_AUTHOR_DATE is the timestamp used for the “author” or “committer” field. GIT_URL – the base name of the remote GIT repository GIT_URL_N – if you are working with more than one remote Git repository (i.e n number of Git repositories) , this will list them all numerically GIT_BRANCH – the name of the current Git branch the Jenkins Git plugin is operating upon GIT_LOCAL_BRANCH – the name of the local Git branch when the “checkout to specific local branch” Jenkins Git plugin option is selected GIT_PREVIOUS_COMMIT – the id of the previous commit on the current branch GIT_PREVIOUS_SUCCESSFUL_COMMIT – this variable will output the hash of the commit of the last successful build

3. example

I need to use a script command, obtain the Map returned by checkout and save it in an environment variable:

stage('Checkout code') {
  steps {
    script {
      def scmVars = checkout([
        $class: 'GitSCM',
        ...
      ])

      echo "scmVars.GIT_COMMIT"
      echo "${scmVars.GIT_COMMIT}"

      env.GIT_COMMIT = scmVars.GIT_COMMIT
      echo "env.GIT_COMMIT"
      echo "${env.GIT_COMMIT}"
    }
  }
}