Blog

DevOps Assembly Lines

Quick Tips

E2E App Deployment using Jenkins :

Problem Description :

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.

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

Solution:

Docker is a container tool that simulates an entire operating system with all the required resources. I have created a Dockerfile for the same which makes is very easy to setup the required environment in just a click.

No alt text provided for this image

FROM: This keyword is used to specify the name of docker image we are going to use for this setup. Here , i have used the latest version of CentOS. You can use any other image as per your wish.

RUN: This keyword is used to run the shell commands as soon as the docker image is downloaded and launched.

COPY: This is used to copy the files from host inside the container

CMD: This is used when we are required to execute any shell command. Here i have used to to start the Jenkins server.

As we require to configure Jenkins inside the container , so , before installing jenkins, we need to install java JDK because jenkins is dependent on JDK. Then , we have to add the jenkins user to the sudoers file to handle the commands and other request because by default , jenkins user don’t have permission to other files except its home directory. Then i have installed python3 as i have a python mail.py file to send the email notification in case of the job failure. Git is required to handle the github repository.

Below is mail.py file

No alt text provided for this image

Now, we have to build this docker image

docker build -t jenkins:v1 .

When you run the above command, you will see the below screen

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

Now, we have to start/launch this container image:

docker run -dit --privileged -p 1234:8080 -v /:/host jenkins:v1 

-p: used to bind the host port and the container ports. Jenkins run on 8080 port and i have chosen port 1234 for host machine to bind to it. We can use any other free port instead 1234.

–privileged: It has been used to allow enhanced access to the container.

-v: It is used to mount the “/” dir of host machine on the container at “/host” dir.

Now , we can access the Jenkins server using the Host IP

No alt text provided for this image

To pull the github repo automatically , we need to define “webhooks” in the github repository to trigger the jenkins job. For this , we need a public IP which can be obtained using “ngrok” which is a free tool.

We need to run ngrok on the same port of host which we have bound to Jenkins server. In our case , it is 1234.

./ngrok http 1234

No alt text provided for this image

You will see the above screen when you run the ngrok service on port 1234. Here we will get our Public URL i.e. “http://01708424.ngrok.io” in my case and now , I can use this to initialize the github webhooks.

No alt text provided for this image

This webhook will trigger the jenkins job to pull the repo.

JOB-1:

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

Here , we are copying the data in /host/web directory. Later , we will mount this directory on the apache server.

JOB-2:

Now, we have to launch a container having an interpreter or compiler for the respective type of code. Like, here we have an HTML file , so we will launch httpd image. Similarly, if we have a python file, then we launch a container having python interpreter to run the file.

No alt text provided for this image

Here, as we have copied all the files in /web directory which were pulled from the github repo, so if we find any file with HTML extension , we will start an apache container. Similarly , we can apply the above condition for other file , as per the requirement.

JOB-3:

Now, our task is to verify the code. We can do this by creating a job as mentioned below:

No alt text provided for this image

A simple way to verify that if our web-site is working or not, is to check the response code of apache/http server. Code 200 implies that the website is working fine and is accessible.

If this job fails due to any reason , that means , the web-server is down or not accessible, then it must trigger the Job-4 to notify the concerned team.

JOB-4:

If the job-3 fails, then we have to send the email notification. Here we will use the mail.py file which we created in the initial phase.

No alt text provided for this image

JOB-5:

This is the last stage of our Build Pipeline, which is “monitoring” the server. We need to have an eye on the web-server container to check if it is working fine or not. If, due to any reason, it goes down , we have to launch a new web-server , ensuring minimal or no downtime.

For this , we schedule a job which will check the container every minute.

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

That’s All !! We have build all the required jobs.

The whole pipeline looks like this:

No alt text provided for this image

Hope you found this article helpful. For any queries or suggestions, please contact me. Your reviews would be of great help for me!

Leave a Reply

Your email address will not be published. Required fields are marked *