Image for post
Image for post

Web deployment through automation

Hemakshi Manchandia
Jun 19 · 6 min read

In the previous automation task in DevOps, we automated the tasks of pulling the repositories from GitHub when developer pushes some new code onto master and dev branches, and accordingly deployed them on the required production and test environment respectively.

But in the testing environment, to see whether the new code is fit for deployment in the production or not, we were checking it manually.

So in this project, we’ve tried to completely automate the process end-to-end; i.e., form pushing the piece of code by the developer, to testing it, and deploying it in the real world if was successful. All this, without any human intervention

I’ve achieved this, using: Git and GitHub, Jenkins, Docker.

A brief of the problem statement is:

First of all, we create a docker image, using this Dockerfile. We’ll launch our Jenkins from this image’s container only.

Image for post
Image for post
Dockerfile

Here’s the various keywords we used in it:

· FROM: the base image to be used for the container

· RUN: execute these commands on the base image obtained

· CMD: to start a service when the container starts

The commands to build an image and start a container from the dockerfile are:

docker build -t jenkins_os:v7 .
docker run -it privileged -v /:/host -p 8083:8080 --name jnksos1 jenkins_os:v7
Image for post
Image for post
starting a container
  • - -previleged- to create a conatiner that has all the root capabilities of the host.
  • -v : stands for volume. Here we are mounting our / folder with the host folder of the container.
  • -p :stands for patting. Here we are patting port 8083 of the host with the port 8080 of the container.

Next, we log in into Jenkins from the container created:

Image for post
Image for post
starting screen
Image for post
Image for post
jenkins password

Now we move onto Jenkins for the automation part. We create 6 jobs for this task.

All our jobs will be connected in a pipeline. I used the Build Pipeline Plugin to visualize our pipeline.

Image for post
Image for post
web_deployment pipeline

a) So I created job1 to pull my repository from GitHub, onto my RHEL 8 system.

Here’s the configuration of my job1.

Image for post
Image for post
Image for post
Image for post
job1

Command-

rm -rf /host/workspace
mkdir /host/workspace
cp -rf * /host/workspace

b) Next, we have to check the webpage pulled from the GitHub repo, and start the corresponding container for it. If, webpage is of .php type, start the container of php image. Same for .html type files. I’ve installed the image from docker hub.

Image for post
Image for post
Image for post
Image for post
job2

This will be our Job2, which is a downstream for the Job1, will be built only Job1 was successfully built.

c) If the container is successfully launched, i.e., the built of job2 is stable, then we’ll run its downstream job, Job3 , where we’ll check whether the deployed webpage is actually working or not.

Image for post
Image for post
job3

If the status code of the webpage is equal to 200, then it means that the webpage is running smoothly.

d) The next job i.e job4 is downstream job of job3, if the build is stable.

Image for post
Image for post
job4

In Job4, we’ve used the amazing plugin “Email Extension” to send a mail to the developer regarding the successof webpage deployment.

In the “Manage Jenkins” ->“Configuration” settings, we have to setup the sender details, and also set up the smtp server for sending mails.

Image for post
Image for post

For the set of smtp server, we have to make some changes in the /etc/sysconfig/jenkins file of the container-

Image for post
Image for post
/etc/sysconfig/jenkins

After that don’t forget to restart jenkins services in the container. For that you can use-

docker exec jnksos1 yum initscripts -y
docker exec jnksos1 /etc/rc.d/init.d/jenkins
docker exec jnksos1 sudo service jenkins restart
Image for post
Image for post

Also, in the post-build step of Job4, we have to enter the recipient email address. I have deliberately fail this job using exit1 so as to trigger the post-build and send the mail.

Image for post
Image for post

Here are the screenshots of the outputs after I push the two file info.html and details.php.

Image for post
Image for post
git bash
Image for post
Image for post
Image for post
Image for post
mail

You can see the webpages are successfully deployed on the respective servers-

Image for post
Image for post
html webpage
Image for post
Image for post
php webpage

e) All the above tasks can take place only if the containers keep on running continuously without fail. We have to monitor that all containers are running, and if any container fails at any point in time, we have to restart the container and resume our task again. For this we make our Job5, which makes sure of it.

Image for post
Image for post
Image for post
Image for post
job5

f) Now, what if the developer has written some wrong code. I have created another job -job6 for that. This job will send a mail to the developer if the code has some error. I have checked whether the error code is 500 or not.

Image for post
Image for post
Image for post
Image for post
job6

Output-

I have pushed a wrong php code in github. Now, a mail will be sent to the developer by job6.

Image for post
Image for post
error mail

So, that was my project.

I would really like to thank my partner Aashi Gupta for her cooperation, support and help.

If you have any doubt you can write in the comment section.

Thanks for reading :-)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store