0
0
Jenkinsdevops~5 mins

Custom notification logic in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want Jenkins to send messages only when certain things happen, like a build failing or succeeding. Custom notification logic lets you control exactly when and how Jenkins sends these messages to your team.
When you want to send a Slack message only if the build fails.
When you want to email the team only if tests fail but skip notifications on success.
When you want to notify different people depending on the branch being built.
When you want to send a summary notification after multiple jobs finish.
When you want to avoid spamming notifications for repeated failures.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
        // Simulate build step
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
        // Simulate test step
      }
    }
  }
  post {
    success {
      script {
        if (env.BRANCH_NAME == 'main') {
          slackSend(channel: '#builds', message: "Build succeeded on main branch: ${env.JOB_NAME} #${env.BUILD_NUMBER}")
        }
      }
    }
    failure {
      slackSend(channel: '#alerts', message: "Build failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}")
    }
  }
}

This Jenkinsfile defines a pipeline with build and test stages.

The post section controls notifications:

  • success: Sends a Slack message only if the build succeeds on the main branch.
  • failure: Sends a Slack message to alert channel if the build fails.

This way, notifications are customized based on build result and branch.

Commands
This command updates the Jenkins job configuration with the new Jenkinsfile containing custom notification logic.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
Starts a build of the Jenkins pipeline to test the notification logic in action.
Terminal
jenkins build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] slackSend Sending Slack message to #builds [Pipeline] End of Pipeline Finished: SUCCESS
Simulates a build failure to verify that failure notifications are sent correctly.
Terminal
jenkins build my-pipeline --simulate-failure
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] slackSend Sending Slack message to #alerts [Pipeline] End of Pipeline Finished: FAILURE
Key Concept

If you remember nothing else from this pattern, remember: use the post section in Jenkinsfile to run notification steps conditionally based on build results and environment variables.

Common Mistakes
Placing notification steps inside the main stages instead of the post section.
Notifications may run even if the build fails early or skip important conditions.
Use the post section with success, failure, or always blocks to control notifications reliably.
Hardcoding notification channels without checking branch or build context.
Leads to irrelevant notifications and spamming the wrong teams.
Use environment variables like BRANCH_NAME to customize notification targets.
Not testing notification logic with both success and failure builds.
You might miss bugs where notifications never send or send incorrectly.
Run builds simulating success and failure to verify all notification paths.
Summary
Use the post section in Jenkinsfile to add custom notification logic after build stages.
Send notifications conditionally based on build status like success or failure.
Use environment variables to customize who gets notified and when.