2016-06-10 110 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