我在groovy中设置了一个管道,需要拉取在git中提交了一些代码的人,这样我就可以发布破坏构建的人的名字。我在网上搜索了一下,似乎找不到解决办法。我通过使用jenkins的slack插件,弄清楚了如何在slack中发布posted。示例:
slackSend color: 'warning', message: "${git.user_name} broke the build."
yxyvkwin1#
你必须使用shell并执行git命令来检索数据,将其存储在文件中,然后将文件读入变量,如下所示:
git
sh 'git log --format="%ae" | head -1 > commit-author.txt' readFile('commit-author.txt').trim()
上面的代码将为您提供最后提交的作者。
dgiusagp2#
我使用以下方法。首先在JenkinsFile中添加一个stage,从git log中检索提交作者(和消息)到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() } } }
然后在post-build操作中发送Slack消息:(顺便说一句,我发送到两个不同的通道(成功/失败),以便成功通道可以静音。
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}'""") } }
mutmk8jj3#
还有一种方法可以获取信息。Jenkins中运行的每个作业都有一个名为${env.BUILD_URL}的变量。如果你在这个${env.BUILD_URL}中添加“API/json”并curl这个url,你将得到Jenkins知道的关于这个构建的所有信息。提交者名称也显示在那里:
${env.BUILD_URL}
"commitId": "d2212180afc238fb423981d91f39d680dfd06c67", "timestamp": 1499117423000, "author": { "absoluteUrl": "https://jenkins.company.com/user/camelel", "fullName": "itai ganot"
下面的命令会让你得到最后一个提交者的全名:
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"
示例:
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
为了提高可读性,您可以使用python jsonTool或jq工具,它们将输出排序为JSON。
jq
curl ${env.BUILD_URL}api/json | python -mjson.tool
或
curl ${env.BUILD_URL}api/json | jq
2sbarzqh4#
一行:
def lastCommiterEmail = sh(returnStdout: true, script: 'git log --format="%ae" | head -1').trim()
bz4sfanl5#
我提取用户电子邮件的方法。
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}" }
t5zmwmid6#
如果你安装了Email Ext Plugin,另一种获取提交者的方法是:
def emailTo = emailextrecipients([culprits()])
6条答案
按热度按时间yxyvkwin1#
你必须使用shell并执行
git
命令来检索数据,将其存储在文件中,然后将文件读入变量,如下所示:上面的代码将为您提供最后提交的作者。
dgiusagp2#
我使用以下方法。
首先在JenkinsFile中添加一个stage,从git log中检索提交作者(和消息)到env中。var:
然后在post-build操作中发送Slack消息:(顺便说一句,我发送到两个不同的通道(成功/失败),以便成功通道可以静音。
mutmk8jj3#
还有一种方法可以获取信息。
Jenkins中运行的每个作业都有一个名为
${env.BUILD_URL}
的变量。如果你在这个
${env.BUILD_URL}
中添加“API/json”并curl这个url,你将得到Jenkins知道的关于这个构建的所有信息。提交者名称也显示在那里:
下面的命令会让你得到最后一个提交者的全名:
示例:
为了提高可读性,您可以使用python jsonTool或
jq
工具,它们将输出排序为JSON。或
2sbarzqh4#
一行:
bz4sfanl5#
我提取用户电子邮件的方法。
t5zmwmid6#
如果你安装了Email Ext Plugin,另一种获取提交者的方法是: