发送有关jenkins管道故障的电子邮件

时间:2016-09-27 08:51:40

标签: jenkins jenkins-pipeline jenkins-email-ext

如何在jenkins管道中添加一个旧式的后期构建任务,该任务在构建失败时发送电子邮件?我找不到"构建后的行动"在管道的GUI中。我知道我可以包装整个构建脚本try / catch,但是当构建脚本很大时这看起来很难看,并且即使手动中止作业也会继续发送电子邮件。我希望获得与基于pre email-ext的构建后操作相同的功能。

try {
    // Do sth
} catch(e) {
    emailext body: '$DEFAULT_CONTENT', 
        recipientProviders: [
            [$class: 'CulpritsRecipientProvider'],
            [$class: 'DevelopersRecipientProvider'],
            [$class: 'RequesterRecipientProvider']
        ], 
        replyTo: '$DEFAULT_REPLYTO', 
        subject: '$DEFAULT_SUBJECT',
        to: '$DEFAULT_RECIPIENTS'
    throw err
}

7 个答案:

答案 0 :(得分:21)

这个答案适用于我的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'  
         }  
     }  
 }

答案 1 :(得分:15)

目前,您必须将此过程与try - catch一起使用才能获得构建后的操作。

但是将来这个步骤是有计划的(实际上是在审查中)。您可以阅读the blog post以获取更多信息(请参阅声明性管道部分)。另请参阅the Jenkins Jira ticketthe pull request

更新

Jenkins声明性管道现在有post部分,它将在每次构建结束时有条件地运行步骤。 See the docs了解更多信息。

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 :)"'
 }
}

答案 2 :(得分:13)

仅在构建状态发生变化时才采取行动,您可以使用post > changed block

为了检查,状态已更改为什么状态,您可以结合使用script block检查currentBuild.currentResult属性的值。

像这样:

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

}

答案 3 :(得分:0)

这在具有声明式管道的Jenkins(当前版本2.164.2和emailext)上运行良好:

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

您可以控制电子邮件的构建失败或状态更改。另外,我已将构建日志放入电子邮件正文中

答案 4 :(得分:0)

DRY您的脚本化管道,您可以轻松extend Jenkins with your own shared library并定义自定义步骤 emailOnError

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

然后像使用它

emailOnError() {
    // Do sth   
}

答案 5 :(得分:0)

以上所有答案几乎都是正确的,但是构建失败后,我将在帖子部分使用此答案。也许这可能有用

post {
always {
  script {
    if (currentBuild.currentResult == 'FAILURE') {
      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "abc.xyz@blabl.com, sendToIndividuals: true])
    }
  }
}

答案 6 :(得分:0)

如何使用 groovy 脚本在 Jenkins 管道中发送电子邮件通知,

在当前的市场场景中,devops 和自动化 QA 之间的界限非常细,因此使用 groovy 学习流水线脚本非常重要。

https://automationreinvented.blogspot.com/2021/06/how-to-send-email-notification-in.html?m=1