Complete Automation of Web-Application Deployment and Testing using Jenkins, Docker and GitHub.

Prakash Singh Rajpurohit
7 min readJul 7, 2020
Complete Automation of Web-Application Deployment and Testing using Jenkins, Docker and GitHub.

Agenda:

  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 installed 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 and if app is not working , then send email to developer with error messages.
  7. Create One extra job job4 for monitor : If container where app is running. fails due to any reson then this job should automatically start the container again.

Pre-requisites:

  1. Must have Jenkins installed
  2. Must have Docker installed
  3. Must have python installed (Opt, I use to mail error message to developer).

Step 1

I created one container image for Jenkins using Dockerfile. Here in this I also install epel-release and ssh-pass package which we gonna need for SSH connection between Jenkins container and Base OS RHEL-8 and also as soon as this container launch it will automatically start Jenkins service in the container.

FROM centos:7RUN yum install wget -y
RUN yum install git -y
RUN yum install sudo -y
RUN yum install net-tools -y
RUN wget https://pkg.jenkins.io/redhat-stable/jenkins.repo -O /etc/yum.repos.d/jenkins.repoRUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum makecache -y
RUN yum install jenkins -y
RUN yum install java-11-openjdk.x86_64 -y
RUN echo -e "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoersRUN yum install /sbin/service -y
CMD sudo service jenkins start -DEFOREGROUND && /bin/bash
EXPOSE 8080#For SSH Connection between Container and Base OS RHEL 8.
RUN yum install epel-release -y
RUN yum install sshpass -y

This is the docker command I use to build the container image.

docker build -t jenkins:t2 /root/devt2/jenkinsDoc

This is the docker command to launch the Jenkins Container, I had done the patting (Port forwarding) to have the outside connectivity with this container and also I mount the RHEL-8 directory jen-storage with container jen-storage directory. When this container launch on the fly it will create a directory jen-storage in this Jenkins container.

docker run -it -p 9999:8080 -v /root/jen-storage:/jen-storage --name jenkinsos jenkins:t2

As we know docker container is running in isolation world, so when we launch a container using this image, container where Jenkins is installed is isolated from base OS RHEL 8. Now if we want to launch an application server container in RHEL-8 using this Jenkins so for that we have to do a SSH connection between Jenkins container and Base OS RHEL 8. Through SSH Jenkins will establish a connection, go inside the base OS RHEL-8 and launch the application server container in RHEL-8.

Watch this video to do a SSH connection between docker container and Redhat (RHEL-8):

We have to run this command in Jenkins container to establish a connection between Jenkins container and base OS RHEL-8. First command will generate a private key and public key of container which is full encrypted. Second command will establish a connection between Jenkins container and RHEL-8. Using ssh root@192.168.0.31 we can go inside RHEL-8 and launch or run any command in RHEL-8.

ssh-keygen -t rsa -b 4096sshpass -p "prison@123" ssh-copy-id -o StrictHostKeyChecking=no root@192.168.0.31

Here I am creating one more container image using Dockerfile for application server container. Through SSH connection Jenkins will launch this application server container inside RHL-8.

FROM centos:7
RUN yum install httpd -y
RUN yum install php -y
RUN yum install /sbin/service/ -y
RUN yum net-tools -y
EXPOSE 80
CMD /usr/sbin/httpd -DEFOREGROUND && /bin/bash

Step 2

Job1:

When developer push the code this Job will pull the GitHub files automatically through poll scm. This repository is store in Jenkins container and also in base OS RHEL-8 because we had done the patting between Jenkins container and base OS RHEL-8.

Pull files from GitHub.

In GitHub repository we have 2 files name Mail.py and index.php.

If web application server is not running properly because of some error in code, we will use this Mail.py file to send an alert message to developer.

Index.php is code which we gonna deploy on application server container. It is very simple code.

GitHub Repository

Job 1 Output:

Step 3

Job2:

Now this job will check the code of developer, accordingly Jenkins will launch the respective language interpreter, install image container inside RHEL-8 to deploy this code. Here for practical I am using php code, now Jenkins start/launch the container that has PHP already installed. We already created a container image name webos where php, httpd already installed.

Job 2 Ouput :

Step 4

Job3:

This job will test the application server using status code. If server is not running properly because of code it will automatically send a mail to the developer with error message.

Job 3 Output:

Step 5

Job 4:

This job will check that if any container where the app is running fails due to any reason then this job will automatically start the container again.

Job 4 Output:

Application Web-server is running you can see :

Step 6

Now let us do 1 more thing if we make error in code (index.html) intentionally then what will happen? Let see step by step.

Intentionally I edit the code you can see.

Error code.

Job 1 :

Now job 1 automatically pull the repository because we are using poll scm in Jenkins. You can see the Output of job-1

job1-output. Pull the Repository from GitHub.

Job 2:

Now job2 will remove the older application server container and launch new one.

Job 2 Ouput.

You can see our web-server is not working.

Web-server.

Job 3:

Now job3 will check that application server is running properly or not. After checking it found that server is not working so now it will send a mail to developer.

This job3 check server is running properly or not, if not then it will send mail to developer. For mail I created python program file. You can also use jenkins mail plugins.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = 'sender mail id'
password = 'password'
send_to_email = 'receiver mail id'
subject = '!!! Testing Server Report !!!'
message = "Server is not working,please debug the code"
msg = MIMEMultipart()server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

You can see mail sent, because webserver is not running.

Mail Sent Successfully

Job 4:

Now job4 will again try to launch the container if any container fail, but our server is not working so it is of no use.

Job4 Ouput

Job1, Job2, Job3 and Job4 build pipeline.

Build Pipeline.

You can watch complete practical video of this Task.

Thank-you for reading.

If this article is helpful, it would be appreciable if you could give 1 clap for it.

--

--

Prakash Singh Rajpurohit

Cloud Stack Developer. Working on Machine Learning, DevOps, Cloud and Big Data.