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

Plugin should provide GIT_COMMIT_MESSAGE as Environment Variable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • git-plugin
    • None

      We often need to do decision making on base of commit message like if commit message contains WIP so build process should not start.

          [JENKINS-52490] Plugin should provide GIT_COMMIT_MESSAGE as Environment Variable

          Mark Waite added a comment - - edited

          This is already available for Pipeline jobs by using the GIT_COMMIT (sha1 of the commit) value returned in the map of the checkout step. Use that SHA1 to call a shell step and read the commit message into your Pipeline.

              branch = 'JENKINS-50401'
              checkout_result = checkout([$class: 'GitSCM',
                          branches: [[name: branch]],
                          extensions: [[$class: 'CloneOption', honorRefspec: true, noTags: true],
                                       [$class: 'LocalBranch', localBranch: branch]
                                      ],
                          userRemoteConfigs: [[refspec: "+refs/heads/${branch}:refs/remotes/origin/${branch}", 
                                                              url: 'https://github.com/MarkEWaite/jenkins-bugs.git']]])
              commit_message = sh(returnStdout: true, script: "git log -n 1 ${checkout_result.GIT_COMMIT}")
          

          If that same information is needed in a Freestyle project, you'll need to use a similar technique from within the build script.

          Mark Waite added a comment - - edited This is already available for Pipeline jobs by using the GIT_COMMIT (sha1 of the commit) value returned in the map of the checkout step. Use that SHA1 to call a shell step and read the commit message into your Pipeline. branch = 'JENKINS-50401' checkout_result = checkout([$class: 'GitSCM', branches: [[name: branch]], extensions: [[$class: 'CloneOption', honorRefspec: true, noTags: true], [$class: 'LocalBranch', localBranch: branch] ], userRemoteConfigs: [[refspec: "+refs/heads/${branch}:refs/remotes/origin/${branch}", url: 'https://github.com/MarkEWaite/jenkins-bugs.git']]]) commit_message = sh(returnStdout: true, script: "git log -n 1 ${checkout_result.GIT_COMMIT}") If that same information is needed in a Freestyle project, you'll need to use a similar technique from within the build script.

          imran khan added a comment -

          How to access it in declarative pipeline where checkout done implicitly. I know we can over ride this behaviour. But it would be nice to get this variable out of box.

          imran khan added a comment - How to access it in declarative pipeline where checkout done implicitly. I know we can over ride this behaviour. But it would be nice to get this variable out of box.

          Mark Waite added a comment - - edited

          Refer to the Jenkins Minute video that shows how to use the GIT_COMMIT id in a shell step. Use that GIT_COMMIT value from the declarative Pipeline to call

          git log -n 1 ${GIT_COMMIT}
          

          Mark Waite added a comment - - edited Refer to the Jenkins Minute video that shows how to use the GIT_COMMIT id in a shell step. Use that GIT_COMMIT value from the declarative Pipeline to call git log -n 1 ${GIT_COMMIT}

          imran khan added a comment -

          Till the time we didnt get GIT_COMMIT_MESSAGE, Following step will work in declarative jenkins piepline

          GIT_MESSAGE = powershell (returnStdout: true, script: 'git log -1 --format=%B ${GIT_COMMIT}').trim()

          imran khan added a comment - Till the time we didnt get GIT_COMMIT_MESSAGE, Following step will work in declarative jenkins piepline GIT_MESSAGE = powershell (returnStdout: true, script: 'git log -1 --format=%B ${GIT_COMMIT}').trim()

          J Knurek added a comment -

          I've had a try at implemented what is needed for this feature. I actually haven't tested it, or added any unit tests, because I'm not even sure this is a valid approach. So I'd appreciate some feedback on the PR before continuing: https://github.com/jenkinsci/git-plugin/pull/796

          J Knurek added a comment - I've had a try at implemented what is needed for this feature. I actually haven't tested it, or added any unit tests, because I'm not even sure this is a valid approach. So I'd appreciate some feedback on the PR before continuing:  https://github.com/jenkinsci/git-plugin/pull/796

          Mark Waite added a comment - - edited

          I've reviewed and commented on the pull request.

          Mark Waite added a comment - - edited I've reviewed and commented on the pull request .

            Unassigned Unassigned
            imranrazakhan imran khan
            Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: