Automation Project by Integrating GitHub, Docker and Jenkins
MLops_Task2

Automation Project by Integrating GitHub, Docker and Jenkins

Task Description

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 : If container where app is running. fails due to any reson then this job should automatically start the container again.

Solution-:

Step1:

First we have to create a docker image containing Jenkins installed in it by using Dockerfile. for it we have to use the given below code in Dockerfile.

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

No alt text provided for this image

now we have to run the given command to execute the Dockerfile and to create a docker image which will contain jenkins installed in it.

docker run -it privileged -p 9999:8080 -v /:/host task2:v1


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


Job-1

this job will download the code as soon as code is pushed in github and will copy it in the folder.

No alt text provided for this image
sudo rm -rf /host/web-repo
sudo mkdir /host/web-repo
sudo cp -r . /host/web-repo


No alt text provided for this image

Job-2

this job will launch another OS hosting our code that we have pulled from github . This job will only run if job1 build's without any failure.

chroot /host/bin/bash <<"EOT"
path = "/root/web/recipe.html"
filename=$(basename "$path")
ext = "${filename##*.}"
sudo echo $ext


if [ "$ext" == "html" ]
then
	if docker ps | grep task_2
	then
    docker rm -f task_2
	sudo docker run -itd -p 1234:80 -v /web:/usr/local/apache2/htdocs --name task_2 httpd:latest
	else
	sudo docker run -itd -p 1234:80 -v /web:/usr/local/apache2/htdocs --name task_2 httpd:latest
	fi
else
echo "this is not html "
fi
EOT


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.

No alt text provided for this image

Job-3

This job will test if the hosted code is running fine or not

export status=$(curl -o /dev/null -sw "%{http_code}" 172.18.0.3:1234/recipe.html)
if [$status==200]
then
exit 0
else
exit 1

fi

No alt text provided for this image

Job-4

this job will keep monitoring the production container and launch a new replica of that container as soon as it goes down.

if docker ps | grep task_2
then
exit 0
else
docker run -itd -p 1234:80 -v /web:usr/local/apache2/htdocs --name task_2 httpd:latest

fi

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

This is my github link for reference :



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

社区洞察

其他会员也浏览了