Integrate Jenkins with Telegram bot

Through the Jenkins webhook, you have learned how Jenkins can build a job automatically when a commit is pushed. However, when we commit the code and push it, we have to go to the Jenkins UI to check if the building process is working or not, if that is successfully completed and if there are any errors existing or not. It is somehow time-consuming and inconvenient. In this post, you will learn how to integrate Jenkins with Telegram to notify statuses like build starting, successful build or build failure.

image

In the overview, Jenkins does 2 steps:

  1. When Jenkins starts building a job /task, it sends the message: “Job name is building”
  2. If the build is successful, the message will be: “Job name is Success”. Otherwise, the message will be: “Job name is Failure”

Note:
Ubuntu version 18.04.6
Docker version 20.10
Jenkins version 2.387.1

Prerequisites:
GitHub account
Jenkins install on Ubuntu, please follow the previous post or you can use KodeKloud playground to skip this step.
Telegram account for creating the Bot.

Setting up Telegram Bot

Creating a bot on Telegram is quite easy, you can find a lot of tutorials on the internet. You just put @BotFather in Telegram to search and choose the BotFather with a blue tick. On the other hand, you can do it by accessing this link: https://telegram.me/BotFather.

And then enter /newbot which leads to the following steps to create your bot.

The new jenkins_with_telegram_bot bot is now created, then add this bot to any group you want and get the chat_id by accessing https://web.telegram.org/, see the screenshot below.

At this point, Jenkins can use the token to access the HTTP API and chat_id for sending messages to Telegram.
Before going to the next step, you should test if this setup works properly or not by simply using curl command.

$ curl --location 'https://api.telegram.org/bot5947491691:AAHV0bU3AWSgHpaG9TOpUULVm8FeSc50AVs/sendMessage' --form 'text="test"' --form 'chat_id="-860822502"'

Note: Please use your token and chat_id

The following output as

Integrating with Jenkins Pipeline

To install Jenkins, you can follow the previous blog Jenkins Pipeline for Beginners
At this point, you should store the token and chat_id in Secret text Credentials of Jenkins. To do that, from the Jenkins dashboard page, we click Manage Jenkins > Manage Credentials.

Under Stores scoped to Jenkins on the right, click on System.

Under System, click on the Global credentials (unrestricted) link to access this default domain.

We need to create 2 credentials: telegramToken and telegramChatid.
Click Add Credentials on the left.

From the Kind field, choose the secret text and then copy and paste it into the Secret field as below:

Token for ID telegramToken.

chat_id for ID telegramChatid.

Now you can create the Jenkins Pipeline, and I have prepared an example with 2 files JenkinsfileTelegramSuccess and JenkinsfileTelegramFailure for 2 cases of success and failure: https://github.com/raymondbaoly/jenkins-example

To create the Pipeline job, from the dashboard page, click New Item > input the job name and choose the Pipeline


Under Pipeline > Definition, choose Pipeline script from SCM, then choose Git and paste the link source code git. Next, update Branch Specifier to the branch you want, which is “main” here.

Note: If you use private repository git, you need to specify “Username with password credentials”. Here we use the public repository Github so it is chosen as none.

Update Script Path to JenkinsfileTelegramSuccess and then click on Save button.

At this point, we click Build Now to start executing Jenkinsfile.

Eventually, we have the first successful pipeline.

And the message is now sent to Telegram. There is a lot of useful information such as Git branch, Last message, Author, and Short commit.

To test the failure case, you can update Script Path to JenkinsfileTelegramFailure which is prepared to use the invalid command: kubectl get pod

Checking the logs on Jenkins, you will see kubectl not found

The fact is that the job often fails in stages: docker build and docker push the image.
For example: You modify the Dockerfile for installing more packages incorrectly. Or the registry server is downtime so the image cannot be pushed up.

Just now, I have demonstrated how to update Jenkins build (CI) status to Telegram. Additionally, this is the method I am using for the CI/CD pipeline. Maybe this isn’t the best strategy, but for now, it works fine. Let me know if you have any other creative or interesting solutions