Using GIT variables in a declarative Jenkins pipeline

Finally I found an example and I was able to understand how to do this.

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

stage('Checkout code') {
  steps {
    script {
      // Checkout the repository and save the resulting metadata
      def scmVars = checkout([
        $class: 'GitSCM',

      // Display the variable using scmVars
      echo "scmVars.GIT_COMMIT"
      echo "${scmVars.GIT_COMMIT}"

      // Displaying the variables saving it as environment variable
      env.GIT_COMMIT = scmVars.GIT_COMMIT
      echo "env.GIT_COMMIT"
      echo "${env.GIT_COMMIT}"

    // Here the metadata is available as environment variable

Leave a Comment