Automated Web Deployment and Testing using Jenkins.
In this article we are going to Integrate Jenkins, Docker, and Github to achieve End-to-End Automation.
❗ Task Description ❗
- 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 build pipeline plugin in Jenkins
- Job1 : Pull the Github repo automatically when some developers push repo to Github.
- 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 ).
- Job3 : Test your app if it is working or not.
- Job4 : if app is not working , then send email to developer with error messages.
- 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.
Pre-configuration required:
► Linux Operating System:
►Git:
yum install git -y //command to install in RHEL8apt-get install git -y
► Docker:
► Dockerfile :
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
In this case I have used the docker shown below:
FROM centos:latest
RUN yum install wget -y
RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install jenkins -y
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install git -y
RUN echo "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
USER jenkins
ENV USER jenkins
CMD ["java", "-jar", "/usr/lib/jenkins/jenkins.war"]
EXPOSE 8080
❗Note: While creating docker file the name should be “Dockerfile “only exact same as it is predefined by dockers official documentation❗
Lets Begin :)
1. Dockerfile for creating an image that has jenkins installed.
Using Vim Editor to Encode in the Dockerfile :
Build the image using the command below:
docker build -t <image name> : <version> <Dockerfile path>
In my case it is:
docker build -t myjenkins:v1 /root/ws/
Then Process begins it Download & Install our Requirements :
Successfully Done.
After creating the image we have to run the docker container Using Command :
docker run -it --privileged -p 8081:8080 -v /:/host myjenkins:v1
We will Get a unique Password..
Use this Password while login in Jenkins Container..
Open your Browser And Go to port 8081 which will Assign to Jenkins container.
Type Below cmd in Search Bar of Browser :
localhost:8081
We will redirect to this Window of Jenkins
Put That Password here.
Create User :
It shows out instance Running on 8081 port :
Jenkins is Ready to Use.
After that we will write our first job :
Jenkins Work Begins :)👇
Create jobs :
JOB 1 : Pull the Github repo automatically when some developers push repo to Github.
JOB 2:
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 ).
The above command changes the root using chroot and also checks the extension of the file. If the file is a webpage, the command launches a container main_job from httpd image. Also, this job runs after the successful completion of Job 1.
Job 3:
Test your app if it is working or not.(Testing)
Job4 :
If app/website is not working , this job will send email to developer with error messages.
Here we have used Email Extension.
Before Going to Configure job we will update some Settings Related to E-mail which is Important for Our Job.
And go to Manage Jenkins → Configure System → Under E-mail Notification apply the following changes with your credentials.
Job 5:
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.
Creating a Build Pipeline
Install a plugin named Build Pipeline from Manage Jenkins → Manage Plugins under Available Tab.
Thank You for reading !!!
Keep learning Keep sharing !!!