Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-44449

Provide GIT_COMMIT_SHORT environment variable

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Won't Fix
    • git-plugin
    • None

    Description

      GIT_COMMIT env variable provides SHA of the current commit being build.

      Please also provide GIT_COMMIT_SHORT env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

      e.g. 81995adb1726ffdd1ab9c8911fe2bfa2a96f6679 would be shown as 81995ad

      Links:
      https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416

      https://git-scm.com/docs/git-rev-parse

       

      Attachments

        Issue Links

          Activity

            bjmi Björn Michael created issue -
            bjmi Björn Michael made changes -
            Field Original Value New Value
            Description _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
            [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416]
            [https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html]

             
            _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
             [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416]
             [https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html|https://git-scm.com/docs/git-rev-parse]
            bjmi Björn Michael made changes -
            Description _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
             [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416]
             [https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html|https://git-scm.com/docs/git-rev-parse]
            _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
             [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416
            ] [https://git-scm.com/docs/git-rev-parse]

             
            bjmi Björn Michael made changes -
            Description _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
             [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416
            ] [https://git-scm.com/docs/git-rev-parse]

             
            _GIT_COMMIT_ env variable provides SHA of the current commit being build.

            Please also provide _GIT_COMMIT_SHORT_ env variable that contains the short format of SHA that is easier to read and to use as build qualifier.

            e.g. _81995adb1726ffdd1ab9c8911fe2bfa2a96f6679_ would be shown as _81995ad_

            Links:
            [https://stackoverflow.com/questions/5694389/get-the-short-git-version-hash#answer-5694416]

            [https://git-scm.com/docs/git-rev-parse]

             
            markewaite Mark Waite made changes -
            Assignee Mark Waite [ markewaite ]
            bjmi Björn Michael made changes -
            Link This issue relates to JENKINS-26100 [ JENKINS-26100 ]

            Current workaround uses Environment Script Plugin with following script content:

            echo GIT_SHA_SHORT=`git rev-parse --short=8 ${GIT_COMMIT}`

            bjmi Björn Michael added a comment - Current workaround uses  Environment Script Plugin  with following script content: echo GIT_SHA_SHORT=`git rev-parse --short=8 ${GIT_COMMIT}`
            swf Yves Schumann added a comment -

            On a declarative Pipeline, the following workaround could be used:

            pipeline { 
                ...
                environment {
                    GIT_COMMIT_SHORT = sh(
                            script: "printf \$(git rev-parse --short ${GIT_COMMIT})",
                            returnStdout: true
                    )
                }
                ...

            Usage of printf is necessary to prevent trailing newline on the end of generated short Git hash.

            swf Yves Schumann added a comment - On a declarative Pipeline, the following workaround could be used: pipeline { ...     environment {         GIT_COMMIT_SHORT = sh(                 script: "printf \$(git rev-parse -- short ${GIT_COMMIT})" ,                 returnStdout: true         )     } ... Usage of printf is necessary to prevent trailing newline on the end of generated short Git hash.

            You can just use

            env.GIT_COMMIT.take(7)
            
            bbd5875 bbd5875 bbd5875 added a comment - You can just use env.GIT_COMMIT.take(7)

            git rev-parse ensures that short SHA1 is unique during execution and increases number of digits appropriately.

            bjmi Björn Michael added a comment - git rev-parse ensures that short SHA1 is unique during execution and increases number of digits appropriately.
            markewaite Mark Waite added a comment -

            There are several different workarounds available for this request as described in earlier comments. In addition to those workarounds, there is the token macro GIT_REVISION that can be referenced in some contexts with a parameter describing the number of characters to retain from the commit SHA-1

            markewaite Mark Waite added a comment - There are several different workarounds available for this request as described in earlier comments. In addition to those workarounds, there is the token macro GIT_REVISION that can be referenced in some contexts with a parameter describing the number of characters to retain from the commit SHA-1
            markewaite Mark Waite made changes -
            Resolution Won't Fix [ 2 ]
            Status Open [ 1 ] Closed [ 6 ]

            People

              Unassigned Unassigned
              bjmi Björn Michael
              Votes:
              6 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: