问题
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."
回答1:
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.
回答2:
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":"camelel@gmail.com","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
回答3:
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}"
}
来源:https://stackoverflow.com/questions/37755586/how-do-you-pull-git-committer-information-for-jenkins-pipeline