How to extract git commit hash from Pipeline project?


  • Windows 10 PC as a master device.
  • Jenkins LTS controller 2.375.1
  • Firefox browser.

Previously, on a Freestyle project, I was able to call %GIT_COMMIT% as an environment variable in a Windows batch script and it would display the commit hash.

Having moved to a Pipeline project, the GIT_COMMIT environment variable doesn’t currently exist, and the Pipeline syntax global variables documentation specifies:

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.

How would one go about this?

Looked this up several times and all the answers all seem very confusing, and this community is so helpful so thought I’d ask!

One code sample that creates the GIT_COMMIT env var based on the return value of checkout scm is

def scmVars = checkout([$class: 'GitSCM', branches: [[name: 'master']], 
    userRemoteConfigs: [[url: '']]])

I copied the original of that example from

Thank you so much for the explaination!

Where do I specify this code? Under the environment { } section?

You’ll need to use a script block to wrap the checkout since you can’t assign values in declarative pipeline outside the environment section.