由于Jenkins会在我们推送到GitHub后自动构建所有项目,因此我们希望Jenkins在构建流程结束时发送电子邮件通知,以便通知构建是否成功。
我创建了一个共享库,并编写了以下脚本:
#!/usr/bin/env groovy
def call(String buildStatus = 'STARTED') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESS'
// Default values
def subject = "JENKINS-NOTIFICATION: ${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def details = """<p>${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>"""
// Send email to user who has started the build
emailext(
subject: subject,
body: details,
attachLog: true,
compressLog: true,
recipientProviders: [[$class: 'RequesterRecipientProvider'], [$class:'UpstreamComitterRecipientProvider']]
)
}
请注意,我定义了两个
recipientProviders
。据我所知,RequesterRecipientProvider
应该向在Jenkins上手动触发构建的人发送电子邮件,而UpstreamComitterRecipientProvider
应该向最后一次git提交引发构建的人发送电子邮件。(来源)在Jenkinsfile中,我加载了库,并在jenkinsfile的post-block中定义了sendNotification命令:
#!groovy
@Library('shared-library@master') _
pipeline {
agent any
stages{
stage('Checkout code base'){
steps{
checkout scm
}
}
stage('do something'){
steps{
sh "do something"
}
}
stage('do something'){
steps{
sh "do something"
}
}
}
post{
always{
sendNotifications currentBuild.result
}
}
}
当我手动触发Jenkins构建时,会收到电子邮件通知,但当我推送到GitHub并触发Jenkins构建时,没有发送电子邮件通知。这是管道的日志:
messageContentType = text/html; charset=UTF-8
Request made to attach build log
Request made to compress build log
Adding recipients from project recipient list
Sending email to upstream committer(s).
Adding recipients from trigger recipient list
Successfully created MimeMessage
An attempt to send an e-mail to empty list of recipients, ignored.
Some error occured trying to send the email...check the Jenkins log
很遗憾,Jenkins日志里没有更多的信息。在我的看法中,UpstreamComitterRecipientProvider
没有像它应该做的那样提供最后一位提交者的电子邮件地址。
我尝试使用 DevelopersRecipientProvider
,这将向项目提交历史记录中的所有开发人员发送电子邮件。这很好用。很不幸,UpstreamComitterRecipientProvider
却不行。
是否有人遇到类似的问题?我是否遗漏了什么?
如果您有任何建议,请告诉我。