Complete CI/CD pipeline

akshayca

Akshay C A

Posted on February 24, 2022

Complete CI/CD pipeline

A CI/CD pipeline is a series of steps that must be performed in order to deliver a new version of the software. Continuous integration/continuous delivery (CI/CD) pipelines are a practice focused on improving software delivery using either a DevOps or site reliability engineering (SRE) approach.

I've created a simple CI/CD pipeline with -

  • GitHub for SCM.
  • Jenkins.
  • Docker.
  • Docker Hub.
  • AWS EC2 Instances.
  • AWS EKS service.

Let's begin!

Step 1

Create an EC2 instance to run Jenkins.

I'm going to create an AWS EC2 instance and install docker in it to run Jenkins.

Image description
Image description
Image description

It is best practice for a user to access Jenkins with limited permission, so let's create a Jenkins user for this purpose.

Image description

Once this Jenkins container is up, edit the inbound rules to expose port 8080.

Image description

Let's finish up the initial settings

Image description
Image description
Image description

You need to make the data persist even after you restart the server.
So, stop the container, re-run docker the volume defined.

Image description

Next, provide the required access to docker.

Login as root user.
Image description

Image description
Image description

So, we now have Jenkins up and running.

Image description

Step 2

Push your code to GitHub.

For this project, we will use the Java Maven App with a Postgres database.

You can clone my project for your reference - Repo Link

Image description

Here is the list of tasks that needs to be performed by Jenkins -

  1. Build the solution and increment the version number.
  2. Build the Docker Image using the Dockerfile and push the Image to the DockerHub.
  3. Trigger the automation tests.
  4. Deploy the App to a K8s cluster.
  5. Commit version update to SCM.

Step 3

Create a simple pipeline project in Jenkins.

  • Set GitHub hook trigger for GITScm polling
  • Provide the Git Repository URL and credentials
  • Script Path as Jenkinsfile

Image description

Image description

Image description
Image description

Image description

Step 4

Create Jenkins shared library.

Jenkins Shared Libraries are written in Groovy and allow you to create common sets of logic, and share that among teams/projects/organizations.

I've created one for this project - Repo Link

Update your Jenkins file to read the shared library.

Image description

Increment version-
Image description
Image description

Build the Jar-
Image description
Image description

Build the Docker Image and Push it to the DockerHub -
Image description
Image description
Image description
Image description
Image description
Image description
Image description

Now if everything is configured properly, whenever you push your code to the master. Jenkins should trigger the pipeline and perform the below actions.

  • Clone the git repository
  • Increment the version number
  • Build the Jar file
  • Build Docker Image
  • Push the Image to Docker Hub

Image description

Step 5

Deploy to K8s cluster

Now we are supposed to trigger the automation tests. Once all the tests are passed and all the checklists are cleared, we can deploy our App to the production.

Install Kubectl command-line tool inside Jenkins container.
Install aws-iam-authenticator tool inside Jenkins container.
Image description

I've create an EKS cluster called demo-cluster.
Image description
Image description

Create Kubeconfig file to connect to EKS cluster.

  • contains all the necessary for authentication Image description

Image description

Image description

Add AWS credentials on Jenkins for AWS account authentication
Image description

Adjust Jenkinsfile to configure EKS cluster deployment.
Image description
Image description

Image description

Image description

The last thing which you need to do once the deployment is successful is to commit the version update to the code repository.
Image description

The pipeline is now complete.

Now let's run it !!!

Push some changes to the Master branch our pipeline will be triggered.

Image description

Check the logs

Image description
Image description
Image description
Image description
Image description
Image description
Image description
Image description

Now let's connect to the EKS cluster from our local machine to verify.

Image description
Image description
Image description

Image description

Image description

Thank you!
Please comment for feedback.
Happy learning :)

Overview of our CI/CD pipeline -

Image description

💖 💪 🙅 🚩
akshayca
Akshay C A

Posted on February 24, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Complete CI/CD pipeline
devops Complete CI/CD pipeline

February 24, 2022