Reputation: 2507
I have a pipeline set up in groovy and need to pull the person who committed some code in git so I can publish that persons name who broke the build. I've searched the web and can't seem to find a solution. I figured out how to publish posted in slack by using the slack plugin for jenkins. Example:
slackSend color: 'warning', message: "${git.user_name} broke the build."
Upvotes: 10
Views: 20242
Reputation: 347
For windows you can use
script
{
def committerDetails = bat (
script: 'git log -n 1 --format="%%ae"',
returnStdout: true
)
env.COMMITTER_EMAIL = committerDetails.tokenize('\n')[2]
}
And use it like
mail to: "${env.COMMITTER_EMAIL}",
from: '[email protected]',
subject: 'Buld Failures Detected',
body: "Job Failed - \"${env.JOB_NAME}\" build: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
If you want all email addresses for all commits on the branch, you can use
def allCommitterDetails = bat (
script: 'git log --format="%%ae"',
returnStdout: true
)
def committersEmails = allCommitterDetails.tokenize('\n')
committersEmails .unique()
env.COMITTERS_EMAILS = committersEmails
Upvotes: 0
Reputation: 2215
Another way to get committers if you have Email Ext Plugin installed:
def emailTo = emailextrecipients([culprits()])
Upvotes: 1
Reputation: 41
One line:
def lastCommiterEmail = sh(returnStdout: true, script: 'git log --format="%ae" | head -1').trim()
Upvotes: 2
Reputation: 1910
I use the following method.
First add a stage in JenkinsFile to retrieve commit author (and message) from git log into an env. var:
stage('get_commit_details') {
steps {
script {
env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
env.GIT_AUTHOR = sh (script: 'git log -1 --pretty=%cn ${GIT_COMMIT}', returnStdout: true).trim()
}
}
}
Then in post-build action send the Slack message: (BTW I send to two different channels (success/failure) so that the success channel can be muted.
post {
failure {
slackSend (channel: 'xyz-build-failure', color: '#FF0000', message: """FAILED:
Job: ${env.JOB_NAME}
Build #${env.BUILD_NUMBER}
Build: ${env.BUILD_URL})
Comitted by: ${env.GIT_AUTHOR}
Last commit message: '${env.GIT_COMMIT_MSG}'""")
}
success {
slackSend (channel: 'xyz-build-success', color: '#00FF00', message: """SUCCESS:
Job: ${env.JOB_NAME}
Build #${env.BUILD_NUMBER}
Build: ${env.BUILD_URL})
Comitted by: ${env.GIT_AUTHOR}
Last commit message: '${env.GIT_COMMIT_MSG}'""")
}
}
Upvotes: 5
Reputation: 1239
My way of pulling user email.
script{
def COMMITTER_EMAIL = bat (
script: "git --no-pager show -s --format=%%ae",
returnStdout: true
).split('\r\n')[2].trim()
echo "The last commit was written by ${COMMITTER_EMAIL}"
}
Upvotes: 0
Reputation: 6305
There's another way to pull that information.
For each job run in Jenkins there's a variable which is called ${env.BUILD_URL}
.
If you add to this ${env.BUILD_URL}
"api/json" and curl this url you will get all the information that Jenkins knows about that build.
Committer name is also displayed there:
"commitId": "d2212180afc238fb423981d91f39d680dfd06c67",
"timestamp": 1499117423000,
"author": {
"absoluteUrl": "https://jenkins.company.com/user/camelel",
"fullName": "itai ganot"
The following command will get you the full name of the last committer:
itai@Itais-MacBook-Pro ~/src/scripts - (master) $ curl -s --insecure https://jenkins.company.com/job/geek-kb/job/scripts/job/master/5/api/json | python -mjson.tool | grep fullName
"fullName": "itai ganot"
Example:
itai@Itais-MacBook-Pro ~/src/scripts - (master) $ curl -s --insecure https://jenkins.company.com/job/geek-kb/job/scripts/job/master/5/api/json
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","actions":[{"_class":"hudson.model.CauseAction","causes":[{"_class":"jenkins.branch.BranchIndexingCause","shortDescription":"Branch indexing"}]},{},{},{},{},{},{"_class":"hudson.plugins.git.util.BuildData","buildsByBranchName":{"master":{"_class":"hudson.plugins.git.util.Build","buildNumber":5,"buildResult":null,"marked":{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","branch":[{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","name":"master"}]},"revision":{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","branch":[{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","name":"master"}]}}},"lastBuiltRevision":{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","branch":[{"SHA1":"d2212180afc238fb423981d91f39d680dfd06c67","name":"master"}]},"remoteUrls":["https://github.com/geek-kb/scripts.git"],"scmName":""},{"_class":"hudson.plugins.git.GitTagAction"},{},{"_class":"org.jenkinsci.plugins.workflow.cps.EnvActionImpl"},{},{},{},{"_class":"org.jenkinsci.plugins.workflow.job.views.FlowGraphAction"},{},{}],"artifacts":[],"building":false,"description":null,"displayName":"# 5 | master","duration":17807,"estimatedDuration":14531,"executor":null,"fullDisplayName":"Itai Ganot » scripts » master # 5 | master","id":"5","keepLog":false,"number":5,"queueId":4894,"result":"SUCCESS","timestamp":1499117462714,"url":"https://jenkins.company.com/job/geek-kb/job/scripts/job/master/5/","changeSets":[{"_class":"hudson.plugins.git.GitChangeSetList","items":[{"_class":"hudson.plugins.git.GitChangeSet","affectedPaths":["Jenkinsfile"],"commitId":"d2212180afc238fb423981d91f39d680dfd06c67","timestamp":1499117423000,"author":{"absoluteUrl":"https://lel.doesntexist.com/user/camelel","fullName":"itai ganot"},"authorEmail":"[email protected]","comment":"Test\n","date":"2017-07-04 00:30:23 +0300","id":"d2212180afc238fb423981d91f39d680dfd06c67","msg":"Test","paths":[{"editType":"edit","file":"Jenkinsfile"}]}],"kind":"git"}],"nextBuild":null,"previousBuild":{"number":4,"url":"https://lel.doesntexist.com/job/geek-kb/job/scripts/job/master/4/"}}itai@Itais-MacBook-Pro ~/src/scripts - (master) $ curl -s --insecure https://lel.doesntexist.com/job/geek-kb/job/scripts/job/master/5/api/json
For more readability, you may use python jsonTool or the tool jq
which will order the output as JSON.
curl ${env.BUILD_URL}api/json | python -mjson.tool
or
curl ${env.BUILD_URL}api/json | jq
Upvotes: 3
Reputation: 27466
You have to use shell for that and execute git
command to retrieve data, store it in a file and later read the file into a variable, like this:
sh 'git log --format="%ae" | head -1 > commit-author.txt'
readFile('commit-author.txt').trim()
The above will give you the last commit author.
Upvotes: 7