# How to extract git commit hash from Pipeline project?

**URL:** <https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970>\
**Category:** Ask a question\
**Created:** [December 15, 2022, 2:23pm UTC](https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970 "2022-12-15T14:23:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AxlInvk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/axlinvk/32/582_2.png) [@AxlInvk](https://community.jenkins.io/u/AxlInvk)\
**Post date:** [December 15, 2022, 2:23pm UTC](https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970/1 "2022-12-15T14:23:01Z")

</div>

Specs:

- Windows 10 PC as a controller 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!

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [December 15, 2022, 5:29pm UTC](https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970/2 "2022-12-15T17:29:04Z")

</div>

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

```auto
def scmVars = checkout([$class: 'GitSCM', branches: [[name: 'master']], 
    userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]])
env.GIT_COMMIT = scmVars.GIT_COMMIT
env.GIT_BRANCH = scmVars.GIT_BRANCH

```

I copied the original of that example from

> <https://stackoverflow.com/questions/36304208/jenkins-workflow-checkout-accessing-branch-name-and-git-commit>

---

<div class="post-metadata">

**Author:** ![AxlInvk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/axlinvk/32/582_2.png) [@AxlInvk](https://community.jenkins.io/u/AxlInvk)\
**Post date:** [December 20, 2022, 1:30pm UTC](https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970/3 "2022-12-20T13:30:51Z")

</div>

Thank you so much for the explaination!

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

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [December 20, 2022, 2:32pm UTC](https://community.jenkins.io/t/how-to-extract-git-commit-hash-from-pipeline-project/4970/4 "2022-12-20T14:32:31Z")

</div>

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.
