你如何为Jenkins管道提取git提交者信息

时间:2016-06-10 19:09:28

标签: git jenkins jenkins-pipeline

我在groovy中设置了一个管道,需要拉出在git中提交一些代码的人,这样我就可以发布破坏构建的人名。我在网上搜索过,似乎无法找到解决方案。我想通过使用jenkins的slack插件找出如何发布松弛的帖子。例如:

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

5 个答案:

答案 0 :(得分:3)

您必须使用shell并执行git命令来检索数据,将其存储在文件中,然后将文件读入变量,如下所示:

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

以上内容将为您提供最后一位提交作者。

答案 1 :(得分:1)

还有另一种方法来提取这些信息。

对于Jenkins中运行的每个作业,都有一个名为${env.BUILD_URL}的变量。

如果您添加到此${env.BUILD_URL}“api / json”并卷曲此网址,您将获得Jenkins所知道的有关该版本的所有信息。

提交者名称也显示在那里:

  "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。

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

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

答案 2 :(得分:0)

我提取用户电子邮件的方式。

@OneToOne
@JoinColumn(name="USERS_ID")
private UserDetails userDetails;

答案 3 :(得分:0)

我使用以下方法。

首先在JenkinsFile中添加一个阶段,以从git log到环境中检索提交作者(和消息)。 var:

  var DateString="21/03/2020"
  DateString=DateString.split("/")
  var bdate=new Date(DateString[1]+"/"+DateString[0]+"/"+DateString[2])
  var temp=bdate.getDay()

然后在构建后操作中发送Slack消息: (顺便说一句,我发送到两个不同的通道(成功/失败),以便可以忽略成功通道。

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()
            }
        }
    }

答案 4 :(得分:0)

一行:

def lastCommiterEmail = sh(returnStdout: true, script: 'git log --format="%ae" | head -1').trim()