Drone CI can send build notifications to Slack channels.
Here’s a basic Slack notification setup in Drone:
kind: pipeline
type: docker
name: default
steps:
- name: build
image: plugins/docker
settings:
repo: your-dockerhub-username/your-repo
username:
from_secret: docker_username
password:
from_secret: docker_password
- name: notify
image: plugins/slack
settings:
hook_url:
from_secret: slack_webhook_url
channel: build-notifications
template: |
Build {{ .Build.Number }} for {{ .Repo.Owner }}/{{ .Repo.Name }} has {{ .Build.Status }}!
Commit: {{ .Commit.Message }}
Author: {{ .Commit.Author }}
Branch: {{ .Branch }}
Link: {{ .Build.Link }}
when:
status: [success, failure]
This pipeline first builds and pushes a Docker image, then uses the plugins/slack step to send a notification. The settings define the Slack webhook URL (fetched from a secret), the target channel, and a template for the message content. The when.status clause ensures notifications are sent only on build success or failure, not on cancellation or pending states.
Let’s look at a real-world scenario. Imagine a team developing a web application. They use Drone CI to automatically build and deploy their application on every push to their main branch. They want to be immediately informed in their #developers Slack channel whenever a build succeeds or fails, so they can react quickly.
The plugins/slack step uses a webhook URL. This URL is a unique endpoint provided by Slack that allows external applications to post messages into a specific channel. You create this webhook by going to your Slack workspace’s app directory, searching for "Incoming WebHooks," and configuring a new one. This will give you a URL that looks something like https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX.
The template uses Go’s text/template syntax. Drone injects various pieces of context about the build into this template. You can access details like:
.Build.Number: The sequential build number..Build.Status: The status of the build (e.g.,success,failure,pending,canceled)..Repo.Owner: The owner of the repository..Repo.Name: The name of the repository..Commit.Message: The commit message..Commit.Author: The author of the commit..Branch: The branch the build is running on..Build.Link: A direct URL to the build in Drone.
You can customize the template extensively. For instance, to include a link to the specific commit on GitHub:
template: |
Build {{ .Build.Number }} for {{ .Repo.Owner }}/{{ .Repo.Name }} has {{ .Build.Status }}!
Commit: <{{ .Commit.Link }}|{{ .Commit.Message }}>
Author: {{ .Commit.Author }}
Branch: {{ .Branch }}
This uses Slack’s mrkdwn formatting to make the commit message a clickable link to the commit.
The when clause is crucial for controlling when notifications are sent. Without it, you’d get a notification for every build state change, which can be noisy. Common conditions include status, branch, event (e.g., push, pull_request), and environment.
Consider a more advanced notification that includes a "Deploy" button if the build is successful:
- name: notify
image: plugins/slack
settings:
hook_url:
from_secret: slack_webhook_url
channel: build-notifications
template: |
{{ if eq .Build.Status "success" }}
:large_green_circle: Build {{ .Build.Number }} for {{ .Repo.Owner }}/{{ .Repo.Name }} succeeded!
Commit: {{ .Commit.Message }}
Author: {{ .Commit.Author }}
Branch: {{ .Branch }}
Link: {{ .Build.Link }}
{{ else }}
:red_circle: Build {{ .Build.Number }} for {{ .Repo.Owner }}/{{ .Repo.Name }} failed!
Commit: {{ .Commit.Message }}
Author: {{ .Commit.Author }}
Branch: {{ .Branch }}
Link: {{ .Build.Link }}
{{ end }}
actions:
- name: deploy
text: Deploy
type: button
url: http://your-deploy-app.com/deploy?build={{ .Build.Number }}&repo={{ .Repo.Name }}
style: primary
enabled: "{{ eq .Build.Status \"success\" }}"
when:
status: [success, failure]
This example uses if/else within the template to change the emoji and text based on build status. It also introduces actions to create interactive Slack messages. The deploy action is a button that, when clicked, sends a request to an external deployment application. The enabled field uses a template expression to conditionally show the button only on successful builds. The url for the action also uses template variables to pass context about the build to the deployment service.
The plugins/slack plugin is a wrapper around Slack’s Incoming Webhooks API. When Drone executes this step, it constructs a JSON payload containing the rendered template and any actions, and POSTs it to the hook_url. Slack then processes this payload and displays the message in the configured channel. The actions feature leverages Slack’s Block Kit UI elements, allowing for richer, interactive messages beyond simple text.
If you need to send notifications to multiple channels or with different message formats based on certain conditions (e.g., different messages for staging vs. production deployments), you can define multiple notify steps, each with its own when clause and settings.
The next thing you’ll likely want to explore is integrating with other notification services or building more complex notification logic, perhaps involving conditional notifications based on commit tags or specific file changes.