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

最后都变了- 提交于 2019-11-27 04:44:01

问题


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
 } 

回答1:


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: "example@example.com",
            sendToIndividuals: true])
        }
      }
    }
  }
}



回答2:


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}"
}



回答3:


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
}


来源:https://stackoverflow.com/questions/36948606/jenkins-notifying-error-occured-in-different-steps-by-sending-mail-in-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!