17

I am trying to assign the git commit hash to a variable defines in Jenkins pipeline as follows

GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"

This will print the commit hash in Jenkins build log but it fails to assign the value.

When I try to print the value using

steps{
    script {
                GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"

                echo "**************************************************"
                echo "${GIT_COMMIT_HASH}"
                echo "**************************************************"
    }
}

This will results null

How may I assign the value ?

not 0x12
  • 19,360
  • 22
  • 67
  • 133

5 Answers5

23

You have to tell the sh script to return stdout back to your script, rather than just dumping it to stdout.

GIT_COMMIT_HASH = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
Rob Hales
  • 5,123
  • 1
  • 21
  • 33
  • 1
    Its not gonna work Im getting an error sh: Syntax error: word unexpected (expecting ")") – not 0x12 Sep 21 '17 at 00:43
  • You must have something typed incorrectly. I'm guessing you missed a comma. – Rob Hales Sep 21 '17 at 01:19
  • 1
    Oops. Forgot the label for the first variable. I think it is "script". Fixed above. – Rob Hales Sep 21 '17 at 02:02
  • 2
    I just validated this with some of my scripts. This is the correct syntax now. Sorry, I should be more careful with my copy/paste editing. – Rob Hales Sep 21 '17 at 04:40
  • copy-pasting this into osx's bash is a no-go – ofloveandhate Jan 30 '18 at 19:43
  • @ofloveandhate this is for a linux server – not 0x12 Feb 13 '18 at 02:36
  • 7
    `git rev-parse HEAD` is a faster way of doing this – Rossiar Dec 10 '19 at 12:55
  • @ofloveandhate This is something you would put in your `Jenkinsfile`, not paste at the shell prompt. The shell command is the fragment `git log -n 1 --pretty=format:'%H'` which will work fine in Bash if you have `git` installed. As suggested in the previous comment, you could replace it with a faster command, too. – tripleee Jul 29 '21 at 08:54
5

Under https://<your_jenkins_hostname>/env-vars.html/ there is a page showing the list of the env-variables available in each build.

They might depend on your plugins, but if you are using git in your pipeline then you probably have the right one to have the GIT_COMMIT env variable available.

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
3

You can define a helper function and then call it within your pipeline

def getCommitSha() {
  return sh(returnStdout: true, script: 'git rev-parse HEAD')
}

// to an env var
env.GIT_COMMIT_HASH = getCommitSha()

// to a var within the pipeline
GIT_COMMIT_HASH = getCommitSha()
0

You may use this inside the environment block

GITCOMMIT="${sh(returnStdout: true, script: 'git rev-parse HEAD')}"

Nitin G
  • 714
  • 7
  • 31
0

Although the GIT_COMMIT variable is present in my https://%3Cyour_jenkins_hostname%3E/env-vars.html/ as suggested by @GregDubicki is his answer, actually the variable was not present during pipeline execution. Maybe this is because I'm using a manually configured checkout step.

So I had to modify that step a bit to extract the Git commit hash. Like this:

stage('Checkout SCM') {
    steps {
        script {
            // Here we save the result of the `checkout` operation to a local variable
            def scmVars = checkout([$class: 'GitSCM',
                      branches: scm.branches,
                      doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                      // These are my custom options, for which I had to use the manual checkout step. These might not be necessary for your case.
                      extensions: scm.extensions + [
                              [$class: 'LocalBranch'],
                              [$class: 'CloneOption', noTags: false],
                              pruneTags(true),
                      ],
                      submoduleCfg: scm.submoduleCfg,
                      userRemoteConfigs: scm.userRemoteConfigs
            ])
            // Here we save the Git commit hash into an environment variable
            env.GIT_COMMIT = scmVars.GIT_COMMIT
        }
    }
}

Now in later steps the GIT_COMMIT environment variable can be used as usual, for example:

stage('Build') {
    steps {
        echo "Building on git commit = ${GIT_COMMIT}"
    }
}

Inspired by this answer.

vadipp
  • 877
  • 1
  • 12
  • 22