Automation of CI/CD Pipeline: Part II

Shikhar Srivastava
5 min readJun 13, 2020

--

Automation is the ultimate need for DevOps practice and ‘Automate everything’ is the key principle of DevOps. In DevOps, automation kick starts from the code generation on Developers machine till the code is pushed to the code and even after that to monitor the application and system in production.

Entire DevOps pipeline containing continuous integration, continuous testing, and continuous deployment, including application performance monitoring in live is automated.

Automating infrastructure set up and configurations and the software deployment is the key highlight of DevOps practice. DevOps practice is heavily dependent on Automation in order to make deliveries over a period of a few hours, and make frequent deliveries across platforms.

Thus, automation in DevOps fosters speed, greater accuracy, consistency, reliability and increases the number of deliveries. Ultimately, automation in DevOps encapsulates everything right from building, deploying and monitoring.

In this article, I am going to demonstrate a project which involves complete integration of git, docker, Jenkins and python. Unlike my previous article, this project will involve complete automation of the whole process from the deployment of code from developer to its proper execution and monitoring.

Project Overview:

We have to create a container image that has Jenkins installed using Dockerfile. When we launch this image it should automatically start the Jenkins service in the container. Now, we have to create 5 jobs in Jenkins that will fetch the code, start the container image according to the code, test the code, notify in case of failure and continuous monitoring of the container so that it can restart it in case of any failure.

Tasks to perform:

1. Create container image that’s has Jenkins installed using docker file.

2. When we launch this image, it should automatically start the 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 installed 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 the app is not working, then send an email to the developer with error messages.

8. Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.

Step 1: We have to create a docker image using docker file which will automatically launch the Jenkins service. This file has all the necessary software which will be required further.

Dockerfile

Commands to build and start the docker image:

  • docker build -t <container_name:version> <path_of_dockerfile>
  • docker run — —it — —priviliged -p 7000:8080 -v /:/base — —name <image_name> <container_name:version>

So now our container is ready. You can also use tunelling to get a public ip.

Step 2: Create Jenkins job 1.

The developer pushes all the code to Github. Job 1 will download the uploaded code to our local system.

Pulls the code from GitHub repo

We have set a trigger to monitor this process automatically.

Set up a trigger to monitor

Copy the files to our base os.

Copy the downloaded files to our local storage

Step 3: Create Jenkins job 2.

This job will check the type of code whether it is HTML or PHP code. Then it will automatically launch the respective language container to perform execution of the code.

So for this, we need to create separate docker images for both HTML and PHP. You can also download the pre-created images from dockerhub.com.

Docker image for HTML website
Docker image for PHP website

When job 1 is run successfully, job 2 will be automatically launched.

Trigger only after a successful build of job1

It will verify the code for its type and then start the respective docker image automatically.

Check for type of code

Step 4: Create Jenkins job 3.

This job will run our code and check whether it is working properly or not. In case of any failure, it will trigger a new job which will notify the developer about the error.

Job3 will trigger after a successful build of job 2
Code to start and verify our app

Step 5: Create a Jenkins job 4.

This job will be trigger if job 3 fails to execute properly. It will send an email to the developer regarding the error. I have used python script to generate an email message. You can also use the email plugin of Jenkins for this purpose.

Python script to send mail

Step 6: Create a Jenkins job 5.

Suppose our container on which our app is deployed fails due to any reason, it will cause big damage to our clients who are using our infrastructure. In order to avoid this, job 5 will continuously monitor the container and in case of any failure, it will restart the container immediately.

Code to restart the container

After the successful execution of all the jobs, the build pipeline view will look like this. All the processes have been automated.

Build Pipeline

So this marks the completion of our project.

Thank you!

--

--