Web Develoyment Automated by Jenkins And With Docker , Github .

Web Develoyment Automated by Jenkins And With Docker , Github .


welcome Everyone,

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 install 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.


7. Job4 : if app is not working , then send email to developer with error messages.


8. Create One extra job job5 for Monitor and showing Pipeline : If container where app is running. fails due to any reson then this job should automatically start the container again.and pipeline will show that it Working Good

No alt text provided for this image


For This Task2 We Requires Linux Operating System And Jenkins And Docker , Git Also Required

yum install git -y //command to install in RHEL8


apt-get install git -y 
No alt text provided for this image

And Docker Also:-

No alt text provided for this image

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.

I Used This Docker file for this Task :-

FROM centos:7
RUN yum install wget -y
RUN wget  -O  /etc/yum.repos.d/jenkins.repo   https://pkg.jenkins.io/redhat/jenkins.repo
RUN rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
RUN yum install java -y  
RUN yum install jenkins -y  
RUN yum install git -y
RUN yum install sudo -y
RUN echo  -e "jenkins ALL=(ALL)  NOPASSWD:ALL "  >> /etc/sudoers
CMD java  -jar /usr/lib/jenkins/jenkins.war

1. Dockerfile for creating an image that has jenkins installed.

Using Vim Editor i Encode Dockerfile :-

No alt text provided for this image

we Build the image using these cmd :

docker build -t <image name> : <version> <Dockerfile path>

In my case it is -> docker build -t jenkins:v1 /ws/

docker build -t jenkins:v1 /root/aaditya

Then Process begins it Download & install Ouir Requirements :-

No alt text provided for this image
No alt text provided for this image

Successfully Done.

After making the image we have to run the docker container Using Command :-

docker run -it --privileged -p 90:8080 --name jenu host jenkins:v1

after this we will get a password and we will have to write that password for initial login into our jenkins container ...

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

We will Get a unique Password..

Use this Password while login in Jenkins Container..

Open your Browser And Go to port 90 which will Assign to Jenkins container.

Type Below cmd in Search Bar of Browser :-

localhost:90

We will redirect to this Window of Jenkins :-

Put That Password here ...

No alt text provided for this image

lets do All Processing Of Configuration Of Jenkins.

No alt text provided for this image
No alt text provided for this image

Create User Account To Jenkins:-

No alt text provided for this image
No alt text provided for this image

Now Jenkins IS Ready To Work

No alt text provided for this image

Now After Installation And After All Process Of Configuration of Jenkins Complete then First Download The Plugins Of SSH And All Relatable Plugins you have to download install ..

No alt text provided for this image

Next Step:-

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

After Setup Of Credentials setup SSH remote host:-

No alt text provided for this image

SSH Setup Completed.

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.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Put Your Git Repo Link And then setup Build Triggers

No alt text provided for this image
No alt text provided for this image


No alt text provided for this image


Output Of JOB1:-

No alt text provided for this image

Now Job1 Successfully Completed..

Now JOB2:-


No alt text provided for this image

No alt text provided for this image
#!/bin/bash
if ls  /root/aaditya  | grep html
then
if  sudo  docker ps -a | grep OShtm
then
sudo docker rm -f  OShtm
echo "older container is terminated "
sudo docker run -dit -p 777:80  -v  /root/aaditya:/usr/local/apache2/htdocs  --name  OShtm  httpd:latest
echo "New container with updated code files is launched "
else
sudo docker run -dit -p 777:80  -v  /root/aaditya:/usr/local/apache2/htdocs  --name  OShtm  httpd:latest
fi
else
if ls  /root/aaditya | grep php
then
if  sudo  docker ps -a | grep OSphp
then
sudo docker rm -f  OSphp
echo "older container is terminated "
sudo docker run -dit -p 777:80  -v  /root/aaditya:/var/www/html  --name  OSphp  vimal13/apache-webserver-php:latest
echo "New container with updated code files is launched "
else
sudo docker run -dit -p 777:80  -v  /root/aaditya:/var/www/html  --name  OSphp  vimal13/apache-webserver-php:latest
fi
fi
fi


Here we have compatibility for python, html & php. If you want some more language to be handled then you can add it. One more thing here we need to take care of is we need to make containers for these language interpreter also.

Output of Job-2 :-

No alt text provided for this image


By Lookins Our code out Jenkins Launch Html Container And in this Above Output We see Container ID.

Job3 : This job will test the app/website , whether it is working or not.


No alt text provided for this image
status=$(sudo curl -o /dev/null -s -w "%{http_code}" 192.168.99.101:777)
echo $status
if [[$status = 200]]
then
    echo "Congrats"
fi
if [[$status != 200]]
then
    echo "Sorry"
    exit 1
fi



No alt text provided for this image

OutPut Of JOB3:-

No alt text provided for this image

Job4 : If app/website is not working , this job will send email to developer with error messages.

No alt text provided for this image

Here we Give email Address for Reciving mails about job.

No alt text provided for this image

OutPut Of JOB4:-


No alt text provided for this image


Job 4 completed , Now Job 5

No alt text provided for this image

OutPut Of Job5:-

No alt text provided for this image

And Final Pipeline Is:-

No alt text provided for this image

All Jobs Are Done Successfully !

No alt text provided for this image

*Now Final OutPut:-

And Final Page And Web deployed By Jenkins is :-

No alt text provided for this image

Task 2 Completed Succefully

Thank You So much @Vimal Daga Sir And Linux World!



Chirag Deepak Ratvekar

Mainframe Developer at Wipro | Microsoft Certified Azure Administrator Associate | Cloud & Data Enthusiast.

4 年

Excellent work brother ????

回复

要查看或添加评论,请登录

Aaditya Joshi的更多文章

社区洞察

其他会员也浏览了