[DroneCI/Slack] How to Send CI Build Notification with Slack (Step by Step Tutorial)
Today let’s have a look at how to integrate Slack to your CI pipeline. The CI platform we are using is DroneCI, which is a self-service Continuous Integration platform for busy development teams.
1. Benefits
Well, there are many benefits to integrate Slack to your CI pipeline. Here are some of them:
- You can get notified when the CI pipeline is running, failed or succeeded.
- The notification will be sent to the Slack channel you specified. Therefore, you can easily track the CI pipeline status in the Slack channel.
- You can also get notified when someone pushes the code to the repository. This is very useful when you are working in a team. You can get notified when your team member pushes the code to the repository.
- Slack is a very popular communication tool. Your team is probably using it. Therefore, it’s very convenient to integrate Slack to your CI pipeline.
2. Prerequisites
- You have a Slack account.
- You have a DroneCI account.
3. Steps
3.1 Create Slack Channel
- Create channel for receiving the notification. Give a name, for example,
#ci-pipeline
. Remember the name, we will use it later. - Specify the channel visibility. In this example, we will make it private. Then click
Create
.
3.2 Create Slack App
Make sure you have the permission to create Slack App. If not, please contact your Slack administrator.
- Go to https://api.slack.com/apps and click
Create New App
. - Enter the
App Name
and select theWorkspace
. Then clickCreate App
. - Click
Incoming Webhooks
underAdd features and functionality
section. - Click
Activate Incoming Webhooks
. And then clickAdd New Webhook to Workspace
. - Select the channel you want to send the notification to. Then click
Allow
. - Copy the
Webhook URL
. We will use it later.
3.3 Add Slack Step in DroneCI Pipeline
There is a slack plugin for DroneCI. We will use it to send the notification to Slack channel. You can find a bunch of examples and the documentation here. I will attach the example
1- name: slack
2 image: plugins/slack
3 settings:
4 webhook: https://hooks.slack.com/services/...
5 channel: ci-pipeline
6 link_names: true
7 username: xxx
8 template: |
9 {{#success build.status}}
10 build {{build.number}} succeeded. Good job.
11 {{else}}
12 build {{build.number}} failed. Fix me please.
13 {{/success}}
14 repo: {{repo.name}} - {{build.branch}}
15 version: 1.0.0
16 job url: {{build.link}}
17 when:
18 status:
19 - failure
20 - success
21 - changed
22 event:
23 - push
24 - pull_request
💡Line 4: Webhook URL we got previously.
💡Line 5: Channel name. In this example, it is ci-pipeline
.
💡Line 7: Username of DroneCI.
💡Line 8-16: The message content that will be displayed in Slack channel.
💡Line 17: The condition of triggering the step.
After inserting the above into your DroneCI pipeline, you will get the notification in your Slack channel when the pipeline is running, failed or succeeded. Here is an example of the notification.
If you want to know more about DroneCI, please check out my other posts listed below.
If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!