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

Provide easy access to current commit ID in Jenkinsfile

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • pipeline
    • None

      During old style jenkins jobs I can easily print current commit ID (e.g. GIT_COMMIT, SVN_REVISION) to the console.
      But when I want to use it in Jenkinsfile (multi-branch pipeline)I tried

      env.GIT_COMMIT
      

      It gives me null/empty output.
      Commit hash might be useful for informing some revision software about build that succeeded or failed (e.g. I want to use it to ping Atlassian Stash about my build).

      I think this is one of the basic SCM information that should be available (just like current branch name in env.BRANCH_NAME).

          [JENKINS-34455] Provide easy access to current commit ID in Jenkinsfile

          krzyk created issue -
          krzyk made changes -
          Description Original: During old style jenkins jobs I can easily print current commit ID (e.g. GIT_COMMIT) to the console.
          But when I want to use it in Jenkinsfile I tried
          {code}
          env.GIT_COMMIT
          {code}

          It gives me null/empty output.
          Commit hash might be useful for informing some revision software about build that succeeded or failed (e.g. I want to use it to ping Atlassian Stash about my build).

          I think this is one of the basic SCM information that should be available (just like current branch name in {{env.BRANCH_NAME}}).
          New: During old style jenkins jobs I can easily print current commit ID (e.g. GIT_COMMIT, SVN_REVISION) to the console.
          But when I want to use it in Jenkinsfile (multi-branch pipeline)I tried
          {code}
          env.GIT_COMMIT
          {code}

          It gives me null/empty output.
          Commit hash might be useful for informing some revision software about build that succeeded or failed (e.g. I want to use it to ping Atlassian Stash about my build).

          I think this is one of the basic SCM information that should be available (just like current branch name in {{env.BRANCH_NAME}}).

          zimba tm added a comment - - edited

          I have a less than ideal workaround:

          sh "git rev-parse --short HEAD > .git/commit-id"                        
          commit_id = readFile('.git/commit-id')
          

          zimba tm added a comment - - edited I have a less than ideal workaround: sh "git rev-parse --short HEAD > .git/commit-id" commit_id = readFile('.git/commit-id')

          Jesse Glick added a comment -

          duplicate of a well-known issue

          Jesse Glick added a comment - duplicate of a well-known issue
          Jesse Glick made changes -
          Link New: This issue duplicates JENKINS-26100 [ JENKINS-26100 ]
          Jesse Glick made changes -
          Resolution New: Duplicate [ 3 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]
          R. Tyler Croy made changes -
          Workflow Original: JNJira [ 170572 ] New: JNJira + In-Review [ 198892 ]
          Andrew Bayer made changes -
          Component/s New: pipeline-general [ 21692 ]
          Andrew Bayer made changes -
          Component/s Original: workflow-plugin [ 18820 ]

          A bit shorter version of the workaround above:

          commitId = sh(returnStdout: true, script: 'git rev-parse HEAD')
          

          Vsevolod Kalinin added a comment - A bit shorter version of the workaround above: commitId = sh(returnStdout: true , script: 'git rev-parse HEAD' )

            jglick Jesse Glick
            krzyk krzyk
            Votes:
            1 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved: