Integration of Git + jenkins + Docker

Backbencher05
7 min readSep 7, 2020

Complete Automation

Jenkins

Jenkins is an open source automation server with an unparalleled plugin ecosystem to support practically every tool as part of your delivery pipelines. Whether your goal is continuous integration, continuous delivery or something else entirely, Jenkins can help automate it. Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project ,and making it easier for users to obtain a fresh build.

Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Git & GitHub

GitHub is a Git hosting repository that provides developers with tools to ship better code through command line features, issues (threaded discussions), pull requests, code review, or the use of a collection of free and for-purchase apps in the GitHub Marketplace. With collaboration layers like the GitHub flow, a community of 15 million developers, and an ecosystem with hundreds of integrations, GitHub changes the way software is built.

GitHub builds collaboration directly into the development process. Work is organized into repositories, where developers can outline requirements or direction and set expectations for team members. Then, using the GitHub flow, developers simply create a branch to work on updates, commit changes to save them, open a pull request to propose and discuss changes, and merge pull requests once everyone is on the same page.

Prerequisites to understand the work done in this project :

  • Git
  • Github
  • Docker
  • Jenkins
  • Linux OS
  • Very basic knowledge of Shell/Unix commands

❗️TASK-2❗️

1. Create container image that’s has Jenkins installed using dockerfile

2. When we launch this image, it should automatically starts Jenkins service in the container.

3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins

4. Job1 : Pull the Github repo automatically when some developers push repo to Github.

6. Job3 : Test your app if it is working or not.

7. Job4 : if app is not working , then send email to developer with error messages.

8. Create One extra job job5 for monitor : If container where app is running. fails due to any reason then this job should automatically start the container again.

Step 1: Create container image that’s has Jenkins installed using dockerfile

first of all we need to create a Dockerfile for Jenkins to run on Docker container. I am using Redhat8 as base OS so using centos: latest image which is going to be pulled from docker hub.

# mkdir /ws

# cd /ws

# gedit Dockerfile

Now, we are going to create the file with name Dockerfile having the following commands:

FROM: this keyword is used to pull image to be used for container from dockerhub

RUN: this contains the commands to be executed while building the modified container

  1. We will be installing wget for downloading the files from url
  2. We installed sudo for giving the jenkins root power
  3. Next we set up the yum repository for Jenkins from URLs using wget command.
  4. Then we will first install java as it is a prerequisite for the smooth functioning of Jenkins (But the java version has to be kept in mind as Jenkins doesn’t support Oracle Java).
  5. We install Jenkins next
  6. Now we make a Jenkins entry in the sudoers file to give it root powers without the requirement for any password.
  7. We will also require Git installed to be used in Jenkins as a plugin.
  8. We use EXPOSE keyword to expose the 8080 port as Jenkins work on that.
  9. CMD keyword used here will keep the Jenkins live till the container is on and will start on container boot.

Finally we build our own docker image with jenkins using the below command:

# docker build -t <tag_name>:<version_tag> .

the dot ( . ) in the end which means all files.

ws]# docker build -t jenkinsimg:v1 .

After build u will see Docker image is created which have jenkins installed

Step 2: When we launch this image, it should automatically starts Jenkins service in the container.

Now we need to launch a container from this image to launch jenkins on Docker.

# docker run -it — privileged — name Junk -p 1234:8080 -v /:/host jenkinsimg:v1

Now we need to install some necessary plugins to get going. For that we need to go to Manage Plugins and look for below mentioned plugins:

  • Git
  • SSH
  • Email Extension
  • Build Pipeline

Finally these plugins are installed.

Job1 : Pull the Github repo automatically when some developers push repo to Github.

This job get the code from gitHub and copy in the workSpace all the process done automatically , as soon as developer commit code will auto push to GitHub and from GitHub it come to Jenkins and from jenkins it come to our Rhel-8

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

This job is the most important part of this project as it is going to launch the server this job is so intelligent that it automatically finds whether the webapp is written in HTML or PHP if developer provide HTML code it will launch HTML server and if provided PHP it will launch PHP server.

For this task rather than using precreated images from dockerhub I have built my own image using docker file.

After Creating docker Image of html and Php we write code which will check the code and start respective docker container image

We also set Trigger that after job 1 successfully complete than only process the job 2

This code check and start the respective container

OR

if you know your code is html directly start server by checking the extenstion

Hence, this job is very important as first to build a continuous pipeline we trigger this job only after the previous build is stable.
Secondly, coming to the script let me tell you that as the docker container doesn’t have support to install containers so we use chroot command to change the root from docker to the base OS.

Here, i have provided one filter to check for files by their extensions. These conditions are completely upto you.
Finally, we see the container is launched else if already present it is first removed and then launched again.

Job3 : Test your app if it is working or not.

The job 3 is to test if the application is working fine or not. The status variable is used to test the status according to the status code of the app. As 200 is for success and 404 is for error etc.

OR

Job4 : if app is not working , then send email to developer with error messages.

For job 4, use the E mail notification Post Build action and we can set the mail context accordingly.

We can visualize it using build pipeline.

Create One extra job job5 for monitor : If container where app is running. fails due to any reason then this job should automatically start the container again.

We have done such a huge infrastructure many advanced things but point is, what if due to any reason our workSpace fails or collaps. We will loose everything in a moment, so inorder to relaunch the infrastructure on failure, I have made one job here so It will also automated.

--

--