The Power Of Integration

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.

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

- Then we will use docker build command to build image , named as jenu:final

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

- here we need to unlock it with password.

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

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]


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 .


STEP-5: Create Third job
- Objective: This job test the app , if it is working or not.

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

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.

- 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

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