How to Send Telegram Notifications With Google Cloud Build (GCB)
This article is a quick How-to after struggling with Google Cloud Build about how to send notifications when a build has failed or is successful.
Join the DZone community and get the full member experience.Join For Free
This article is a quick how-to after struggling with Google Cloud Build about how to send notifications when a build has failed or is successful. Since I did not want to use Slack and the heavy client, I give a try in this post with Telegram and discover the magical chatbot world.
Google Cloud Build
Cloud Build is a service that executes your builds on the Google Cloud Platform infrastructure. Cloud Build can import source code from Google Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket, execute a build to your specifications, and produce artifacts.
Google Cloud Build is a cheap alternative to Jenkins, Travis-Ci and Circle CI if you have private projects. While the other solutions require a quite expensive subscription, you pay GCB according to the used build time. If you do not commit frequently or the build is a short task, it is a fair solution. And a bonus, the first 120 build minutes are free.
There is no built-in process therefore I had to build my own. GCB recommends Slack however my choice is in favor of mobile push messages with Telegram.
Telegram is a cloud-based instant messaging and voice over IP service. Users can send messages and exchange photos, videos, stickers, audio and files of any type. Default Messages and media in Telegram are encrypted when stored on its servers.
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands, and inline requests. You control your bots using HTTPS requests to our bot API.
What can do a BOT ( extracted from Telegram website):
Get customized notifications and news.
Integrate with other services.
Accept payments from Telegram users.
Create custom tools.
Build single- and multiplayer games.
Build social services.
How to Send Notifications With GCB
Here is a diagram to explain how to implement such a notification bot.
Create a Job With GCB
The first step is to identify the GIT repository that you want to use as a demo project.
Then go into the Google Cloud Build section:
Check the Presence of the Subject
You should have a topic created by GCB indicating that the build messages are sent into a dedicated queue.
Create the Telegram BOT
Now it is time for the fun part of the tutorial.
You will have to install first the Telegram application on your mobile phone and to follow this tutorial to create your BOT. Just talk to BotFather.
After the creation of the BOT, store in your password manager, the Telegram TOKEN.
Create the Cloud Function
In this step, we will finally create our Telegram notification chatbot. The program will be coded in Node.js and deployed as a Cloud Function in Google Cloud.
Why a Cloud Function? Because we do not want to allocate a full-time virtual machine to process only during a few seconds the Google Build messages.
The code is available there and the explanation is pretty straightforward: https://github.com/sleroy/gcb-telegram-build.
Deploying the Cloud Function
To deploy the Cloud function, you need to use gcloud.
The cloud function is only publishing in a single GROUP_ID.
To obtain a Telegram GROUP Id, you need to add the Telegram bot by inviting it into a group. Then you need to query a special endpoint to get the list of all subscriptions for this bot.
Here is the Q/A about it: https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id.
The deployment instruction to push the cloud function. If everything is OK, the cloud function will be deployed.
What you should obtain, is an output similar to that:
Conclusion and Further Improvements
This bot is really simple and easy to do but it lacks the main feature, to answer to commands. Here is a list of interesting commands I would like to add:
- Get the list of nth last commits.
- Trigger a build.
- Display the build logs (or the last nth lines).
For this, you need to implement a bot server (in node.js for example). There are plenty of examples on the internet and it will be probably the subject of another article.
Published at DZone with permission of Sylvain Leroy. See the original article here.
Opinions expressed by DZone contributors are their own.