Integration of Github, Jenkins & Docker

Mini Project : MLops Training Task 2

Anirudhi Thanvi
4 min readSep 7, 2020

: Anirudhi Thanvi

Brief about the Task

A completely automated setup has been made in which the developer only needs to commit the source code and the changes will get displayed on the docker file of the extension of the file.

Job#1:
Pull the Github repo automatically when some developers push repo to Github.

Job#2:
By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed).

Job#3:
Test your app if it is working or not.

Job#4:
If app is not working , then send email to developer with error messages.

~For Moinitoring:
Job#5:
If container where app is running. fails due to any reason then this job should automatically start the container again.

Pre-requisites Required and Setup for project:

  1. Oracle Virtual Box
  2. RedHat Linux RHEL8
  3. Docker Installed and Configured on Your System.
  4. Jenkins Installed and Setup on Your System.
  5. Git Installed on Your System.

Environmental Setup:

  1. To start the docker services : systemctl start docker
  2. Setting up jenkins

Firstly, we will create a Jenkins install docker container using Dockerfile.

Then we build and launch the docker image which will automatically starts Jenkins service in the container. For building the docker image we run the command.

Execution Cell :

gedit Dockerfile
docker built -t jenkins:v1 .
docker run -dit — priviliged -v -P — name os1 jenkin:v1

Now we will setup the jenkins by entering certain credentials and then will install some plugins.

Let’s move on to the implementation part when you are ready with your full setup.

Step 1: Pull the Github repository automatically when some developers push repository to Github.

Execution Cell:

mkdir -p mlops
sudo cp -rfv * /root/mlops

Whenever developer will push any new code in the github repo. this job will download the code automatically.

Step 2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter and install image container to deploy code accordingly.
It automates when Job-1 has a success.

Execution Cell:

if /root/mlops | grep html | wc -l
then
if sudo docker ps | grep webos
then
sudo docker rm -vf webos
fi
sudo docker run -dit -p 8085:80 -v /mlops:/usr/local/apache2/htdocs — name webos httpd
fi

Step 3: Test your app if it is working or not.
It automates when Job-2 is successful.

Execution Cell:

export check=$(curl -siw “%{http_code}” -o /dev/null 10.0.2.15:8085)
if [ $check -eq 200 ]
then
echo “its working”
else
echo “it failed”
fi

Step 4: If app is not working , then send email to developer with error messages.
It automates as and when Job-3 is successfully completed.

Execution Cell:

export check=$(curl -siw “%{http_code}” -o /dev/null 10.0.2.15:8085)
if [ $check -eq 200 ]
then
sudo python3 /root/mlops/success_mail.py
else
sudo python3 /root/mlops/failure_mail.py
fi

Step 5: If container where app is running fails due to any reason then this job should automatically start the container again.
Starts after Job-3 has a success.

Execution Cell:

if sudo docker ps | grep webos
then
exit 0
else
sudo docker stop webos
sudo docker rm -vf webos
sudo docker run -dit -p 8085:80 -v /mlop:/usr/local/apache2/htdocs — name webos httpd
fi

YaY !!! It’s done. Now whenever the developer updates the repository in GitHub, jenkins will automatically make changes accordingly.

Github : https://github.com/ani-thanvi/Integration-docker-jenkins-github.git

--

--