Notifications

Send build results to Slack, email, or other channels so your team knows the moment something breaks.

SyntaxslackSend channel: '#builds', message: "Build ${env.BUILD_NUMBER} passed"

A pipeline should tell people when things pass or fail. Notifications close the feedback loop.

Common channels

  • Slack — the Slack Notification plugin posts to a channel with slackSend.
  • Email — the Email Extension plugin sends rich emails with emailext.
  • Microsoft Teams, Discord — via their plugins.

Where to send them

Put notification steps in the post section so they react to the final result — success, failure, or unstable.

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Build') {
            steps { sh 'make build' }
        }
    }
    post {
        success {
            slackSend channel: '#ci', color: 'good',
                message: "SUCCESS: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
        }
        failure {
            slackSend channel: '#ci', color: 'danger',
                message: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER} <${env.BUILD_URL}|View>"
        }
    }
}

When to use it

  • A Slack notification fires only when a previously passing build starts failing so the team is alerted without being spammed on every run.
  • An email notification is sent with the build log attached whenever the nightly regression suite fails.
  • A Microsoft Teams webhook receives a card message with the job name, build number, and a direct link when any production deploy fails.

More examples

Slack notify on state change

Sends a red alert on failure and a green recovery message when the build returns to passing.

Example · groovy
post {
  failure {
    slackSend channel: '#ci-alerts',
              color: 'danger',
              message: "FAILED: ${JOB_NAME} #${BUILD_NUMBER}\n${BUILD_URL}"
  }
  fixed {
    slackSend channel: '#ci-alerts',
              color: 'good',
              message: "RECOVERED: ${JOB_NAME} #${BUILD_NUMBER}"
  }
}

Email notification via Mailer plugin

Sends an email to the team with the console URL when the pipeline fails.

Example · groovy
post {
  failure {
    mail to: '[email protected]',
         subject: "BUILD FAILED: ${JOB_NAME} #${BUILD_NUMBER}",
         body: "Check the console output:\n${BUILD_URL}console"
  }
}

Teams webhook via curl

Posts a failure notification to a Microsoft Teams channel via an incoming webhook URL stored as a credential.

Example · groovy
post {
  failure {
    withCredentials([string(credentialsId: 'teams-webhook-url', variable: 'TEAMS_URL')]) {
      sh """
        curl -H 'Content-Type: application/json' \\
          -d '{"text":"FAILED: ${JOB_NAME} #${BUILD_NUMBER} ${BUILD_URL}"}' \\
          $TEAMS_URL
      """
    }
  }
}

Discussion

  • Be the first to comment on this lesson.