CI/CD Tutorial For Developers
Tags: tutorial, devops, kubernetes, docker
Published: 2022-12-15 05:53:40

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5s2ej46jktj6hepcayy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ae72bu7jaz1eiofwvvs.png)

### Pre-Requisites
- Create a [free SingleStore account](https://www.singlestore.com/cloud-trial/?utm_campaign=singlestore-for-ai&utm_medium=referral&utm_source=dev.to&utm_term=cicd). 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](https://app.harness.io/auth/#/signup/?utm_source=internal&utm_medium=social&utm_campaign=community&utm_content=pavan_cicd_article&utm_term=get-started) account to do CI/CD (on-premise)
- Kubernetes cluster access from any cloud provider to deploy our application (you can also use [Minikube](https://minikube.sigs.k8s.io/docs/start/) or [Kind](https://kind.sigs.k8s.io/docs/user/quick-start/) to create a single node cluster).
- Docker, preferably [Docker Desktop](https://www.docker.com/products/docker-desktop/)
- Download and install [Node.js](https://nodejs.org/en/download/)

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](https://github.com/pavanbelagatti/harness-ci-example) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bwww9c4hmg3ng2jjb2u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur7vnqorzidnnlr9guyh.png)

Select the Continuous Integration module and add the necessary stages and steps, as shown below.
![cicd with harness images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agj1alkta3airzy01p2r.png)

‘Test’ step set-up is done as below,
![npm test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qemv0rybo1p3yuwh7s00.png)

The “‘Push to Docker Registry” step is as below,
![build and push image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k72jk64budqebn8ud0d2.png)

Next, set up a deployment pipeline.
![CD overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgqu0d6giyzov9c36ac1.png)

Add the required details in the “Service” tab.
![deploy service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xy4mkloyv2fkadc376hx.png)

Define the environment type in the “Environment” tab.
![environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbh51pgvrkj3pqobgqre.png)

Strategise the execution by selecting which deployment you prefer.
![execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24nmlpbdl0lttx1irff3.png)

Save everything and run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20qaj71244gnsgjnj0tc.png)

You can see both CI and CD getting executed one by one with all the steps specified.
![CI/CD execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg40dwp34ga5dn1oussl.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvj7ptjvb0lmpmz3p8jt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fheffhlvwvcz2jty4xq.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27rr9rp2acjmrb34py5g.png)

#### 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](https://www.atlassian.com/team-playbook/examples/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.