Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarative pipeline send email

Tags:

email

jenkins

i need to send a mail notification using jenkins (if it is a success build or someting went wrong)

Right now i have something like that:

pipeline {
    agent any

...Some stages ...

post {
        success {
            mail to:"[email protected]", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
        }
        failure {
            mail to:"[email protected]", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
        }
    }   
}  

But when i execute i get the following error:

java.net.ConnectException: Connection refused: connect
Caused: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect

I guess i cannot send on the port 25 i need to send it on the port 465.. am i wrong?But i dont know how can i do this.

In most of the examples they use plugins to the jenkins, but i can only use code (declarative pipeline)

suggestions?

like image 772
RtyUP Avatar asked Dec 04 '17 21:12

RtyUP


People also ask

Which is better scripted or declarative pipeline?

Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.

What is declarative pipeline?

A declarative pipeline supports conditional statement usage, allows access to environment variables and provides facilities to add logging and error handling. The tradeoff is that declarative pipelines don't allow deep integration into Groovy and Java APIs.

How do I send an email to Jenkins?

To send email, the plugin needs the smtp configured. Go to Manage Jenkins > Configure System > search for “Extended E-mail Notification”. Configure the smtp. Example: My configuration with gmail smtp.


1 Answers

Jenkins default email server attempts to run through your localhost on port 25. That error message indicates, as you have guessed, that you need to change the email settings.

To do so, go to Manage Jenkins -> Configure System -> Email Notifcation or Extended Email Notification

As an aside, unless you have an email server running locally (and I'd be surprised if that was the case), changing the local port number won't work. You would need to point it to an external server. Here is a setting page for having Jenkins use the Google SMTP server, for instance

like image 180
cstarner Avatar answered Oct 14 '22 11:10

cstarner