Docker-Jenkins-Git Project #2
Image Source: Google Images

Docker-Jenkins-Git Project #2

HEY GUYS!! This is my second mini-project(task) which is based on integrating the tools like jenkins, github, docker. This was one of the tasks which I love the most as it highlighted the industry use case which I took approx. 12 hrs to complete.

##Problem Statement :

  1. Create a container image that’s has Jenkins installed using Dockerfile.
  2. When we launch this image, it should automatically start the Jenkins service in the container.
  3. Create a job chain of job1, job2, job3 and job4 using the build pipeline plugin in Jenkins.
  4. Job1: Pull the GitHub repo automatically when some developers push the repo to GitHub.
  5. Job2: 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 ).
  6. Job3: Test your app if it is working or not.
  7. Job4: If the app is not working, then send an email to the developer with error messages.
  8. Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.

##Solution

Step 1:

Creation of Dockerfile of jenkins(not the pre-configured jenkins) of first installing it with few necessary other tools.

First made the workspace task2 where Dockerfile was created. Inside Dockerfile installation of jenkins, python3, git, some tweaks for jenkins to run smoothly. FROM: the image I used here was centos 8(latest)

RUN: the commands to be executed while building the image whcih will all be present in all the containers by default which uses this image for creation.

As some features are not installed by default in various images with many softwares not configured in yum repolist.

  • installing wget to download files from the url
  • setting up yum repository for jenkins from the url
  • install jenkins
  • installing java-11-openjdk as jenkins requires java as base.
  • installing python36 for execution of mail.py(python) file.
  • installing git

COPY: here I copied the smtp mail code from the local host to the image permanently, the file used was mail.py CMD: the cmd used here will start Jenkins on running of docker container and keep Jenkins live till the container is active. Below is the image.

No alt text provided for this image
No alt text provided for this image


Building of image cmd used was docker build -t jenkinsos1:v4 

While running a container command used was docker run -it --name os1 -p 8082:8080 -v /:/host jenkinsos:v4

No alt text provided for this image
No alt text provided for this image

Step 2:

Setting up jenkins inside the container with the initialpassword provided.(USER PASSWORD CHANGE ---- RECOMMENDED)

This is one-time for a particular container until its not removed from the localhost. This and the next whole setup which I did will stay safe until the container is removed completely. I am not showing this how it is done. PS: You can google it ;D

Step 3:

Coming onto the actual DevOps part where jobs are created which is explained below.

Job Configuration

Job 1:

Q) Pull the GitHub repo automatically when some developers push the repo to GitHub

A) So basically what is done here is that the files are copied from github repository to the localhost in some local repository. Configuration image below.

No alt text provided for this image

Job 2:

Q) 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 )

A) Basically what I did here that I used basic linux commands like ls, grep, | to decide from the file extension and launch containers accordingly for deploying it over the container with allotting specific port no. Similarly other extensions can be tried, with pulling a docker image too at the runtime. Configuration image below.

No alt text provided for this image

After execution of Job2 one can easily see whether the images was created or not.

No alt text provided for this image

Job 3:

Q) Test your app if it is working or not.

 A) So what I did here was I added a condition to check for the status of the running website. If it does not give 200 as the http_code then build fails or if it succeeds then exit with code 1. Configuration image below.

No alt text provided for this image

Note- The post build action takes place depending upon Job 3 success or failure.

Job 4:

Q) If the app is not working, then send an email to the developer with error messages.

 A) Here the pre configured mail.py is run with python3 and sends Test Status message only if Job 3 failed otherwise it won't execute. Configuration image below.

No alt text provided for this image

Job 5:

Q) If the container where the app is running fails due to any reason then this job should automatically start the container again.

 A) For this monitoring job, I set a condition command to grep the running container if it doesn't find it then it will create the image. So to do that I used build periodically trigger for every minute to check the status of the container. Configuration image below.

No alt text provided for this image

 Different conditions can be set for variety possible failures of Job 1, Job 2, Job 3. Container will be created accordingly. 

No alt text provided for this image


Build Pipeline

So there are two possible cases happening here if the setup is done perfectly.

  1. Job 1, Job 2, Job 3 runs and there is no need for Job 4 to be run. Job 5 shows that there is no fault with the container creation.
No alt text provided for this image
No alt text provided for this image

2. Job 1, Job 2 runs and Job 3 fails then Job 4 will execute and the mail will be sent.

No alt text provided for this image
No alt text provided for this image

Thank You!!!

For any queries do comment and if you can suggest some improvements to the current project, I'll be very much obliged to work on it.

To view or add a comment, sign in

More articles by Aditya Raj

  • Automating Testing for ML/DL models

    Hey Folks!!! Learning never stops!!! Keeping this 3 words in mind, I have spent my past 2 months into the topics of…

    8 Comments
  • Transfer Learning - Face Recognition using MobileNet

    Idea In today's world data scientists have done a wonderful job in making comprehensive models that are able to tell…

    1 Comment

Others also viewed

Explore content categories