Send email on jenkins pipeline failure

前端 未结 6 1485
不知归路
不知归路 2021-01-30 17:16

How can i add to a jenkins pipeline an old-style post-build task which sends email when the build fails? I cannot find \"Post-build actions\" in the GUI for a pipeline. I know t

相关标签:
6条回答
  • 2021-01-30 17:40

    to DRY your scripted pipelines you can easily extend Jenkins with your own shared library and define a custom step emailOnError like

    def call(Closure action) {
        try {
            action()
        } catch(e){
            emailext    body: '$DEFAULT_CONTENT', 
                        recipientProviders: [
                            [$class: 'DevelopersRecipientProvider'],
                            [$class: 'RequesterRecipientProvider']
                        ], 
                        replyTo: '$DEFAULT_REPLYTO', 
                        subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - ERROR!',
                        to: '$DEFAULT_RECIPIENTS'
            throw err
        }   
    }
    

    then use it like

    emailOnError() {
        // Do sth   
    }
    
    0 讨论(0)
  • 2021-01-30 17:47

    This is working well on my Jenkins (current ver 2.164.2 and emailext) with declarative pipeline:

    pipeline {
    ...
    
    environment {
            EMAIL_TO = 'someone@host.com'
        }
    post {
            failure {
                emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n -------------------------------------------------- \n${BUILD_LOG, maxLines=100, escapeHtml=false}', 
                        to: "${EMAIL_TO}", 
                        subject: 'Build failed in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
            }
            unstable {
                emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n -------------------------------------------------- \n${BUILD_LOG, maxLines=100, escapeHtml=false}', 
                        to: "${EMAIL_TO}", 
                        subject: 'Unstable build in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
            }
            changed {
                emailext body: 'Check console output at $BUILD_URL to view the results.', 
                        to: "${EMAIL_TO}", 
                        subject: 'Jenkins build is back to normal: $PROJECT_NAME - #$BUILD_NUMBER'
            }
        }
    }
    

    You can control email for failed build or changed status. Also I've put build log to the email body

    0 讨论(0)
  • 2021-01-30 17:48

    At the moment you have to use this procedure with a try-catch to get a post-build action.

    But in the future this step is planned (actually it is in review). You can read the blog post for further information (see the declarative pipeline part). See also the the Jenkins Jira ticket and the pull request.

    Update:

    The Jenkins declarative pipeline now has the post section that will run steps conditionally at the end of each build. See the docs for more information.

    post {
     always {
       sh 'echo "This will always run"'
     }
     success {
      sh 'echo "This will run only if successful"'
     }
     failure {
      sh 'echo "This will run only if failed"'
     }
     unstable {
      sh 'echo "This will run only if the run was marked as unstable"'
     }
     changed {
      sh 'echo "This will run only if the state of the Pipeline has changed"'
      sh 'echo "For example, the Pipeline was previously failing but is now successful"'
      sh 'echo "... or the other way around :)"'
     }
    }
    
    0 讨论(0)
  • 2021-01-30 17:49

    For taking action only when build status has changed you can use the post > changed block.

    And for checking, what state the status has changed to you can use the script block in combination with checking the value of currentBuild.currentResult property.

    Like so:

    pipeline {
        ...
    
        post {
            changed {
                script {
                    if (currentBuild.currentResult == 'FAILURE') { // Other values: SUCCESS, UNSTABLE
                        // Send an email only if the build status has changed from green/unstable to red
                        emailext subject: '$DEFAULT_SUBJECT',
                            body: '$DEFAULT_CONTENT',
                            recipientProviders: [
                                [$class: 'CulpritsRecipientProvider'],
                                [$class: 'DevelopersRecipientProvider'],
                                [$class: 'RequesterRecipientProvider']
                            ], 
                            replyTo: '$DEFAULT_REPLYTO',
                            to: '$DEFAULT_RECIPIENTS'
                    }
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-30 17:53

    Almost all above answers are correct but this one I am using in post section when the build is failed. Maybe this can be useful

    post {
    always {
      script {
        if (currentBuild.currentResult == 'FAILURE') {
          step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "abc.xyz@blabl.com, sendToIndividuals: true])
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-30 17:59

    This answer worked on my Jenkins ver. 2.96.

    Jenkins pipeline email not sent on build failure - Stack Overflow

     pipeline {  
         agent any  
         stages {  
             stage('Test') {  
                 steps {  
                     sh 'echo "Fail!"; exit 1'  
                 }  
             }  
         }  
         post {  
             always {  
                 echo 'This will always run'  
             }  
             success {  
                 echo 'This will run only if successful'  
             }  
             failure {  
                 mail bcc: '', body: "<b>Example</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo@foomail.com";  
             }  
             unstable {  
                 echo 'This will run only if the run was marked as unstable'  
             }  
             changed {  
                 echo 'This will run only if the state of the Pipeline has changed'  
                 echo 'For example, if the Pipeline was previously failing but is now successful'  
             }  
         }  
     }
    
    0 讨论(0)
提交回复
热议问题