向Jenkins管道中的上游提交者发送通知电子邮件

时间:2017-05-11 10:12:22

标签: git jenkins continuous-integration email-ext jenkins-email-ext

由于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 &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</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没有。

有没有人遇到过类似的问题?我错过了什么吗?

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

听起来有点像你真的想要CulpritsRecipientProvider或DevelopersRecipientProvider作为UpstreamComitterRecipientProvider查看上游版本而不是当前版本。