Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins notifying error occured in different steps by sending mail in Pipeline (former known as Workflow)

I have a pipeline with multiple steps, for example:

stage 'dev - compile'
node('master') {
  //do something
}

stage 'test- compile'
node('master') {
    //do something
}

stage 'prod- compile'
node('master') {
    //do something
}

I want to send a email if something goes wrong in the job, how can I send an email no matter where the error got triggered, something like:

try {
 /** 
  all the code above
  **/
 } catch(Exception e) { 
     mail the error
 } 
like image 757
Daniel Hernández Avatar asked Apr 29 '16 22:04

Daniel Hernández


People also ask

How do I get error messages in Jenkins pipeline?

In Jenkins, in the pipeline where failure occurred, in the pane, select the latest build, and click Console Output. On the Console Output page, check the logs to find the reason for the failure. If required, update the parameters in the deployment input configuration file.

What is Jenkins workflow?

Jenkins is an open source continuous integration/continuous delivery and deployment (CI/CD) automation software DevOps tool written in the Java programming language. It is used to implement CI/CD workflows, called pipelines.

How can Jenkins notify in case of a build failure?

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”.


3 Answers

I think it is a better way to use the jenkins build in post section instead of using try catch:

pipeline {
  agent any
    stages {
      stage('whatever') {
        steps {
          ...
        }
      }
    }
    post {
        always {
          step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "[email protected]",
            sendToIndividuals: true])
        }
      }
    }
  }
}
like image 55
Andi Avatar answered Oct 25 '22 05:10

Andi


What I did to include useful information in my mail about the failure:

try {
    stage 'checkout cvs'
    node('master') {
        /** CODE **/
    }

    stage 'compile'
    node('master') {
        /** CODE **/
    }

    stage 'test unit'
    node('master') {
        /** CODE **/
    }   

    stage 'package'
    node('master') {
        /** CODE **/
    }

    stage 'nexus publish'
    node('master') {
        /** CODE **/
    }

    stage 'Deploy to App Server'
    node('master') {
        /** CODE **/            
    }

} catch(e) {
    String error = "${e}";
    // Make the string with job info, example:
    // ${env.JOB_NAME}
    // ${env.BUILD_NUMBER} 
    // ${env.BUILD_URL}
    // and other variables in the code
    mail bcc: '',
         cc: '',
         charset: 'UTF-8',
         from: '',
         mimeType: 'text/html',
         replyTo: '',
         subject: "ERROR CI: Project name -> ${env.JOB_NAME}",
         to: "${mails_to_notify}",
         body: "<b>${pivote}</b><br>\n\nMensaje de error: ${error}\n\n<br>Projecto: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}";
    error "${error}"
}
like image 21
Daniel Hernández Avatar answered Oct 25 '22 05:10

Daniel Hernández


Well, your idea is absolutely correct, you just need to move mail after the catch block or use finally. Examples (in pseudocode):

try {
    //code
    email = 'success'
} catch(Exception e) { 
    // error handler: logging
    email = 'failure'
}
send email

Or the same approach using the catchError pipeline built-in:

result = 'failure'
catchError { // this catches all exceptions and set the build result
    //code
    result = 'success' // we will reach this point only if no exception was thrown
}
send result 

Or using finally:

try {
    //code
} finally { 
    send email
}
like image 36
izzekil Avatar answered Oct 25 '22 03:10

izzekil