Build Pipeline
Tanuj Mathur
Digital Specialist Engineer at Infosys | RHCSA | DevOps | Amazon Web Services |
Would you believe me if I tell you that your hard work to create a website is successful and you can run your website within minutes and monitor it at the same time? In the first instance, this might sound funny, but yes this is possible.
In the world of automation, various tools enable you to accomplish this process within a few minutes or even seconds. Launching a Docker container to run Jenkins and then further launching a new container to run your website after downloading the code from GitHub and then keep a check if the build is successful or not. If not, you will get an E-mail and hence you have no need to keep checking it manually. So just relax and watch your website reach out to the world.
During my learning of DevOps, I worked on a task to implement a website or any other file with an extension by launching the relevant container and check the proper functioning of the code.
Description of the task:
1. Create a container image that has Jenkins installed using Dockerfile
2. When we launch this image, it should automatically start 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 the repo to Github.
5. Job2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter and install image container to deploy code.
6. Job3: Test your app if it is working or not.
7. Job4: If the app is not working, then send an email to the developer with error messages.
8. Create One extra job job5 for monitoring: If the container where the app is running, fails due to any reason then this job should automatically start the container again.
Tools Used:
- Github
- Jenkins
- Docker
Creating the Dockerfile for a container with Jenkins configured as follows:
FROM centos RUN yum install wget -y RUN yum install net-tools -y RUN yum install python3 -y RUN yum install httpd -y RUN yum install sudo -y RUN yum install git -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-11-openjdk.x86_64 -y RUN echo -e "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers USER jenkins ENV USER jenkins CMD ["java", "-jar", "/usr/lib/jenkins/jenkins.war"]
Build the image and then run it exposing a port to run Jenkins WebUI to configure jobs.
docker build -t mytask:v1 /mlops_task/ docker run -it --name --privileged -p 8082:8080 -v /task2:/task mytask:v11
Before you start using Jenkins make the following configurations to use SSH for running commands on the base OS and installing and configuring plugin to send an E-mail.
This job will automatically pull the files present in the GitHub repository and copy it to the mounted directory of the container as soon as changes are made as it monitors it all the time.
sudo cp -r . /task/
This job checks the extension of the file and starts the relevant container as per the requirement.
cd /task2 if ls | grep *.html then if sudo docker ps -a | grep html_container then sudo docker rm -f html_container sudo docker run -dit -p 85:80 -v /task2:/usr/local/apache2/htdocs/ --name html_container httpd else sudo docker run -dit -p 85:80 -v /task2:/usr/local/apache2/htdocs/ --name html_container httpd fi elif ls | grep *.php then if sudo docker ps -a | grep php_container then sudo docker rm -f php_container sudo docker run -dit -p 88:80 -v /task2:/var/www/html --name php_container vimal13/apache-webserver-php else sudo docker run -dit -p 88:80 -v /task2:/var/www/html --name php_container vimal13/apache-webserver-php fi else echo "Container Unknown" fi
This job tests if the code is working or not.
export status=$(curl -o /dev/null -s -w "%{http_code}" https://192.168.1.2:85/index.html) if [[ $status==200 ]] then exit 0 else exit 1 fi
This job will send an e-mail to the developer if the build fails due to any kind of error.
Finally, in case if the container fails it will be restarted within seconds so that the viewers do not face any kind of lag or delay in viewing the content.
cd /task2 if ls | grep *.html then if sudo docker ps | grep html_container then exit 0 else sudo docker run -dit -p 85:80 -v /task2:/usr/local/apache2/htdocs/ --name html_container httpd fi elif ls | grep *.php then if sudo docker ps | grep php_container then exit 0 else sudo docker run -dit -p 88:80 -v /task2:/var/www/html --name php_container vimal13/apache-webserver-php fi else echo "Container Unknown" fi
And here we go with the complete pipeline of running your website!
Happy Learning!