Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline - Notify with console error log through email

Currently I have configured a job with Jenkinsfile to send notification in case of a failure .

catch (err) {
    currentBuild.result = "FAILED"
    mail (to: '[email protected]',
         subject: "Job '${env.JOB_NAME}'- (${env.BUILD_NUMBER}) has FAILED",
         body: "Please go to ${env.BUILD_URL} for more details. ");

    throw err
}

Is it possible to send console logs as well in the email in case of a job failure ?

like image 305
vivekyad4v Avatar asked May 17 '17 11:05

vivekyad4v


People also ask

How do I email notifications from Jenkins?

Go to the Jenkins home page and click the 'Manage Jenkins' menu option. Then, select the 'Configure System' option. Enter the SMTP server name under 'Email Notification'. Click the 'Advanced' button and then click the checkbox next to the 'Use SMTP Authentication' option.

How do I read Jenkins console logs?

In order to read the console output in Jenkins, All you need to do is copy and paste the code as a stage in your pipeline script. After the build is run, a file named buildConsolelog. txt will be stored in the home directory of your project.

How do I send an email notification if retry build is failed in Jenkins?

Select “Configure”. Select “Add post-build action” and Click “E-Mail Notification”. Enter your recipients mail address and select first option “Send e-mail for every unstable build”. Click “Save” button.


2 Answers

If you install the email extension plugin found here https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin then you can do this with another parameter. That looks like this:

emailext attachLog: true, body: '', subject: ''

That will attach the logs as a txt file attachment to your emails, which is the method I personally use.

Or, alternatively, if you don't mind "sending the console logs" to mean "sending a URL to the console logs", you can do something similar to what jozefow recommended and change the body to...

body: "Please go to ${env.BUILD_URL}/consoleText for more details. ");
like image 129
Spencer Malone Avatar answered Oct 19 '22 21:10

Spencer Malone


You can download console logs and attach it to email. Get logs from current build by running command: wget ${env.BUILD_URL}/consoleText -O console_text.txt

like image 2
jozefow Avatar answered Oct 19 '22 20:10

jozefow