如果构建失败,如何在詹金斯中添加电子邮件通知

时间:2019-04-11 10:14:51

标签: jenkins jenkins-pipeline

如果构建失败,我需要在jenkins中为自由式和管道作业添加电子邮件通知

2 个答案:

答案 0 :(得分:1)

reg。 Email-ext插件

enter image description here

在管道作业中,您可以使用构建后动作/尝试以适当的步骤捕获-参考。到mail

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 {
            echo 'This will run only if failed'
        }
        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'
        }
    }
}

或尝试捕获(脚本方式)

try{
//code to handle
  } catch (e) {   
          emailext (
            from: 'sender@domain.com',
            to: 'recepient@domain.com',
            subject: "job failed- ${env.JOB_NAME}, Build #${env.BUILD_NUMBER}, FAILED",
            attachLog: true,
body: """
Foooooo text 

For current build refer to: ${env.BUILD_URL}
job: ${env.JOB_NAME}
build number: #${env.BUILD_NUMBER}

With ERROR:
${e.message}
For full log refer to 
${env.BUILD_URL}
"""
          )
        throw e
   }      

答案 1 :(得分:0)

构建后操作>电子邮件通知

Mailer插件的一部分。