Skip to content

CI/CD Pipeline

Continuous Integration and Continuous Deployment (CI/CD) automate the process of testing, building, and deploying your code. This guide covers setting up a modern pipeline.

What is CI/CD?

  • CI (Continuous Integration) — Automatically test and build code on every push
  • CD (Continuous Deployment) — Automatically deploy validated builds to production

GitHub Actions Example

Create .github/workflows/deploy.yml:

name: CI/CD Pipeline

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Run tests
        run: npm test

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t my-app:${{ github.sha }} .

      - name: Push to registry
        run: |
          echo ${{ secrets.REGISTRY_TOKEN }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin
          docker push my-app:${{ github.sha }}

  deploy:
    needs: build
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to production
        run: |
          # Your deployment script here
          echo "Deploying version ${{ github.sha }}"

Pipeline Stages

A typical CI/CD pipeline follows these stages:

Code Push → Lint → Test → Build → Deploy (Staging) → Deploy (Production)
StagePurposeFailure Action
LintCode style checksBlock merge
TestUnit & integration testsBlock merge
BuildCompile & packageBlock deploy
Deploy StagingTest in staging envBlock production
Deploy ProductionRelease to usersRollback

Best Practices

  1. Run tests on every PR — Never merge untested code
  2. Keep pipelines fast — Target under 10 minutes for feedback
  3. Use caching — Cache dependencies between runs
  4. Deploy incrementally — Use canary or blue-green deployments
  5. Monitor after deploy — Set up alerts for error rate spikes
  6. Store secrets securely — Use your CI platform’s secret management