Automation of CI/CD Pipeline

Shikhar Srivastava
5 min readJun 12, 2020

--

CI/CD PIPELINE

A CI/CD Pipeline implementation, or Continuous Integration/Continuous Deployment, is the backbone of the modern DevOps environment. It bridges the gap between development and operations teams by automating the building, testing, and deployment of applications. In this blog, we will learn what a CI/CD pipeline is and how it works through the help of a practical example.

CI stands for Continuous Integration and CD stands for Continuous Delivery/Continuous Deployment. You can think of it as a process similar to a software development lifecycle.

Our task is to automate the entire process, from the time the development team gives us the code and commits it to the time we get it into production. We will automate the pipeline in order to make the entire software development lifecycle in DevOps/automated mode. For this, we will need automation tools like Jenkins, Git/Github and Docker.

Automation Tools

Jenkins provides us with various interfaces and tools in order to automate the entire process. We have a Git repository where the development team will commit the code. Then, Jenkins takes over from there, a front-end tool where you can define your entire job or the task. Then, it moves on to the staging server to deploy it using Docker. It is just like a virtual environment in which we can create a server. It takes a few seconds to create an entire server and deploy what we want to test.

Automation Practical:

The developers generally commit their code to the master branch which is involved in the production. Before pushing it to master branch, they test their code on another branch(dev) to ensure that there is no error in the code. The Q&A team will verify the code and then merge it with the master branch. So we’ll create 3 Jenkins job for this process.

JOB 1: If Developer push to dev branch then Jenkins will fetch from dev and deploy on the dev-docker environment.

JOB 2: If Developer push to master branch then Jenkins will fetch from master and deploy on the master-docker environment.(Note: both dev-docker and master-docker environment are on different docker containers.)

JOB 3: Manually the QA team will check(test) for the website running in the dev-docker environment. If it is running fine then Jenkins will merge the dev branch to master branch and trigger JOB 2.

So here we go….

Note:- I’ll be performing all the steps on RHEL8.

Step 1: Create a git repository locally.

Commands:

  • mkdir directory_name
  • cd directory_name
  • git init
  • git remote add origin github_url
Create an uninitialized git repository

Now our git repository has been created. Upload the code that you require. I’m adding an HTML file.

  • git add homepage.html
  • git commit homepage.html -m “comments”
  • git push -u origin master

Now create a dev branch in the same repo:

  • git branch dev
  • git checkout dev //(to get inside dev branch)
  • git push origin -all

So we are done with the repo creation.

Step 2: Create a webhook in git for automation.

Setup a post-commit that will automatically push the code to the respective branch and auto start the Jenkins job.

Commands:

  • cd .git/hooks/
  • gedit post-commit

Code:

Use your own Jenkins URL, auth_token, username and password.

#!/bin/bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH" == "master" ]
then
git push origin master
curl --user "user_name:password"
Jenkins_URL/job/Job1_master/build?token=auth_token
elif [ "$BRANCH" == "dev" ]
then
git push origin dev
curl --user "user_name:password" Jenkins_URL/job/Job2_dev/build?token=auth_token
fi

Step 3: Start Jenkins and docker service

  • systemctl start jenkins
  • systemctl start docker

Step 4: Create a separate directory to manage the downloaded codes.

  • mkdir production
  • mkdir testing

Step 5: Create and configure Jenkins job1.

Add the GitHub URL
Add a trigger for the auto-build(Job1 will automatically run after a successful build of job3)
Write a command to launch a docker container

Step 6: Create and configure Jenkins job2.

Add the GitHub URL
Add a trigger for auto build
Write a command to launch a docker container

Step 7: Create and configure Jenkins job3.

Add the GitHub URL
Option to merge
Post-build action

Step 8: Now add a new file to dev branch and commit changes. It will automatically start Jenkins job 2 and launch container in docker.

Docker container image for the testing server.

Now the Q&A team will test the website. You can find the IP on which testing server is running by command.

  • docker inspect testing_server

When the testing is done and verified, the Q&A team start the Job3 and merge it with the master branch. After the successful build of Job3, it automatically triggers Job1. Then Job1 will fetch all the files from GitHub repo master branch and deploy it on docker container i.e. the production server. So now our website is available on the production server for public use.

So this marks the completion of our journey of automating our process.

Thank You!!

--

--