由于Jenkins在推送到GitHub之后自动构建了所有项目,我们希望Jenkins在构建pipline结束时发送成功或不成功时发送电子邮件通知。
我使用以下脚本创建了一个共享库:
#!/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
没有。
有没有人遇到过类似的问题?我错过了什么吗?
任何建议都将不胜感激。
答案 0 :(得分:0)
听起来有点像你真的想要CulpritsRecipientProvider或DevelopersRecipientProvider作为UpstreamComitterRecipientProvider查看上游版本而不是当前版本。