Continuous Integration — Github-Jenkins + Telegram integration

Photo by Tine Ivanič on Unsplash

What is Continuous Integration (CI) ?

Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily — leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible..

Manually invoking different Jenkins jobs to build, validate, push and deploy does not imply that you are following CI/CD practices.

For Trunk Based Development, it is necessary to integrate automatic build pipeline to keep trunk codebase in deployable state.

Also For Feature Branch Development, it is always a good practice to keep master/feature branch in healthy state and always verify that the new features are not breaking master.

Please Refer — Why-i-prefer-trunk-based-development-over-feature ?

CI could be achieved by integrating version control system with the build pipeline.

Michael Nygard well explained CI/CD flow in his book — Release It !

image copied from Release It! Design and Deploy by Michael Nygard

Below are the common hosted version control services which have in built CI/CD pipeline features :

Azure Devops covers the entire application lifecycle, and enables DevOps capabilities (Git based Repo, Board(~Jira), Pipeline, Artifacts ,Test) — Will cover in upcoming articles

Gitlab/Azure/AWS provides many out-of-the-box Devops features but there is always a risk of vendor Lock-In.

On the other side we have Jenkins which is a self-managed open source tool and provides 300+ plugins and support declarative pipeline.

In this article, I will cover how to integrate Github repository with Jenkins build pipeline using webhooks and how to integrate Telegram api in build pipeline.

For every commit or MR merged to master branch, Github webhook will generate event to Jenkins Pipeline which will invoke our build pipeline.

Build pipeline will checkout the latest version from Github. It will compile -> build code -> build docker image -> test -> notify developers over telegram.(Checkout + Build code + Build docker + Run Test cases + Push notification)

Pre-requisite
Basic knowledge of Docker + Github + Jenkins + Declarative pipeline/Jenkinsfile

Set Up Jenkins

To keep things simple, I am running a Jenkins as a docker.

docker run -d -p8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /app/jenkins_home:/var/jenkins_home -v $(which docker):/usr/bin/dockercustom_jenkins

Here I am using customised jenkins docker image. Please refer to this repo for the same.

Make sure jenkins is publically access-able.

Configure Github WebHooks

  • Go to Github Account -> Setting -> Developer Settings -> Personal Access Token
  • Choose repo, admin.repo options and generate token
  • Don’t forget to copy the token
  • Goto Jenkins -> Credentials -> Global Credentials -> Add credentials -> Select Secret text option and paste the github copied personal access token
  • Goto Manage Jenkins -> Configure System -> Add Github server
  • Configure github account and select the credential created in the above step

Add Webhook

  • Go to Github Repo -> Setting -> Webhook -> Add Webhook
  • Provide jenkins payload url with content-type as application/json
  • A green tick sign appears once webhook is integrated
  • Configure pipeline SCM details and select option GitHub hook trigger for GITScm polling option
  • Let’s push code changes in master branch and check whether its integrated or not
  • Eureka !!! you could see there is a job in queue

One of the important principles and practices of Continuous integration is

Everyone can see what’s happening

If the build or test fails, the CI pipeline alerts the team. The team fixes the issue at the earliest and continue to continually integrate and test throughout the project.

To cater to the above principle , I am using telegram.

Telegram Bot Set up

  • Open Telegram App.
  • Search @BotFater , send “/newbot” command and then send bot name and the user name.

Once Bot is set up invoke below api using the above http api token. A Chat ID will be generated.

curl -ivk https://api.telegram.org/bot<http-token>/getUpdates

Create Jenkins secret credentials for Chat ID and Http Token.

  • Edit Build Pipeline and add another stage name “Push Notification”

stage(‘Push Notification’) {

steps {

script{

withCredentials([string(credentialsId: ‘telegramToken’, variable: ‘TOKEN’),
string(credentialsId: ‘telegramChatId’, variable: ‘CHAT_ID’)]) {
sh ”””

curl -s -X POST https://api.telegram.org/bot${TOKEN}/sendMessage -d chat_id=${CHAT_ID} -d parse_mode=”HTML” -d text=”<b>Project</b> : POC \
<b>Branch</b>: master \
<b>Build </b> : OK \
<b>Test suite</b> = Passed”
”””
}
}
}
}
```

Here I am giving you an idea of telegram integration but of course notification message would depend on the build state.

Once build pipeline is successful you will see the below stages :

Don’t forget to check Telegram bot message :

In the above telegram integration I have invoked telegram rest api. However, there is another way using Jenkins — Telegram Bot Plugin

Go to Manage Jenkins -> Plugin Manager -> Available and search for Telegram Bot Plugin and install it

Goto Manage Jenkins -> Configure System -> TelegramBot and configure “Bot name” and “http token” in Jenkins.

Use the below stage

steps {
script{
withCredentials([string(credentialsId: ‘telegramToken’, variable: ‘TOKEN’),
string(credentialsId: ‘telegramChatId’, variable: ‘CHAT_ID’)]) {
telegramSend(messsage:”test message”,chatId:${CHAT_ID})
}
}
}

Once the testing is complete, Don’t forget to add developer-group in your telegram bot as they are the ones who will fix the broken build changes.

Thanks for reading !

Hope you have enjoyed reading the article.

Hit 👏 button (up to 50 times) . It encourages me to keep writing :)

Please watch out for my next article on Continuous Deployment - using Ansible.

Father || Coder || Engineer || Learner || Reader || Writer