1

recently we started moving out jenkins jobs to Jenkins pipeline and we are running to some problems with email sending.

If I do something like this

node('WCH-Regression') {
    // do the first test
    stage 'Test'
    node {
        try {
            sh 'exit 1'
        } catch(err) {
            echo "Error"
            currentBuild.result = "FAILURE"
        } finally {
                step([$class                  : 'Mailer',
                  notifyEveryUnstableBuild: true,
                  recipients              : "ppolivka@xxx.yyy",
                  sendToIndividuals       : true])
        }
    }
}

The email is acknowledged in the job log, but never delivered.

[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ exit 1
[Pipeline] echo
Error
[Pipeline] step
Sending e-mails to: ppolivka@xxx.yyy
[Pipeline] }
[Pipeline] // node

If I do the same thing in normal Jenkins job email is delivered. Also test emails from email configuration in 'Manage Jenkins' are delivered. Do I need to somehow enable emails for pipeline?

We are running Jenkins: 2.7.2 Pipeline plugin 2.2 Mailer plugin 1.17

I just tried to simply send email via

node {
    mail body: 'test', subject: 'test', to: 'ppolivka@xxx.yyy'
}

And still nothing was received. My email configuration in jenkins is fine, all other non pipeline jobs are sending emails and test email from the email notification configuration is working. Why would pipeline emails would not be delivered?

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Pavel Polivka
  • 858
  • 4
  • 22

2 Answers2

3

We finally figured this out.

I did some "debugging" of the pipeline email code.

node {
    def transportClass = javax.mail.Session.getDefaultInstance(java.lang.System.getProperties(), (javax.mail.Authenticator)null).getTransport(new javax.mail.internet.InternetAddress("ppolivka@xxx.com")).getClass();
    echo transportClass.toString();
    mail body: 'test', subject: 'test', to: 'ppolivka@xxx.com'
} 

And from this I noticed that used email transporter is mocked one.

[Pipeline] {
[Pipeline] echo
class org.jvnet.mock_javamail.MockTransport
[Pipeline] mail
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

This is happening when you have mock-javamail.jar on your classpath. I went through our Jenkins plugins and noticed that somebody added plugin that was not from Jenkins repository but build by hand from somebody from other team. This plugin had this jar on the classpath. After the plugin was removed and Jenkins restarted mails started working.

Pavel Polivka
  • 858
  • 4
  • 22
1

Try to re-throw exception (in catch) to make sure the build fails and stops

Amityo
  • 5,635
  • 4
  • 22
  • 29