Skip to content

Jenkins - Notification

Jenkins provides multiple ways to notify team members about build outcomes, crucial for maintaining awareness in a CI/CD workflow.

Jenkins has built-in email notification capabilities.

Step 1: Configure SMTP Server. Go to Manage Jenkins → System. Scroll down to the ‘Jenkins Location’ section and ensure the ‘Jenkins URL’ and ‘System Admin e-mail address’ are correctly set. Then, find the ‘E-mail Notification’ section. Enter your SMTP server details (hostname, port, credentials if required) and test the configuration.

Step 2: Configure Recipients in Job. In your job configuration (Pipeline or Freestyle), add a post-build action. For Freestyle jobs, select ‘E-mail Notification’. For Pipeline jobs, use the mail step within a post block. Specify recipients and choose whether to send emails for failed, unstable, or successful builds.

For more control and integration with modern communication platforms, plugins are highly recommended.

The ‘Email Extension’ plugin (emailext) offers significant improvements over basic email notifications. It allows for customizable email templates (HTML, text), granular triggers (e.g., send email only on the first failure), configurable recipient lists (developers who committed changes, specific users/groups), and adding build logs or artifacts to emails. Configure its global settings in Manage Jenkins → System and use its specific post-build action (Freestyle) or the emailext step (Pipeline).

Plugins are available for popular team chat applications:

  • Slack: The ‘Slack Notification’ plugin allows sending detailed build status messages to Slack channels or users. Configure it in Manage Jenkins → System and use the slackSend step in your Pipeline.
  • Microsoft Teams: Similarly, the ‘Microsoft Teams Connector’ plugin enables notifications to Teams channels via webhooks. Configure a connector in Teams, add the webhook URL in Jenkins (Manage Jenkins -> System), and use the microsoftTeamsSend step in Pipeline.

For custom integrations, you can use plugins like ‘HTTP Request’ to send POST requests with build data (JSON/XML payload) to any webhook endpoint upon build completion.

Pipeline Notification Example (Declarative)

Section titled “Pipeline Notification Example (Declarative)”
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
// Your build steps here
}
}
stage('Test') {
steps {
echo 'Testing...'
// Your test steps here
sh 'exit 1' // Simulate failure for example
}
}
}
post {
always {
echo 'This will always run'
// Example: Clean up workspace
}
success {
echo 'Build successful!'
slackSend channel: '#build-status', message: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})"
}
failure {
echo 'Build failed!'
mail to: 'dev-team@example.com',
subject: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: "Check console output: ${env.BUILD_URL}"
slackSend channel: '#build-alerts', color: 'danger', message: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})"
}
unstable {
echo 'Build unstable (tests failed).'
mail to: 'qa-team@example.com',
subject: "UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: "Tests failed. Check results: ${env.BUILD_URL}"
}
}
}

This example shows how to use the post section in a Declarative Pipeline to send different notifications based on the build status, integrating basic email and Slack.