Setting up Slack Notifications with Jenkins

Setting up Slack notifications for build results with Jenkins is pretty simple. You’ll need the following:

  • A Jenkins server that you have admin rights to.
  • Slack Notification Plugin
  • A Slack workspace, ideally with a channel dedicated to Jenkins alerts.

Setup

To set up Slack, go to <your-workspace-name>.slack.com/apps and search for Jenkins CI:

Add the configuration and set the desired channel where alerts will appear.

After this, you’ll see some directions on setting up Slack on the Jenkins side.

When you’ve finished the setup, make sure to test the connection – you should see a message in Slack like the following:

Adding Jenkins Calls to Pipeline

Once the setup is complete, you can use the following examples to call Slack during the pipeline jobs:

stages {
  stage('Build') {
    steps {
      echo 'build'
    }
  }
  stage('Deploy') {
    steps {
      echo 'Deploy'
      slackSend color: 'good', message: "deployment successful => https://yoursite.com"
    }
  }
}
...
post {
  failure {
    slackSend color: 'danger', message: "deployment failed (<${env.BUILD_URL}|Open>)"
  }
  always {
    cleanWs()
  }
}

This example will call a successful message when all stages complete, and sends a failure message if the build as a whole fails.

Leave a Reply

Your email address will not be published. Required fields are marked *