The following post will be about sending Drone CI build notification to Slack. In case you didn’t know already, since there are a dozen of CI/CD tools out there, Drone CI is an open source, lightweight, self-hosted, Continuous Integration container-based platform, where each step is executed inside an isolated Docker container.
Prerequisites
- Drone CI pipeline
- Slack account/workspace
Solution
Step 1. Login to your Slack workspace on this link.
Step 2. Select the channel you want to send build notifications from Drone, and click the button Add Incoming WebHooks integration.
Step 3. Change the settings to your liking, scroll down the page, and copy the Webhook URL under Integration Settings.
Step 4. Now, before you open your drone pipeline file and paste the code below, I’ve decided to go with the jsonnet
scripting as a YAML
alternative, since I prefer the Jsonnet data template language, which benefits from code re-use and the Don’t Repeat Yourself (DRY) principle.
.drone.jsonnet
pipeline step:
local slack_notification(channel, webhook) = {
name: 'slack notification',
image: 'plugins/slack',
settings: {
webhook: {
from_secret: webhook,
},
channel: channel,
template: |||
{{ #if build.pull }}
*{{ #success build.status }}✔{{ else }}✘{{ /success }} {{ uppercasefirst build.status }}*: <https://github.com/{{ repo.owner }}/{{ repo.name }}/pull/{{ build.pull }}|Pull Request #{{ build.pull }}>
{{ else }}
*{{ #success build.status }}✔{{ else }}✘{{ /success }} {{ uppercasefirst build.status }}: Build #{{ build.number }}* (type: `{{ build.event }}`)
{{ /if }}
Repository: <https://github.com/{{ repo.owner }}/{{ repo.name }}|{{ repo.name }}>
Duration: {{since build.started}}
Commit: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commit/{{ build.commit }}|{{ truncate build.commit 8 }}>
Branch: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commits/{{ build.branch }}|{{ build.branch }}>
Author: {{ build.author }}
Commit message: {{ build.message }}
<{{ build.link }}|Visit build page ↗>
|||,
}
};
Or, with .drone.yml
:
steps:
...
- name: slack notification
image: plugins/slack
webhook:
from_secret: SLACK_WEBHOOK
settings:
channel: [your-slack-channel-name-here]
template: >
{{ #if build.pull }}
*{{ #success build.status }}✔{{ else }}✘{{ /success }} {{ uppercasefirst build.status }}*: <https://github.com/{{ repo.owner }}/{{ repo.name }}/pull/{{ build.pull }}|Pull Request #{{ build.pull }}>
{{ else }}
*{{ #success build.status }}✔{{ else }}✘{{ /success }} {{ uppercasefirst build.status }}: Build #{{ build.number }}* (type: `{{ build.event }}`)
{{ /if }}
Repository: <https://github.com/{{ repo.owner }}/{{ repo.name }}|{{ repo.name }}>
Duration: {{ since build.started }}
Commit: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commit/{{ build.commit }}|{{ truncate build.commit 8 }}>
Branch: <https://github.com/{{ repo.owner }}/{{ repo.name }}/commits/{{ build.branch }}|{{ build.branch }}>
Author: {{ build.author }}
Commit message: {{ build.message }}
<{{ build.link }}|Visit build page ↗>
Note(s): If the above .drone.yml
example doesn’t work, try converting Jsonnet to YAML, by using the drone jsonnet --stdout
command.
Step 5. Commit and push the changes. If the build is successful, you should get a message that looks something like this:
Conclusion
Furthermore, you could even add the Build Status as part of the README.md file of each code repository. Open any repo in Drone, click Settings, copy the Markdown text under Badges and paste it above the first header of the README.md file.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.