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

问题描述 投票:19回答:4

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

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
}
jenkins jenkins-pipeline jenkins-email-ext
4个回答
27
投票

这个答案适用于我的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: "[email protected]";  
         }  
         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'  
         }  
     }  
 }

15
投票

目前你必须使用try-catch这个程序来获得一个构建后的动作。

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

Update:

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

14
投票

只有在构建状态发生变化时才能执行操作,您可以使用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'
                }
            }
        }
    }

}

1
投票

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

    pipeline {
    ...

    environment {
            EMAIL_TO = '[email protected]'
        }
    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'
            }
        }
    }

您可以控制失败的构建或更改状态的电子邮件。我还将构建日志放到了电子邮件正文中

© www.soinside.com 2019 - 2024. All rights reserved.