In my last post I presented Drone, an extremely light CI/CD server.
One cool and satisfying thing is to be automatically notified of the output of your pipelines. In a company, you would probably use a Slack or HipChat bot. For a personal use, I think a Telegram bot is a good idea.
Let’s setup one!
Creating a Telegram bot
Setting up a bot is free and actually very easy. You can do everything from a Telegram client.
See the bots documentation. TL;DR, send a message to @botfather and follow what it says. Choose a handle, a name and a profile picture.
Then, save the token it will give you, e.g.:
Here is the token for bot Drone CI/CD @angristan_drone_bot…
Once the bot is created, open a conversation with it using its handle and send /start
. Otherwise, the bot won’t be able to contact you afterwards.
Next, you will need your Telegram chat ID (which is not your username).
There are multiple ways to do this, including a number of bots. You can use @get_id_bot.
Setting up a Telegram pipeline
We will use the drone-telegram plugin from appleboy.
Here is a basic pipeline:
pipeline:
telegram-notification:
image: appleboy/drone-telegram
secrets: [telegram_token, telegram_to]
You will need to add secrets to your Drone repo. You can do it using the drone-cli:
drone secret add -repository user/repo -name telegram_to -value "..."
drone secret add -repository user/repo -name telegram_token -value "..."
telegram_to
being your chat ID and telegram_token
being your bot’s token.
You will now receive messages upon builds:
We can make the messages look better. Here is my usual pipeline:
pipeline:
telegram-notification:
image: appleboy/drone-telegram
secrets: [telegram_token, telegram_to]
when:
status: [success, failure]
format: markdown
message: >
{{#success build.status}}
✅ Build #{{build.number}} of `{{repo.name}}` succeeded.
📝 Commit by {{commit.author}} on `{{commit.branch}}`:
```
{{commit.message}}
```
🌐 {{ build.link }}
{{else}}
❌ Build #{{build.number}} of `{{repo.name}}` failed.
📝 Commit by {{commit.author}} on `{{commit.branch}}`:
```
{{commit.message}}
```
🌐 {{ build.link }}
{{/success}}
I know, it looks a bit awful. I mean, Markdown inside YAML… But the result is cool!
By default, the pipeline will not be executed if prior pipelines fail. We want to receive a notification regardless of what happened, so this does the trick:
when:
status: [success, failure]
As you can see in the plugin documentation, you add all sorts of attachments and use quite a lot of variables and functions inside of your messages. Enjoy!