Certified Jenkins Engineer

Jenkins Administration and Monitoring Part 1

Demo Send Slack Notification

In this guide, you’ll enhance the Jenkinsfile in the solar-system repository to send Slack notifications based on build outcomes. By the end, you’ll have a reusable Groovy helper and a DRY pipeline that alerts your team of successes, instabilities, and failures.

1. Create a Feature Branch

Start by branching off from main:

git checkout -b feature/enabling-slack
Switched to a new branch 'feature/enabling-slack'

2. Generate the Slack Step

Open Pipeline Syntax in Jenkins:

  1. Select Slack Send.
  2. Configure your channel (e.g., #dasher-notifications).
  3. Set a default message and a hex color for “Build Started.”

The image shows a Jenkins Pipeline Syntax page where a user is configuring a Slack message to be sent to a specific channel named "dasher-notifications."

You can pick a hex code to reflect status:

The image shows a Jenkins pipeline syntax configuration screen for sending Slack messages, with fields for channel, message, and color settings. The color is set to a hex code, and there are options for advanced settings and workspace credentials.

Pipeline syntax will produce:

slackSend botUser: true,
          channel: '#dasher-notifications',
          color: '#439FE0',
          message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"

Note

Ensure you have configured your Slack App and credentials in Jenkins Manage Credentials before using slackSend.

3. Why Reuse Is Better

Adding identical slackSend calls under every stage’s post block quickly becomes unwieldy:

pipeline {
  /* ... */
  stages {
    stage('Upload - AWS S3') {
      steps { /* ... */ }
      post {
        success {
          slackSend botUser: true,
                    channel: '#dasher-notifications',
                    color: '#47ec05',
                    message: "Upload S3 succeeded: ${env.BUILD_NUMBER}"
        }
        failure {
          slackSend botUser: true,
                    channel: '#dasher-notifications',
                    color: '#ec2805',
                    message: "Upload S3 failed: ${env.BUILD_NUMBER}"
        }
      }
    }
    // more stages...
  }
}

The image shows a Jenkins interface with options for setting post-stage or build conditions, including checkboxes for various build statuses.

4. Define a Reusable Slack Method

At the top of your Jenkinsfile, add:

def slackNotification(String buildStatus = 'STARTED') {
    buildStatus = buildStatus ?: 'SUCCESS'

    // Map status to color
    def color = buildStatus == 'SUCCESS'  ? '#47ec05' :
                buildStatus == 'UNSTABLE' ? '#d5ee0d' :
                                            '#ec2805'

    // Construct message
    def msg = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}\n${env.BUILD_URL}"
    slackSend color: color, message: msg
}

Build Status Color Mapping

Build StatusHex Color
SUCCESS#47ec05
UNSTABLE#d5ee0d
FAILURE#ec2805

5. Update the Pipeline

Use a single post { always { ... } } block:

pipeline {
    agent any
    tools {
        // e.g., nodejs '12.x'
    }
    stages {
        stage('Installing Dependencies') {
            steps {
                sh 'npm install --no-audit'
            }
        }
        stage('Code Coverage') {
            steps {
                catchError(buildResult: 'SUCCESS', message: 'Coverage step failed but allowed') {
                    sh 'npm run coverage'
                }
            }
        }
        // additional stages...
    }
    post {
        always {
            slackNotification(currentBuild.result)
        }
        cleanup {
            sh 'rm -rf solar-system-gitops-argocd'
        }
        junit allowEmptyResults: true, testResults: 'test-results.xml'
    }
}

Commit and push:

git add Jenkinsfile
git commit -m "feat: enable Slack notifications"
git push -u origin feature/enabling-slack

Trigger a build to see the consolidated Slack notification:

The image shows a Jenkins pipeline interface for a project named "solar-system," displaying various stages of the build process, including dependency scanning, unit testing, and deployment. Each stage is marked with a status indicator, and the integration testing stage is highlighted.

6. Demonstrate Failure

Add a failing step to confirm a red alert:

pipeline {
    agent any
    stages {
        stage('Fail Fast') {
            steps {
                sh 'echo "Simulating failure"; exit 1'
            }
        }
    }
    post {
        always {
            slackNotification(currentBuild.result)
        }
    }
}

Push and rebuild. The Slack notification color should switch to red on failure.

Warning

Never hardcode sensitive tokens in your Jenkinsfile. Use Jenkins Credentials Binding for secure handling.

7. Next Steps

To reuse slackNotification across multiple repositories, extract it into a Shared Library. This centralizes common pipeline logic and keeps your Jenkinsfile lean.


Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demo Slack Notification Setup