2

I'm struggling to access GIT variables in my Jenkins pipeline

I need to know what GIT branch it's been checked out inside some bash code in a stage of the pipeline. I will use this to create different output file names. My pipeline is declarative, not scripted, and I'm using Jenkins 2.150.1

I tried everything I could find online but it's mostly incomplete code or for scripted pipelines. Or simply I can't put the information together.

Give how much time I spent on this, it would be nice to have a full working example that uses a declarative pipeline.

This is what I tried so far:

#1

Run git inside sh, but Jenkins checks out a commit, not a branch, resulting in a detached head

#2

Looking for environment variables from the shell, but there's none set related to GIT. This snippet

steps {
  sh 'echo $GIT_BRANCH'
}

always returns empty. I then tried on Groovy:

steps {
  echo "${env.GIT_BRANCH}"
}

prints null.

#3

In the "Global Variable Reference" there is a bit that says:

SCM-specific variables such as GIT_COMMIT are not automatically defined as environment variables; rather you can use the return value of the checkout step.

I searched online how to do it and I put together this code:

pipeline {
  stages {
    stage('Build') {
      steps {
        def scmVars = checkout([...])
        echo 'scm : the commit id is ' + scmVars.GIT_COMMIT
      }
    }
  }
}

But it fails with an exception

#4

use an environment command and try to obtain that value somehow

#5

looking for variables at other levels in the Jenkinsfile, but apparently I can do that only on scripted pipelines

#5

Tried to access the build url and API call, but I'm behing a proxy and this complicates things with the URL.

My Jenkinsfile

pipeline {
  stages {
    stage('Build') {
      steps {
        checkout([
          $class: 'GitSCM',
          doGenerateSubmoduleConfigurations: false,
          userRemoteConfigs: [[
            url: '...',
            credentialsId: '...'
          ]],
          branches: [ [name: '*/master'] ]
        ])

        sh '''
        #!/bin/bash -x
        echo $MY_GIT_BRANCH_THAT_I_CANT_FIND
        '''
      }
    }
  }
}
4

I found out how to do it, finally finding an 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}"
    }
  }
}
1

Quoting the docs:

GIT_BRANCH

For Git-based projects, this variable contains the Git branch that was checked out for the build (normally origin/master)

Specifically for the Pipeline plugin, there's an answer to this problem on StackOverflow:

The env.BRANCH_NAME variable contains the branch name.

As of Pipeline Groovy Plugin 2.18, you can also just use BRANCH_NAME (env isn't required but still accepted.)

On some conditions, this variable may be empty, the following should fix this:

Add [$class: 'LocalBranch', localBranch: "**"] to “extentions” in your checkout step.

  • Tried to added the extensions, but didn't work. There are no GIT variables – ColOfAbRiX Jan 24 at 18:29
  • It works for me extensions: [[$class: 'LocalBranch', localBranch: "**"]], – M Faisal Hameed May 7 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.