由于 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 提交的人发送电子邮件。 ( source )在 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
才不是。有没有人遇到过类似的问题?我错过了什么吗?
任何建议,将不胜感激。
最佳答案
听起来有点像你想要的 CulpritsRecipientProvider
或 DevelopersRecipientProvider
反而。UpstreamComitterRecipientProvider
查看上游构建,而不是当前构建。
关于git - 向 Jenkins 管道中的上游提交者发送通知电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912510/