简体   繁体   English

设置 Jenkins 通过电子邮件向推送分支的 BitBucket 用户发送构建通知

[英]Setting Jenkins to email a build notification to the BitBucket user who pushed a branch

A project repository has been successfully connected to a Jenkins server using the BitBucket plugin , and a project set up such that:项目存储库已使用BitBucket 插件成功连接到 Jenkins 服务器,并且项目设置为:

  • Each push to a branch in BitBucket will trigger a webhook sent to the Jenkins server每次推送到 BitBucket 中的一个分支都会触发一个发送到 Jenkins 服务器的 webhook
  • When the Jenkins server receives the webhook it will build the changed branch (by specifying branch name as ** in the config)当 Jenkins 服务器收到 webhook 时,它将构建更改的分支(通过在配置中将分支名称指定为**
  • After the build is complete a notification is sent back to BitBucket of the build status using the BitBucket notifier构建完成后,使用BitBucket 通知程序将构建状态的通知发送回BitBucket

Each of these has been easy to set up with just the instructions in the plugin and a few quick Googles.只需按照插件中的说明和一些快速的 Google 搜索,即可轻松设置其中的每一个。 However I've now run into a problem which is maybe more a matter of wanting to run in an unconventional manner than anything else.但是,我现在遇到了一个问题,这可能更多是想要以非常规方式运行而不是其他任何问题。

Using the normal emailer plugin or the Email-ext plugin it's possible to set emails to send to people involved in the creation of a build.使用普通的 emailer 插件或 Email-ext 插件,可以设置电子邮件以发送给参与构建创建的人员。 For example the Email-ext plugin allows choice of:例如,Email-ext 插件允许选择:

  • Requester请求者
  • Developers (all people who have commits in the build based off its last version)开发人员(所有在基于最新版本的构建中提交的人)
  • Recipient list (a pre-set list)收件人列表(预设列表)
  • Various "blame" settings for broken builds损坏构建的各种“责备”设置

The development process being followed involves each project being worked on by one developer in a named branch, eg userA/projectB .所遵循的开发过程涉及由一个开发人员在命名分支中处理的每个项目,例如userA/projectB Obviously other developers could check that out and push to make changes but that's frowned upon.显然,其他开发人员可以检查出来并推动进行更改,但这是不受欢迎的。 Even in that instance, the user who pushes the change to BitBucket should be notified.即使在这种情况下,也应该通知更改送到 BitBucket 的用户。

None of the current settings support this.当前的设置都不支持这一点。 Requester is the closest, but that only works for manual builds. Requester是最接近的,但这仅适用于手动构建。 It seems a very simple requirement that the push to SCM that triggered a build should notify the user who pushed, but this is not documented anywhere that is easy to find.触发构建的推送到 SCM 应该通知推送的用户似乎是一个非常简单的要求,但这没有记录在任何容易找到的地方。

After a lot of searching it seems the only way to accomplish this is by using a Pre-send script .经过大量搜索,似乎唯一的方法是使用Pre-send script This is added to the Advanced setting of the Email-ext post-build step, and takes the form of code written in Groovy which is a Java extension.这被添加到Email-ext post-build 步骤的Advanced设置中,并采用用Java 扩展Groovy编写的代码形式。

The script can take advantage of Environment variables, but is hard to test as there's no way to run the script with these in place.该脚本可以利用环境变量,但很难测试,因为无法在这些变量就位的情况下运行脚本。 You can test simple Groovy scripts from Home -> Manage Jenkins -> Script console .您可以从Home -> Manage Jenkins -> Script console测试简单的 Groovy 脚本。

One important "gotcha" with the environment variables is that they are "included" in the script, rather than variables or constants.环境变量的一个重要“问题”是它们“包含”在脚本中,而不是变量或常量。 Eg before the script compiles and runs, the content of the variable is pasted in place of its $NAME .例如,在脚本编译和运行之前,变量的内容被粘贴到它的$NAME In the example below the multi-line string syntax is used to include the BitBicket payload, whereas it might be expected that def payload = $BITBUCKET_PAYLOAD would simply work.在下面的示例中,多行字符串语法用于包含 BitBicket 有效负载,而可能预期def payload = $BITBUCKET_PAYLOAD会简单地工作。

import javax.mail.Message.RecipientType
import javax.mail.Address
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()

def bitbucket = jsonSlurper.parseText('''
   $BITBUCKET_PAYLOAD'''
)

switch (bitbucket.actor.username){
  case "userA":
    msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.a@domain.com"));
  break;
  case "userB":
    msg.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("user.b@domain.com"));
  break;
}

The setRecipients command overwrites any existing recipient. setRecipients命令覆盖任何现有的收件人。 Thus the recipient list or other email configuration should be set as a fallback for if the user is not recognised.因此,如果用户未被识别,则应将收件人列表或其他电子邮件配置设置为备用。 Additionally, if there is nobody selected to send the email to, the script won't run at all .此外,如果没有人选择向其发送电子邮件,则脚本根本不会运行 As added debugging, including the username in the body might help.添加调试时,在正文中包含用户名可能会有所帮助。

If the script fails, stack traces should be printed to the console log output of the test, and the build pass/fail shouldn't be affected, but the normal email address setup will be used instead.如果脚本失败,堆栈跟踪应该打印到测试的控制台日志输出,并且构建通过/失败不应该受到影响,但将使用正常的电子邮件地址设置。 In stack traces look for lines with Script() in them, as that's the container which evaluates the Groovy script.在堆栈跟踪中查找其中包含Script()行,因为这是评估 Groovy 脚本的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM