Integrating Jenkins with GitHub and Docker to deploy Web Server
Hello Connections , this is Milind Verma and once again I have written this article to showcase the integration of multiple technologies to automate the deployment of the web server.
Here in this article I have used technologies like Jenkins , Docker , Git and some algorithms. Basically in today's world automation of technologies is very much needed . Everyone wanted to make his deployment faster and more qualitative to the society.
So here I have presented a small task which will give you the idea about how to make the deployment of a service automated.
Mainly some SCM tools like Github , management tools like Jenkins and container tool like docker becomes the backbone of this pipeline of CI/CD to launch the applications within few clicks where all the internal works are done by these tools in the background. The main intention of this pipeline is to make this deployment as fast as possible wherein no human intervention occurs.
So here our task is :
- Create container image that's has Jenkins installed using Dockerfile.
- When we launch this image , it should automatically starts Jenkins service in the container.
- Create a job chain of JOB1, JOB2 , JOB3 and JOB4 using the build pipeline plugin in Jenkins.
- JOB1: pull the GitHub repository automatically when some developers push the repository to GitHub.
- JOB2: By looking at the code or program file , the Jenkins should automatically start the respective language interpreter installed image container to deploy code (e.g if the code is of PHP, then Jenkins should automatically start the container that has the PHP installed).
- JOB3: If the app is not working, then send the email to developer with the error messages.
- JOB4: one extra job for monitor : If the container where the app is running fails due to any reason then this job should automatically start the container again.
LETS START THE WORK .
Creation of the Dockerfile
So here using the Dockerfile I have built the image that has git and jenkins installed and starts the jenkins services as soon as the container is launched using that image.
FROM centos:latest RUN yum install sudo -y RUN yum install wget -y RUN yum install curl -y RUN yum install git -y RUN yum install net-tools -y RUN yum install ncurses -y RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install java -y RUN yum install initscripts -y RUN yum install jenkins -y RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers CMD service jenkins start
Now build the image using the following command.
docker build -t jenkins:v1 .
This command will create the image , it will take some time to create . After creation you can use the image.
Launch the container using the above image .
docker run -dit --name jen1 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker jenkins:v1
Here in this command I have directly mounted the "docker.sock file" and the docker config files on the jenkins container to make the docker commands executable and also expose the container.
Now launch the Jenkins using the port 8080, and enter the jenkins using the initial admin password provided by jenkins at the location /root/.jenkins/secrets/initialAdminPassword.
Install the github and the build pipeline plugin to make the github work inside the jenkins and make the jenkins environment ready for the work. .
After installing the plugin , now we move on to the JOBS.
JOB1:
This JOB pull the code from github and copies it in the folder named task2devops on the base container. I have used the github web hook to make this automatic.
Now making the JOB1.
JOB2:
This job triggers as soon as the JOB1 is built successfully and this job checks the code and launches the container according to it , i.e if the code is of PHP then it launches the container that have the PHP installed and if the code is of the HTML then it will launch the container of the HTML.
This job is running continuous with the JOB1
JOB3:
This job triggers after the successful build of the JOB2 and this job checks the "http code" (like 200 for success, 404 for unsuccessful) of the web server. If the job is unsuccessful then it will alert by sending the Email to the developer to recheck the code.
code for the email s below
JOB4:
After all the jobs are run successfully then this job triggers and keeps on monitoring the container if it is running and if in case the container stops it starts the container again.
my website
Finally the build pipeline for the task
Thank You very much for going through my article
Any suggestions are appreciated :)