DEV Community

Cover image for CI/CD Tutorial For Developers
Pavan Belagatti
Pavan Belagatti

Posted on • Updated on • Originally published at harness.io

CI/CD Tutorial For Developers

DevOps continues to grow in popularity among software development teams. It’s not surprising, given the benefitss: a streamlined workflow leads to faster release frequency and deployment cycles, quicker bug detection, efficient rollback in case of any error, and a more productive team overall. DevOps engineers ensure that the processes and practices of code development, testing, and release are streamlined to reduce inefficiency, risk, and time to market. Coupled with continuous integration (CI) and continuous delivery (CD), DevOps helps IT organizations streamline their development process by automating manual tasks related to code deployment. Let’s explore how you can adopt CI/CD to improve your organization’s efficiency and delivery pipeline.

Continuous Integration

First, let’s take a look at what CI/CD is and what its benefits are. Although these two concepts are often discussed together, each has its own role in the software development process.

Continuous integration is the process of automating the build and testing of code. It requires a SaaS build service (like Harness Hosted Builds) or an on-premise build server responsible for taking code from a source control system (such as Git), compiling it, and testing it to ensure it’s error-free. This helps to keep software projects on track by ensuring that errors/mistakes/bugs get spotted as soon as they occur. If a build is broken, it halts the rest of the process until it’s been fixed. As a result, the build process is much more streamlined.

Continuous Delivery & Deployment

Continuous delivery automates the release process. It ensures that code is always in a releasable state. This process includes taking the code from the build system and routing it through a staging environment to ensure it’s suitable for release. The outcome is a fully automated deployment process, which can be controlled by the team or triggered by triggers outside the team (such as when a developer pushes or commits a code to the main branch). The key difference between CI and CD is that the former is focused on building code, while the latter focuses on releasing the final product.

The below diagram depicts the difference between CI, CD, and continuous deployment.
ci and cd image

Continuous deployment is the final and most desired stage of the CI/CD process. It’s the point at which the team can deploy the code with zero-touch automation. This is the difference between continuous delivery and deployment. When the deployment of the code is done via manual intervention, it is delivery. However, if the code is deployed in an automated fashion without any manual intervention, it is called continuous deployment.

How to Implement CI/CD in Your Organization

  1. First, you’ll start with CI. This process ensures the code is tested and is clean and ready to deploy.
  2. You can then move on to the CD part, where you’ll be able to deploy code to staging and ready for review by the team.
  3. You can deploy the code to the production environment once everything is approved.

CI/CD Tutorial

Today, we will show you how to implement CI/CD in minutes with a simple setup.
CI/CD flowchart

Pre-Requisites

  • Create a free SingleStore account. For this tutorial, we'll be using SingleStore as our database solution. SingleStore is a high-performance, in-memory database that supports both SQL and NoSQL data models.
  • Free Harness account to do CI/CD (on-premise)
  • Kubernetes cluster access from any cloud provider to deploy our application (you can also use Minikube or Kind to create a single node cluster).
  • Docker, preferably Docker Desktop
  • Download and install Node.js

First, we will create a simple “Hello World!” application in Node.js with a simple test case. I have already created this simple “Hello World!” application to make it easier and pushed it to GitHub. You can fork this repo to start working on it.

The Dockerfile you see in the repo will be used to build and push our application as an image to the Docker Hub. The next thing is we will build the image and push it to the Docker Hub using the command,

docker buildx build --platform=linux/arm64 --platform=linux/amd64 -t docker.io/<docker hub username>/<image name>:<tag> --push -f ./Dockerfile .

Once the build and push are successful, you can confirm it by going to your Docker Hub account.
DockerHub image

You can see the deployment.yaml file in the forked repo, which defines the deployment yaml file to help us deploy the application to our Kubernetes cluster. At this point, make sure your Kubernetes cluster is up and running.

Once everything is set, it is time to set up a Harness account to do CI/CD. Create a free Harness account and your first project. Once you sign-up at Harness, you will be presented with the new CI/CD experience and capabilities.

Add the required connectors, GitHub repo, Docker Hub and secrets, if any. Delegate in Harness is a service/software you need to install/run on the target cluster [Kubernetes cluster in our case] to connect your artifacts, infrastructure, collaboration, verification and other providers with the Harness Manager. When you set up Harness for the first time, you install a Harness Delegate.
Harness Connectors

Select the Continuous Integration module and add the necessary stages and steps, as shown below.
cicd with harness images

‘Test’ step set-up is done as below,
npm test

The “‘Push to Docker Registry” step is as below,
build and push image

Next, set up a deployment pipeline.
CD overview

Add the required details in the “Service” tab.
deploy service

Define the environment type in the “Environment” tab.
environment

Strategise the execution by selecting which deployment you prefer.
execution

Save everything and run the pipeline.
run pipeline

You can see both CI and CD getting executed one by one with all the steps specified.
CI/CD execution

Congratulations! We successfully built, and tested the application code and deployed it onto our Kubernetes cluster using the Harness platform.

You can confirm this deployment by using the command kubectl get pods
kubernetes deployments

You can see two replicas running as per our specifications on the deployment.yaml file. Also, confirm the same by going to your Kubernetes dashboard. Since I am using Google cloud (GCP), I can see and confirm that there are two pods running.
kubernetes pods

Harness platform makes it easy for developers to streamline their SDLC by leveraging the different modules available. Today we saw CI and CD modules, and Harness has a total of seven modules as of now.
Harness CI/CD modules

CI/CD and DevOps

CI/CD is an essential part of any DevOps strategy. It helps to automate the code review and testing process, making it easier for teams to test and deploy software. It’s also a crucial part of creating a culture of continuous improvement. It is also important to remember that CI/CD is not a magic bullet. Laying the DevOps culture and methodologies are the initial steps. It is also important to remember that these processes and methodologies under DevOps are not static; they are ever-evolving and should be tweaked as needed to meet the team’s needs.

Oldest comments (1)

Collapse
 
qq449245884 profile image
qq449245884

Dear Pavan Belagatti,may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.