How to Set Up GitLab Notifications in Telegram: A Comprehensive Tutorial
This tutorial provides a step-by-step guide on setting up GitLab notifications in Telegram. Ensuring you stay informed about your GitLab activities conveniently.
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will look at how to configure GitLab notifications in Telegram to receive information about successful deployment and failures during the development phase. This is especially useful for developers, DevOps engineers, and PMs, as it allows you to stay up to date on GitLab activity and respond immediately to crashes.
Let’s get started with what we want to do step-by-step:
- Create a Telegram bot using BotFather and obtain the API token for the bot.
- Add the bot to the Telegram group and find out Telegram group Id.
- Add variables for the Telegram bot in GitLab.
- Let's set up sending messages via GitLab Pipeline.
1. Create a Telegram Bot Using BotFather and Obtain the API Token for the Bot
Open the Telegram app and find @BotFather. Then follow the instructions to create a new bot and get an API token. Find instructions for this here. Create a bot, for example, with the name 'CicdBot.' After creating the bot, a token will arrive in the chat to work with the API, something like 123281243:AAHdqTcvCH1vGWJxfSeofSAjhs6PALDsaw
.
In the bot settings, disable the privacy settings:
Privacy mode is disabled for Cicdbot.
2. Add the Bot to the Telegram Group and Find Out the Telegram Group ID
Go to the Telegram group for notification from GitLab and add the created bot as a participant (find the bot by name in BotFather chat).
After that, you need to get the Telegram group ID. To do this, go to the URL.
Where <YourBOTToken> — is the API Token received in BotFather 123281243:AAHdqTcvCH1vGWJxfSeofSAjhs6PALDsaw
.
And find the chat-id in the response: ... "chat": {"id": <YourGroupID>, ...
3. Add Variables for the Telegram Bot in GitLab
In the settings of the required Gitlab repository (Settings > CI/CD > Secret variables), add secret variables:
TELEGRAM_BOT_TOKEN
— API Token received in @BotFatherTELEGRAM_CHAT_ID
— ID of the chat to which the message will be sent. (It will be sent only if any message has already been sent to the bot from this chat. For a group, it is enough to add a bot to the group).
4. Let’s Set Up Sending Messages via GitLab Pipeline
To conveniently send notifications, we will create a script in notify.sh, in which we will access the Telegram API and send a message to the right user through the created bot:
#!/bin/bash
TIME="10"
URL="https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage"
TEXT="Deploy status: $1%0A%0AProject:+$CI_PROJECT_NAME%0AURL:+$CI_PROJECT_URL/pipelines/$CI_PIPELINE_ID/%0ABranch:+$CI_COMMIT_REF_SLUG%0AUser:+$GITLAB_USER_NAME"
curl -s --max-time $TIME -d "chat_id=$TELEGRAM_CHAT_ID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
The deployment status should be passed as the first parameter for this script. Also transmitted are the Project name, URL pipeline, Branch, and User name. In .gitlab-ci.yml, we describe calling this script at the right time with the right parameters:
...
deploy:
stage: deploy
script:
- sh notify.sh "success message ✅"
notify_error:
stage: notify
script:
- sh notify.sh "fail message ❌"
when: on_failure
I used Emojis to indicate the status of the deployment. If the deployment is successful ✅, the script with the desired parameter is simply called. In case of an unsuccessful deployment ❌, “management moves” to the next stage, in which notification of an unsuccessful deployment occurs.
This is how it looks in a telegram:
Conclusion
By configuring GitLab notifications in Telegram for successful deployment and failures at the development stage, company employees can be aware of GitLab activity in real time. This makes it easier to track pipeline statuses and respond immediately to problems that have arisen.
Opinions expressed by DZone contributors are their own.
Comments