Continuous Integration and Continuous Delivery/Deployment (CI/CD): A Beginner’s Guide

Sandeep Singh
4 min readOct 7, 2024

--

In today’s fast-paced software development environment, delivering high-quality applications quickly and efficiently is essential. Continuous Integration and Continuous Delivery (or Deployment), commonly referred to as CI/CD, is a practice that allows development teams to automate the software release process. This automation ensures that code changes are continuously integrated, tested, and deployed, leading to faster and more reliable releases.

In this blog, we’ll break down CI/CD concepts and walk through how to set up a basic CI/CD pipeline using popular tools.

What is CI/CD?

CI/CD is a methodology aimed at improving software development processes. Let’s break it down:

Continuous Integration

Continuous Integration (CI) is a development practice where developers frequently integrate their code changes into a shared repository, preferably several times daily. Each integration is automatically verified by

  1. Building the project and
  2. Running automated tests.

This process allows teams to detect problems early, improve software quality, and reduce the time it takes to validate and release new software updates.

Continuous Delivery (CD):

Continuous Delivery ensures that code that has passed automated tests is always ready to be deployed to production. However, the deployment is still manual, meaning someone from the team triggers the deployment process when necessary.

Continuous Deployment

As the name suggests, deploying your code continuously to various environments (dev/stage/prod)

Difference between Continuous Delivery and Continuous Deployment:
In continuous delivery, deployment is manual but automated in continuous deployment. Both ensure that the code is always in a deployable state.

Why Use CI/CD?

  1. Speed: Automating testing and deployment speeds up the release cycle.
  2. Early Bug Detection: Bugs are detected early through frequent testing, making it easier to fix them.
  3. Collaboration: Multiple developers can work on the same project without worrying about integration issues.
  4. Reduced Risk: By deploying in small, frequent increments, the risk of introducing major bugs is reduced.
  5. Consistent Deployments: Automation ensures that deployments are consistent and reliable.

Setting up CI/CD: A Step-by-Step Guide

Let’s walk through setting up a CI/CD pipeline using GitHub Actions for continuous integration and continuous deployment.

Step 1: Version Control with Git

Before setting up CI/CD, we need to ensure our code is managed in a version control system. Git is the most popular one. You can push your code to GitHub, GitLab, or Bitbucket, which are popular platforms for hosting Git repositories.

a) Create a repository in GitHub by going to https://github.com/ and clicking on New Repository.

b) Clone your repository and start working on your code.

Step 2: Writing tests

CI/CD workflows rely on automated tests to ensure code quality. Let’s assume you’re working on a simple Node.js project and want to add tests.

In Nodejs, you can write tests with the very popular framework Jest.

file.js :

function subtract(a, b) {
return a - b;
}

module.exports = subtract;

file.test.js :

const subtract = require('./file');

test('subtract 6 - 2 to equal 4', () => {
expect(subtract(1, 2)).toBe(4);
});

You can Run the test locally with :

npm test

Step 3: Setting Up Continuous Integration with GitHub Actions

GitHub Actions allow you to automate workflows directly within your GitHub repository. We’ll use it to run our tests automatically whenever a new commit is pushed.

a) In your GitHub repository, create a .github/workflows directory.

b) Inside this directory, create a file named ci.yml with the following content:

name: Node.js CI

on: // this section specifies that the workflow should run when code is pushed to the main branch.
push:
branches:
- main

jobs: // The jobs section defines what will happen, in this case, installing dependencies and running tests using two different versions of Node.js (14 and 16).
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14, 16]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test

c) Commit this file to your repository. GitHub will automatically trigger the action whenever code is pushed to main.

Step 4: Adding Continuous Deployment

Once your tests are passed, the next step is deployment.

For deployment, you would need a platform for deployment. So, let’s say we are deploying our static website on Netlify.

a) Set up your GitHub account with Netlify and the main branch of your repository.

b) Configure GitHub Action to deploy to Netlify.

let’s update our ci.yml file :

name: Node.js CI/CD

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm test

deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy to Netlify
run: |
curl -X POST -d {} https://api.netlify.com/build_hooks/YOUR_BUILD_HOOK

You can get a “Build Hook” from your Netlify dashboard. This triggers Netlify to deploy your site after tests pass.

Step 5: Monitor Your Pipeline

Once you’ve configured everything, any time you push code to your main branch:

  • Tests will automatically run.
  • If tests pass, the site will automatically be deployed to Netlify.

Conclusion

CI/CD pipelines play an essential role in modern software development. They ensure that code is regularly integrated, tested, and deployed with minimal manual effort. By following the steps outlined above, you can set up a simple CI/CD pipeline to automate the testing and deployment of your applications.

If you’re just getting started, tools like GitHub Actions, Netlify, and Jest make it easy to set up a basic CI/CD pipeline. As you grow more comfortable, you can explore more advanced tools and workflows tailored to your team’s specific needs.

--

--

Sandeep Singh
Sandeep Singh

Written by Sandeep Singh

0 Followers

Fullstack Developer, experienced in modern frontend and backend technologies, and always eager to learn and share knowledge with the community.

No responses yet