DevOps Automation 2

Poojan Mehta
5 min readAug 12, 2020

→WHAT IS THE PROJECT?

TOOLS USED — GIT, JENKINS, DOCKER

Problem Statement-

  1. JOB1: Pull the GitHub repo automatically when some developer commits.
  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)
  3. JOB3: Test your application if it is working or not.
  4. JOB4: If the application is not working, then send an email to the developer with an error message and if it is running file send a success message.
  5. JOB5: To monitor the container where the application is hosted, fails due to any reason then this job will automatically start the container again.

First Create DockerFiles for 3 different image requirements-

  1. Dockerfile for Jenkins image

~Here, CentOs image is taken and git, python, sudo, net-tools, and similar essential tools are to be downloaded.

~Also, install Jenkins and grant Sudo powers to Jenkins user. Also, add the command to start Jenkins services automatically as soon as the container is launched.

2.Dockerfile for Html image

~Here also, CentOs image is taken and important packages are downloaded and HTTPS service will start after the container is launched.

3.Dockerfile for PHP image

~Same piece of image and packages from the HTML image and PHP package is also added.

  • I’ve passed -y option in every command to ignore user prompt while downloading.
  • The final output of all images after build —
Jenkins_task, html_img, and php_img are dockerfile created and Jenkins service is also started automatically

JOB1-

→This job will pul the code from the GitHub and store it in the Host system in the specified folder.

Here, an already running container of HTML of PHP will stop and code will be copied in the folder named data_folder.

Poll SCM will always monitor the Git repository and pull the code instantly after code is pushed in GitHub.

Job1 success and job 2 triggered

JOB2-

→This job will detect the code and will launch a docker container accordingly.

->If HTML code is pulled from Git then html_img image and if PHP code is copied then php_img image will be used

  • >This job is chained to job1. So, it’ll automatically run just after a successful build of job1.
Job1 pulled HTML data, so a docker container for HTML is launched

JOB3-

→This is the main job!! This is a chained job with upstream job-2 and will trigger after a successful build of job2.

→Here, the HTTP status code of the webserver is used and passed in the condition.

->The status code is to be stored in a variable through the output of the curl command.

  • >Also, Jenkins mail plugin can be used to send customized mail notification.
Configure Jenkins as a mail client. Jenkins will contact the google SMTP server.

-Jenkins is a third party application and will be considered as a third-party application for a google account. Create a 16 digit password and provide it to Jenkins.

  • Add POST BUILD ACTION and select MAIL NOTIFICATION and add the recipient mail address.

JOB4-

→This is an independent job that will continuously monitor the Jenkins container and launch one new container if the running one is failed due to any reason!!

THAT’S IT !! AUTOMATIC JENKINS PIPELINE SET UP IS DONE.

FUTURE SCOPE — INTEGRATING THIS PIPELINE WITH MULTIPLE DOCKERFILES AND LAUNCH DIFFERENT OS ACCORDING TO REQUIREMENT ON THE FLY!

GitHub URL for reference — https://github.com/poojan182/jenkins-pipeline

THANKS A LOT FOR YOUR ATTENTION FOR SO LONG!!

--

--

Poojan Mehta

In a continuous process of Technical Writing. Gathering, Organizing, Crafting the things that make sense.