CI/CD

What is CI/CD?

Think of CI/CD like an automatic quality checker plus a delivery robot for your code. Every push is tested, and every safe build can deploy without manual intervention.

CI/CD pipeline illustration

Steps Involved in CI

  1. Push code to the repository.
  2. Install dependencies in a clean environment.
  3. Run linting and automated tests.
  4. Publish test reports and fail fast on errors.

Steps Involved in CD

  1. Build a production-ready artifact.
  2. Deploy to staging or production.
  3. Run smoke checks and restart services.
  4. Notify the team on success or failure.

Node.js Deploy Example

git push origin main
# GitHub Actions runs tests and builds
# On success, deploy step runs:
ssh user@server "cd /var/www/my-nodejs-app \
  && git pull origin main \
  && npm ci --only=production \
  && pm2 restart my-app"

CI/CD Notes

What is CI/CD?

Think of CI/CD like an automatic quality checker plus a delivery robot for your code.

CI = Continuous Integration

Every time you push code, it automatically tests it to make sure nothing is broken.

CD = Continuous Delivery/Deployment

After tests pass, it automatically deploys your app to the server with no manual work needed.

The Simple Flow

You write code → Push to GitHub → Tests run automatically
→ If tests pass → App deploys automatically
→ If tests fail → You get notified (nothing broken in production)

Steps Involved in CI/CD

Step What Happens
1. Trigger You push code to GitHub.
2. Install Dependencies are installed (npm install).
3. Lint Code style is checked.
4. Test Unit and integration tests run.
5. Build App is built for production.
6. Deploy App is sent to your server or cloud.
7. Notify You get success or failure notification.

GitHub Actions — How It Works

GitHub Actions uses a YAML file stored at the path below. Every time you push code, GitHub reads the file and runs the steps automatically.

your-project/
  .github/
    workflows/
      ci-cd.yml   ← this is your pipeline file
Continuous Integration Every push triggers tests to keep the main branch healthy.
Continuous Delivery When tests pass, new builds move closer to production automatically.
Continuous Deployment Approved builds deploy right away with no manual steps.
You write code → Push to GitHub → Tests run automatically → If tests pass → App deploys automatically → If tests fail → You get notified

Pipeline, Unpacked

Every push to main or develop starts the workflow. Pull requests run CI checks before merging.
Dependencies are installed with npm ci for clean, repeatable builds.
Linting verifies code style, and the Jest suite validates all routes and the 404 handler.
The build step runs, then a secure SSH deploy restarts the server with PM2 when CI passes on main.

GitHub Actions Example

# .github/workflows/ci-cd.yml
name: Node.js CI/CD Pipeline

on:
  push:
    branches: ["main", "develop"]
  pull_request:
    branches: ["main"]

jobs:
  ci:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x, 20.x]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build

  cd:
    runs-on: ubuntu-latest
    needs: ci
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20.x"
          cache: "npm"
      - run: npm ci --only=production
      - uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          script: |
            cd /var/www/my-nodejs-app
            git pull origin main
            npm ci --only=production
            pm2 restart my-app

Quick Summary

Without CI/CD Manual deploys, easy to skip tests, slower feedback.
With CI/CD Automated checks, safe deployments, fast and consistent.