Send email on jenkins pipeline failure

前端 未结 6 1488
不知归路
不知归路 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: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

提交回复
热议问题