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

Provide easy access to current commit ID in Jenkinsfile

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved (View Workflow)
    • Major
    • Resolution: Duplicate
    • pipeline
    • None

    Description

      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).

      Attachments

        Issue Links

          Activity

            zimbatm 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')
            
            zimbatm 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')
            jglick Jesse Glick added a comment -

            duplicate of a well-known issue

            jglick Jesse Glick added a comment - duplicate of a well-known issue

            A bit shorter version of the workaround above:

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

            CI system does not give straightforward way to access git commit id is a pain.

            even this way: https://stackoverflow.com/a/59167316/747579 , does not work with me

            abdennour Abdennour Toumi added a comment - CI system does not give straightforward way to access git commit id is a pain. even this way: https://stackoverflow.com/a/59167316/747579  , does not work with me

            def b= currentBuild 

            b?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }?.revision?.hash

            abdennour Abdennour Toumi added a comment - def b= currentBuild  b?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }?.revision?.hash

            People

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

              Dates

                Created:
                Updated:
                Resolved: