2016-06-10 107 views
3

我在groovy中建立了一個管道,並且需要在git中提交一些代碼,因此我可以發佈那些破壞構建的人員名稱。我搜索了網頁,似乎無法找到解決方案。我想通過使用詹金斯的鬆弛插件來發布如何發佈在懶散。例如:如何爲Jenkins管道提供git提交者信息

slackSend color: 'warning', message: "${git.user_name} broke the build." 

回答

3

你必須使用外殼爲和執行git命令來獲取數據,將其存儲在一個文件中,後來文件讀入到一個變量,像這樣:

sh 'git log --format="%ae" | head -1 > commit-author.txt'     
readFile('commit-author.txt').trim()        

以上會給你最後的提交作者。

+1

參看[JENKINS-30412](https://issues.jenkins-ci.org/browse/JENKINS-30412)。 –

1

還有另一種方法來提取該信息。

對於在詹金斯運行的每項工作,都有一個名爲${env.BUILD_URL}的變量。

如果你添加到${env.BUILD_URL}「api/json」並且捲曲這個URL,你會得到Jenkins知道的所有信息。也顯示

提交者的名字有:

"commitId": "d2212180afc238fb423981d91f39d680dfd06c67", 
    "timestamp": 1499117423000, 
    "author": { 
    "absoluteUrl": "https://jenkins.company.com/user/camelel", 
    "fullName": "itai ganot" 

下面的命令,會得到最後的提交者的全名:

[email protected] ~/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" 

例子:

[email protected] ~/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/"}}[email protected] ~/src/scripts - (master) $ curl -s --insecure https://lel.doesntexist.com/job/geek-kb/job/scripts/job/master/5/api/json 

更多可讀性,您可以使用python jsonTool或工具jq這將以JSON命令輸出。

curl ${env.BUILD_URL}api/json | python -mjson.tool 

curl ${env.BUILD_URL}api/json | jq