10/31/17

Find the Git Commit User — Jenkins Pipeline


I recently started to use Jenkins 2 Pipeline scripts to setup Continuous Delivery Process. I wanted to get the email of the Git user who has done the commit for that particular build. Initially idea was to use following global variables set by the Jenkins Git Plugin.
  • GIT_AUTHOR_NAME and GIT_COMMITTER_NAME
  • GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL
Somehow, things didn’t work as per the plugin documentation. So, I decided to use Git client with shell or batch scripts. Following is how it is done in both Linux and Windows environments.
In Windows — as usual some tricks required 
def commiterDetails = bat (
script: 'git --no-pager show -s --format=%%ae',
returnStdout: true
)
def commiter = extractCommiterEmail(commiterDetails)
def extractCommiterEmail(details) {
def arr = details.tokenize('\n')
def email = arr[2].trim()
return email
}

In Linux
committerEmail = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()