drone,

Send Drone CI build notifications with Slack

Oct 04, 2021 · 3 mins read · Post a comment
Send Drone CI build notifications with Slack

The following post will be about sending Drone CI build notification to Slack. In case you didn’t know already, since there are 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.
Drone slack channel

Step 3. Change the settings to your liking, scroll down the page, and copy the Webhook URL under Integration Settings.
Drone slack webhook

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 an 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, if you are using .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: 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:
Drone slack notification

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.