1

I'm using Jenkins multibranch pipelines.

I want to get the Git hash before the pipeline even starts and wondering if the data is available in some system class or variable. I don't want to shell out to the git command nor have to wait till the checkout scm step is executed and then read properties of that object.

Perhaps this is impossible, but it does seem like I can get a list of changed files before the check step and pipeline directive with currentBuild.changeSets like this: Which Jenkins Command to Get the List of Changed Files.

What other secrets does currentBuild greedily hide from me? Does it have a method that could retrieve the current git hash of the branch- again before the checkout step the runs inside the pipeline?

Also, to add why I think this is possible, I see this console output whenever a multibranch job starts:

Started by user me
Obtained mydir/Jenkinsfile from <FULL GIT HASH OF COMMIT>
Running in Durability level: MAX_SURVIVABILITY
.....

So clearly Jenkins gets the hash of the commit immediately. I'm hoping it's possible to retrieve in code.

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392

1 Answers1

1

See http://<Your Jenkins>/env-vars.html/:

The following variables are available to shell scripts
...
GIT_COMMIT
The commit hash being checked out.
...

UPDATE

See also:

Environment Variable Description
GIT_COMMIT For Git-based projects, this variable contains the Git hash of the commit checked out for the build (like ce9a3c1404e8c91be604088670e93434c4253f03) (all the GIT_* variables require git plugin)

I'm using it in a declarative Jenkinsfile of a Multibranch Pipeline project:

        stage('Set build description') {
            steps {
                buildDescription "Commit: ${GIT_COMMIT}"
            }
        }

and the result is:

enter image description here

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • 2
    GIT_COMMIT has never existed for multibranch pipelines so not sure where you are seeing this is available or if you have some 3rd party plugin. If I dump the `env` var before the pipeline{} directive via `env.getEnvironment()` or even inside the pipeline directive with sh step doing `printenv` I see no GIT_* variables set. – red888 Aug 05 '21 at 13:17
  • @red888 As mentioned, I see this when opening the page `env-vars.html` on my Jenkins. See also the update to my answer. And I'm actually using this in a Multibranch Pipeline project's (declarative) Jenkinsfile. – Gerold Broser Aug 05 '21 at 13:37
  • Im sorry but those env vars do not exist for multibranch pipelines out of the box you must have a specific scm plugin or something. You can google this and find people trying to get the git hash like I am who are in the same boat – red888 Aug 05 '21 at 13:47
  • @red888 See the heading of the first paragraph of the linked article: _Jenkins Git plugin environment variables list_. – Gerold Broser Aug 05 '21 at 14:39
  • 1
    This only works if a stage has ran where a node was used and scm checkout happened. I have an 'init' stage with 'agent none' where this variable doesn't exist yet. Once I added an agent block for that init stage it worked (even though I don't have anything in that stage that requires an agent). Would be nice if that wasn't a requirement, but it is... – MrD Apr 06 '23 at 23:47