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

Mar*_*mro 22 jenkins jenkins-pipeline jenkins-email-ext

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

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
}
Run Code Online (Sandbox Code Playgroud)

kuj*_*jiy 30

这个答案适用于我的Jenkins ver.2.96.

Jenkins管道电子邮件未在构建失败时发送 - 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'  
         }  
     }  
 }
Run Code Online (Sandbox Code Playgroud)

  • 什么是‘邮件’?是来自插件吗?它与“emailext”不同吗? (3认同)

ans*_*sib 16

目前,您必须使用此过程try- catch以获取构建后操作.

但是在未来这个步骤是有计划的(实际上它正在审查中).您可以阅读博客文章以获取更多信息(请参阅声明性管道部分).另请参阅Jenkins Jira票证拉取请求.

更新:

Jenkins声明性管道现在具有post将在每次构建结束时有条件地运行步骤的部分. 有关更多信息,请参阅文档.

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 :)"'
 }
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*kis 16

要仅在构建状态更改时执行操作,您可以使用post> changed块.

并且为了检查,状态已更改为什么状态,您可以结合使用脚本块和检查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'
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 8

这在我的 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'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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