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.
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
- - -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:
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.
a) So I created job1 to pull my repository from GitHub, onto my RHEL 8 system.
Here’s the configuration of my job1.
rm -rf /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.
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.
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.
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.
For the set of smtp server, we have to make some changes in the /etc/sysconfig/jenkins file of the container-
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
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.
Here are the screenshots of the outputs after I push the two file info.html and details.php.
You can see the webpages are successfully deployed on the respective servers-
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.
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.
I have pushed a wrong php code in github. Now, a mail will be sent to the developer by job6.
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 :-)