Send email on jenkins pipeline failure

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

    For taking action only when build status has changed you can use the post > changed block.

    And for checking, what state the status has changed to you can use the script block in combination with checking the value of currentBuild.currentResult property.

    Like so:

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

提交回复
热议问题