Explain CI/CD Pipeline With Example.

A CI/CD pipeline is an automated workflow that enables Continuous Integration (CI) and Continuous Deployment/Delivery (CD). It automates the process of code integration, testing, building, and deployment to ensure fast and reliable software delivery.

CI/CD Pipeline Stages :

A CI/CD pipeline consists of several key stages:

1. Source Code Management (SCM) :
  • Developers write code and push it to a Version Control System (VCS) like GitHub, GitLab, or Bitbucket.
  • Example :
  • git add .
    git commit -m "Added new feature"
    git push origin main
  • Once code is pushed, it triggers the CI/CD pipeline.

2. Continuous Integration (CI) :

* Goal: Automatically build and test the code to detect issues early.

* Build Stage
  • The application is compiled and dependencies are installed.
  • Example (Node.js app) :
  • npm install
    npm run build
  • Example (Java app with Maven) :
  • mvn clean package

* Test Stage
  • Automated tests (unit, integration, functional) run to check code quality.
  • Example (JUnit tests in Java) :
  • mvn test
  • Example (Jest tests for a React app) :
  • npm test
  • If tests fail, the pipeline stops, preventing faulty code from moving forward.

3. Continuous Delivery (CD) :

* Goal: Deploy the application to a staging environment for further testing.

* Deployment to Staging
  • The application is deployed to a staging server (a replica of production).
  • Example: Using Docker & Kubernete
  • docker build -t my-app .
    docker push my-app:latest
    kubectl apply -f deployment.yaml
  •  At this stage, manual approval is often required before moving to production.
4. Continuous Deployment (CD)

* Goal: If Continuous Deployment is enabled, code is automatically deployed to production without manual intervention.

* Production Deployment
  • If all tests pass, the application is automatically deployed to production servers.
  • Example using AWS CodeDeploy
  • aws deploy create-deployment \
      --application-name MyApp \
      --deployment-group-name MyDeploymentGroup \
      --s3-location bucket=my-bucket,key=my-app.zip,bundleType=zip
  •  The application is now live for users!

Example : CI/CD Pipeline Using GitHub Actions :

Let's say we have a Node.js application and want to set up a CI/CD pipeline using GitHub Actions.

* GitHub Actions Workflow File (.github/workflows/ci-cd.yml)

name: CI/CD Pipeline

on:
  push:
    branches:
      - main  # Run pipeline when code is pushed to the main branch

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

      - name: Build application
        run: npm run build

  deploy:
    needs: build-and-test
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to production
        run: echo "Deploying application..."

CI/CD Pipeline Flow Example :

* Developer pushes code to GitHub.
* GitHub Actions automatically runs CI/CD pipeline.
* Tests run to check for errors.
* If tests pass, the build process starts.
* Application is deployed to a staging environment.
* After approval, the app is deployed to production.
* The website/app is now live for users! ?

Benefits of CI/CD Pipelines :

* Automates software development (reduces manual effort).
* Detects issues early (ensures code quality).
* Speeds up deployment (fast and reliable releases).
* Enhances collaboration (developers work seamlessly).