如何使用一个或多个 SCM 在 Jenkins 工作流中找到罪魁祸首或提交者

How to get culprits or committers inside a Jenkins workflow with one or more SCMs

从一个或多个 SCM 签出时(通过 checkout() 或其他 SCM 步骤,如 git/svn)是否可以访问有关提交者的信息 and/or Jenkins 工作流作业的罪魁祸首?

目的是使用该信息通知提交者 and/or 罪魁祸首有关作业状态,例如在 mail 步骤中。

工作流定义的一个小例子:

node {
  // checkout from one or more SCMs, e.g.
  git url: '<URL>' 
  checkout([$class:...])
  ...

  // how can we know about committers or culprits at this point?
  $committers = ??

  // send a mail to committers or culprits
  mail to: '$committers', subject: 'JENKINS', body: '<information about the job status>'
}

在 运行 SCM 步骤之后,如何对其进行调整以获取提交者的集合?

编辑: 我目前正在使用 Jenkins 1.596.2 版和 Workflow: Aggregator 1.6 版,这似乎是 JENKINS-24141

中的一个未决问题

如果你想通知破坏构建的罪魁祸首,你不需要任何检查,使用 jenkins 中的电子邮件插件。此插件让您可以选择在过去的良好构建和当前损坏的构建之间向提交者发送邮件。

如果您正在使用 "Editable email notifier plugin",您可以选择向罪犯发送邮件。

如果您使用的是电子邮件插件,那么您可以选择 "Send separate e-mails to individuals who broke the build"。

您可以获得工作的 xml 信息,您可以在其中找到提交更改的人员的姓名以及提交消息。

http://<Jenkins URL>:<Port Number>/job/<Jobname>/<BuildNumber>/api/xml?

在您的浏览器中试一试。搜索 "user"。 您可以将此信息转储到文本文件中进行处理。

如您所见,待定 JENKINS-24141 这不受支持。需要更改 Jenkins 核心。

似乎这个功能是在 email-ext 插件中实现的,但作者忘记记录我们应该如何使用它。

请检查 https://issues.jenkins-ci.org/browse/JENKINS-34763 -- 并在那里添加评论,要求提供示例。我已经做了。

现在可以使用 email-ext 插件。

def to = emailextrecipients([[$class: 'CulpritsRecipientProvider'],
                             [$class: 'DevelopersRecipientProvider'],
                             [$class: 'RequesterRecipientProvider']])
if (to != null && !to.isEmpty()) {
    mail to: to, subject: "JENKINS", body: "See ${env.BUILD_URL}"
}

但是,如果您只想在失败时发送电子邮件,您可能需要使用 Mailer(基于 email-ext pipeline examples):

step([$class: 'Mailer',
      notifyEveryUnstableBuild: true,
      recipients: emailextrecipients([[$class: 'CulpritsRecipientProvider'],
                                      [$class: 'RequesterRecipientProvider']])])

在管道脚本中使用 groovy:

@NonCPS  // Necessary to allow .each to work.
def changelist() {
    def changes = ""
    currentBuild.changeSets.each { set ->
        set.each { entry ->
            changes += "${entry.commitId} by ${entry.author.fullName}\n"
        }
    }
    changes
}

类似于@szym 的回答,但没有 @NonCPS 要求:

def authors = currentBuild.changeSets.collectMany { it.toList().collect { it.author } }.unique()

您可以获取提交者的电子邮件:

committerEmail = sh (
  script: 'git --no-pager show -s --format=\'%ae\'',
  returnStdout: true
).trim()

并发送:

emailext body: 'text you choose', subject: 'subject you choose', recipientProviders: [[$class: 'DevelopersRecipientProvider']], to: committerEmail

取自:https://medium.com/@dilunika/find-the-git-commit-user-jenkins-pipeline-b6790613f8b5

emailext 插件中你可以提供 culprits, developers, requestorrecipientProviders 直接。

emailext body: '', 
recipientProviders: [culprits(),
 developers(),
 brokenBuildSuspects(), 
 brokenTestsSuspects(), 
 requestor()], 
subject: ''

描述

罪魁祸首: 将电子邮件发送到自上次未损坏构建到现在为止提交更改的用户列表。此列表至少总是包括在此构建中进行更改的人员,但如果之前的构建失败,它还包括来自那里的罪魁祸首列表。

开发人员: 向在变更集中引起变更的所有人员发送电子邮件。

构建失败嫌疑人:向怀疑导致构建开始失败的用户列表发送电子邮件。

Broken Test suspects:向怀疑导致单元测试开始失败的用户列表发送电子邮件。此列表包括测试开始失败的构建的提交者和请求者,以及测试开始失败的构建之前任何连续失败构建的提交者和请求者。

来源: Jenkins 管道语法 - 片段生成器