DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From DLT to Lakeflow Declarative Pipelines: A Practical Migration Playbook
  • Automating FastAPI Deployments With a GitHub Actions Pipeline
  • Building Data Pipelines With Jira API
  • Building a Scalable ML Pipeline and API in AWS

Trending

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • What Is Plagiarism? How to Avoid It and Cite Sources
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Set Up GitLab Notifications in Telegram: A Comprehensive Tutorial

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.

By 
Vasilii Kulazhenkov user avatar
Vasilii Kulazhenkov
·
Jul. 17, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.7K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

  1. Create a Telegram bot using BotFather and obtain the API token for the bot.
  2. Add the bot to the Telegram group and find out Telegram group Id.
  3. Add variables for the Telegram bot in GitLab.
  4. 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 @BotFather
  • TELEGRAM_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:

successful

unsuccessful

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.

API GitLab Pipeline (software) Software deployment

Opinions expressed by DZone contributors are their own.

Related

  • From DLT to Lakeflow Declarative Pipelines: A Practical Migration Playbook
  • Automating FastAPI Deployments With a GitHub Actions Pipeline
  • Building Data Pipelines With Jira API
  • Building a Scalable ML Pipeline and API in AWS

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook