The Power Of Integration

Radhika Sharma
5 min readMay 20, 2020

--

Integration of Jenkins with git & docker

In this article I will show you the power of integration in DevOps by integrating continuous integration tool Jenkins with SCM(source code management ) tool Git -GitHub & containerization tool docker.

How to proceed:

STEP 1: Launch Jenkins inside docker container & start its services.

Dockerfile which create image having Jenkins installed
  • here we are using centos:7 image as a base image to specify it ,we are using FROM keyword .
  • Now we create different layers of other software's on the top of it by installing them using RUN keyword.
  • we are installing wget software for using wget command, then installing Jenkins ( here wget will be used), installing openjdk as jenkins run on the top of java, then we are installing docker(as we are using the concept of Dind -docker in docker ) , sudo , curl, git & also giving the sudo power to Jenkins in /etc/sudoers file.
  • Finally we run Jenkins service using CMD command & expose it on port no. 8080 as Jenkins run on 8080 port.
Build Jenkins image from Dockerfile
  • Then we will use docker build command to build image , named as jenu:final
we can see jenu image is created, now launch Jenkins container using this image.
  • Now launch the docker container from this image, here i am giving Jenkins name to this Jenkins container, when we launch it , it will be having all the software's which we installed earlier & also it starts the Jenkins service automatically at 32768 port no. (randomly give as we used -P option for port forwarding).
  • Note: because , we will be launch other docker containers inside this Jenkins container so we have to mount the base system’s docker socket to this container so that the jenkuos(Jenkins container) can launch the containers inside but remember it will launch siblings containers not child containers.
start Jenkins in jenkuos container
  • here we need to unlock it with password.
copy the password from file inside the jenkuos

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

Build Pipeline of these 4 jobs

STEP 3 : Creation of First Job

  • objective : job 1 Pull the GitHub repository automatically inside the Jenkins workspace when some developers commits in its local git repository.
  • we are using post-commit hooks in local git for automatically pushing in GitHub & github-webhook in GitHub, so that as soon as code push in GitHub it will trigger the Jenkins & Jenkins will download the code.
  • For more details how to perform job 1 you can see this blog.

STEP 4: create second job

  • Objective : In job 2 By looking at the code or program file present in Jenkins workspace , 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 ).
  • Here we run docker container inside the docker container [DIND]
This code can launch the docker containers for html,php & python files only.

Explanation:

  • First of all using cd command go into Jenkins job1’s workspace where all the downloaded file is present.
  • Jenkins will identify the type of file using its extension , here to identify the extension i am using ${file##*.}
  • After identifying the type of file it will launch the corresponding docker container , Here we have to mount the files inside the container for that firstly we copy the files using scp inside in base os in which Jenkins is running.
  • For automatic login in remote server we use ssh-keygen tool .
generated ssh -keygen & copy it in remote server
output of job-2 inside Jenkins after launching docker containers ,we had 2 type of files one is of html & another of php so this job will launch 2 conainter named as webos & phpos for these files respectively

STEP-5: Create Third job

  • Objective: This job test the app , if it is working or not.
Here we are taking the example of a html file & php file which is running on 2000 & 3000 port respectively in my case.
  • We are using the concept of http_code for testing . As we know if a website is working fine its http_code always be 200.
  • so if http_code is 200 it will return exit 0 means success otherwise exit 1 means failure.

STEP-6: Create Fourth job

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

  • For send email using Jenkins , you can take reference from here.
  • This job will trigger automatically when job-3 will fail, & thus this job-4 send email .
email-sent when job-3 fail

Here I am using default email notifier of Jenkins , you can also use some email extension plugin.

STEP-6 Create A Extra job-5 for monitor

Objective: If container where app is running. fails due to any reason then job-5 should automatically start the container again.

  • For this we are using build periodically for trigger Jenkins(This job is not present in job-chaining ) it will check in every minute the container( in which our product or app is running ) is running or not, if it is not running then it will restart the container.
here * * * * * means at every minute this job will trigger
  • we are done !!

THE CONCLUSION:

  • As soon as Developer commit code in local git repo -> auto pushing in GitHub -> automatically pull this app in Jenkins workspace -> auto deploying of app inside docker container ->auto Testing -> If it fails -> Then send email
  • finally job 5 will monitor the containers & if it fails then it restart all the containers
As you can see that this pacman game is running inside a webcontainer which is itself running in jenkins container(we are using Dind concept here )

How to create these jobs more efficient & Agile:

  • WE can use kubernetes instead of docker ,for launching containers ,As k8s directly launches pods , & this is the more efficient way.
  • we can manage our containers (update, monitor) very easily by using k8s.

Reference:

This task was give to me in DevOpsAL training, I completed this task under the guidance of our vimal sir. Here I would like to show my gratitude to our trainer @vimaldaga sir. for this valuable training & contents.

Always Happy for you suggestions :)

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response