Since Jenkins builds all of our projects automatically after we push to GitHub, we would like Jenkins to send out e-mail notifications if the build was successful or not at the end of the build pipline.
I created a shared library with the following script:
#!/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']]
)
}
Note that I defined two recipientProviders
. As far as I know the RequesterRecipientProvider
should send an e-mail to the person who triggered the build manually at the Jenkins and the UpstreamComitterRecipientProvider
should send an e-mail to the person who did the last git commit which triggered the build. (source)
In the Jenkinsfile I loaded the library and I defined the sendNotification command in the post-block of the jenkinsfile:
#!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
}
}
}
Now e-mail notifications arrive when I manually trigger a build at Jenkins, but when I push to GitHub and Jenkins build gets triggered, no e-mail notification is sent. This is the log of the pipeline:
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
Unfortunately, there is no further information at the Jenkins log. It seems to me, that UpstreamComitterRecipientProvider
does not provide the e-mail address of the last committer as it should.
I have tried to use the DevelopersRecipientProvider
, which sends e-mails to all developers in the project's commit history. This works just fine. Unfortunately the UpstreamComitterRecipientProvider
does not.
Has anyone experienced similar problems? Am I missing something?
Any suggestions would be appreciated.
Sounds kinda like you want CulpritsRecipientProvider
or DevelopersRecipientProvider
instead.
UpstreamComitterRecipientProvider
looks at upstream builds, not the current one.