Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pull git committer information for Jenkins pipeline

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."
like image 823
user301693 Avatar asked Jun 10 '16 19:06

user301693


People also ask

How does Jenkins pull from Git?

The Jenkins git plug-in The key to Jenkins Git integration is the Git plug-in. One can easily install the Jenkins Git plug-in through the Jenkins administrative console, and once properly configured, it gives all Jenkins build jobs the option to pull content from a Git-compatible source code repository.

How do I find my Git Jenkins repository URL?

Step 1: go to your GitHub repository and click on 'Settings'. Step 2: Click on Webhooks and then click on 'Add webhook'. Step 3: In the 'Payload URL' field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/.

How do you integrate Git with Jenkins?

How does Jenkins integrate with Git? Go to Jenkins dashboard, click on “Manage Jenkins.” Now follow these steps- Manage Plugins -> 'Available' tab -> Enter Git in search bar and filter -> Install required plugin. After the installation, all you need to do is click on “Configure System” and go to the 'GitHub' section.


3 Answers

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.

like image 185
Krzysztof Krasoń Avatar answered Oct 02 '22 13:10

Krzysztof Krasoń


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}'""")
    }
  }
like image 38
dux2 Avatar answered Oct 02 '22 12:10

dux2


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
like image 43
Itai Ganot Avatar answered Oct 02 '22 12:10

Itai Ganot