CI/CD with Jenkins

CI/CD with Jenkins

Introduction

Hey folks, long time no see. Apollogies for being inconsistent in my blogs. But yeah im back and here im. So far i have talked about linux fundamentals, CICD in my previous blogs and now we will move a step forward into CICD world.
Imagine you're baking a cake. You mix the ingredients, bake the cake, and then decorate it. But what if you could check the cake's batter for consistency before baking, test the baking temperature to ensure even cooking, and have a frosting machine that automatically applies the finishing touches? That's essentially what CI/CD (Continuous Integration and Continuous Delivery) does for software development.

CI/CD is a modern approach to software development that automates the processes of building, testing, and deploying software. It's like having a team of tireless assistants working behind the scenes, ensuring that your software is always up-to-date and error-free.

In today's fast-paced world, software companies need to release new features and updates frequently to stay ahead of the competition. CI/CD helps them do this by automating the steps involved in getting software from code to production. This means that developers can spend less time on manual tasks and more time writing new code and fixing bugs.

So lets not wait and move to the actual stuff which we are gonna building through this blog. We will be setting up a Jenkins Pipeline for deploying a React(Vite) application using Docker, AWS EC2 and yeah Github.

1.Setting up an EC2 server on AWS:

We will be doing everything inside this EC2 machine thus its important to first create a machine on AWS. I have explained how to claim the free-tier for AWS in my previous blog, here is the link :

https://blog.ivinayak.tech/cicd-pipeline-with-aws-elastic-beanstalk

So go ahead and launch a EC2 instance through the console with an ubuntu os.

2.Setting up Jenkins

Next we will be setting up Jenkins, For the people who are not aware of Jenkins, Jenkins is a popular open-source automation server that can be used to orchestrate the CI/CD pipeline. It provides a flexible framework for building, testing, and deploying software across various platforms. Not just software it can be used to automate anything from Linux shell scripts to python scrappers.
I Will surely create another blog where we will explore about jenkins working in more detailed manner.
To run Jenkins you need Java-jdk installed on your server.
Here is the step by step procedure which will help you achieve that !

  1. sudo apt update

  2. sudo apt install openjdk-11-jre

  3. java -version

  4. curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null

  5. echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null

  6. sudo apt-get update

  7. sudo apt-get install jenkins

  8. sudo systemctl enable jenkins

  9. sudo systemctl start jenkins

sudo systemctl status jenkins

After completing the startup procedure you will be redirected to this Jenkins homepage:

3.Configuring Jenkins for React Vite

At the core of Jenkins' functionality lie jobs and pipelines. Jobs represent individual tasks within the CI/CD process, such as building, testing, or deploying. Pipelines, on the other hand, are sequences of interconnected jobs that form a complete CI/CD workflow.

Creating a Simple Jenkins Job:

  1. Create a New Job: Access the Jenkins dashboard and click "New Item." Choose the desired job type, such as "FreeStyle Project."

  2. Configure the Job: Provide a descriptive name for the job and define the steps to be executed. Utilize the "Build Trigger" section to specify when the job should run, such as upon code changes.

  3. Save the Job: Click the "Save" button to store the newly created job.

make sure your configuration looks like this ! This will make sure the build process is done perfectly.If you notice we are creating a docker image every time the job is run with the job number as tag to the docker image. This will help us maintaining the artifacts incase of rollback. We are also going to understand the concept of rollback to the previous deployement, why it is important and how can you achieve it !

4.Rollback to previous deployments

So far now we have created a CI/CD pipeline for triggering the github commits, creating the build and deploying it inside the same EC2 machine via docker containers.
Let's imagine a scenario, Imagine you're developing a new feature for an e-commerce website. This feature allows customers to save their shopping carts and resume their purchases later. You've thoroughly tested the feature in your development environment and are ready to deploy it to production.

Following your CI/CD pipeline, the new code is automatically deployed to the production environment. However, shortly after the deployment, users start reporting issues with saving their shopping carts. Upon investigation, you discover that the new feature is causing unexpected errors and preventing users from completing their purchases.

In this scenario, a rollback mechanism proves invaluable. By quickly reverting to the previous stable version of the software, you can restore the e-commerce website's functionality and prevent further disruption to users. Without a rollback mechanism, you would need to manually fix the issue and redeploy the code, potentially causing extended downtime and frustration for users.

Recognizing the severity of the problem, you decide to activate the rollback mechanism. This mechanism automatically reverts the shopping cart page to the previous stable version, effectively undoing the recent deployment.

Within minutes of initiating the rollback, the problematic shopping cart page is replaced with the stable version. Users are able to add items to their carts and proceed through the checkout process without encountering errors. The rollback minimizes downtime and prevents further disruption to the user experience.

Let's make this mechanism see in action and how can you achieve this, remember We had stored the docker images with specific versioning of jobs?
This is the time where we will be utillizing them.

Steps to Implement Rollback:

  1. Create a Freestyle Project: In Jenkins, create a new Freestyle Project. This project will be responsible for invoking the Docker container to stop and restart it with the previous version image.

  2. Add Shell Script: In the project configuration, add a Shell Script build step. This script will be responsible for executing the Docker commands to stop and restart the container.

  3. Docker Commands: The Shell Script should include the following Docker commands:

docker stop jenkins-node-app || true
docker rm jenkins-node-app || true
docker run -d --name jenkins-node-app -p 3000:3000 "jenkins-node:${Rollback_version}"

I hope this has put some light in understand the CI/CD mechanism and how the jenkins flow works. So what are you waiting for? It's time to soar to new heights, armed with the knowledge and tools you've gained. Go create your first CI/CD pipeline using jenkins !!!

See you in the cloud! :)