Search code examples
jenkinsgroovyjenkins-pipelineslack

How to send Slack notification after a Jenkins pipeline build failed?


I have a pipeline groovy script in Jenkins v2.19. Also I have a
"Slack Notification Plugin" v2.0.1 and "Groovy Postbuild Plugin" installed.

I can successfully send "build started" and "build finished" messages.

When a build fails, how can I send the "Build failed" message to a Slack channel?


Solution

  • Just in case if in Declarative Syntax,

    Now, Jenkins provides post. You can check result at the end of pipeline.

    https://jenkins.io/doc/book/pipeline/syntax/#post-example

    Using like:

    pipeline {
        stages { ... }
        post {
           // only triggered when blue or green sign
           success {
               slackSend ...
           }
           // triggered when red sign
           failure {
               slackSend ...
           }
           // trigger every-works
           always {
               slackSend ...
           }
        }
    }
    

    It would be used in every stage also. See the document link please.