Skip to content
WritingArticle
  • #cicd
  • #devops
  • #github-actions
  • #aws
  • #interview

How to Build a CI/CD Pipeline: A Beginner's End-to-End Guide (With Interview Prep)

Pawan Joshi

6 min read

Share
Cover image for “How to Build a CI/CD Pipeline: A Beginner's End-to-End Guide (With Interview Prep)”

Every time you manually zip code, SSH into a server, or click "Deploy" in a dashboard, you risk forgetting a step or shipping broken code.

A CI/CD pipeline automates that entire journey—from git push to a live, verified production release. This guide walks through how CI/CD works from start to end, shows you a real GitHub Actions pipeline you can copy today, and finishes with the top CI/CD interview questions you'll be asked in DevOps and backend interviews.

End-to-end CI/CD pipeline flow from git push to production deploymentCI checks every pull request; CD ships passing builds through staging to production.


1. CI vs. CD (In Plain English)

  • Continuous Integration (CI): Every time a developer pushes code or opens a Pull Request (PR), an automated runner checks out the code, installs dependencies, runs linters, runs tests, and builds an artifact. If anything fails, the merge is blocked.
  • Continuous Delivery vs. Continuous Deployment (CD):
    • Continuous Delivery: Every passing build is packaged and ready to deploy to production, but a human clicks "Approve" before it goes live.
    • Continuous Deployment: Every passing build on main deploys to production automatically with no manual gate (relying on automated tests, canary rollouts, and monitoring).

2. Every Stage of a Real CI/CD Pipeline (Start to End)

  1. Trigger (git push / Pull Request): GitHub sends a webhook to the CI runner (GitHub Actions, GitLab CI, etc.).
  2. Checkout & Cache: The runner clones the repo and restores cached dependencies (node_modules, ~/.cache/pip) so builds stay fast.
  3. Static Analysis (Lint & Typecheck): Catches syntax bugs, formatting issues, and TypeScript/type errors in seconds.
  4. Automated Tests: Runs unit tests (fast, mocked) and integration tests.
  5. Build & Package: Compiles the app (e.g. next build, docker build, or sam build) and outputs a versioned artifact.
  6. Security & Dependency Scan: Checks for leaked secrets and known CVEs (npm audit, Trivy, Dependabot).
  7. Deploy to Staging: Deploys to a production-like environment first.
  8. Smoke / E2E Tests: Hits /health or runs Playwright against staging to verify the app actually boots and responds.
  9. Deploy to Production: Rolls out to prod using Rolling, Blue/Green, or Canary strategy.
  10. Monitor & Rollback: Watches error rates and latency; rolls back automatically if errors spike.

3. Build Your First Pipeline with GitHub Actions

Create .github/workflows/ci-cd.yml in your repository. This real-world workflow runs CI on every PR, and deploys to AWS on merges to main using OIDC (no static AWS keys!).

Deploying from GitHub Actions to AWS using OIDC short-lived credentialsGitHub Actions exchanges a signed OIDC JWT with AWS STS for short-lived credentials.

name: CI/CD Pipeline
 
on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
 
permissions:
  id-token: write   # Needed for AWS OIDC
  contents: read
 
jobs:
  ci:
    name: Lint, Test & Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
 
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
 
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build
 
  deploy:
    name: Deploy to AWS (main only)
    needs: ci
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
 
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/github-actions-deploy
          aws-region: us-east-1
 
      - uses: aws-actions/setup-sam@v2
      - run: sam build
      - run: sam deploy --no-confirm-changeset --no-fail-on-empty-changeset

Notice three best practices in that file:

  • npm ci (clean install from lockfile) instead of npm install.
  • needs: ci ensures deploy never runs if tests fail.
  • OIDC (id-token: write) means zero AWS_ACCESS_KEY_ID secrets to leak or rotate.

4. Deployment Strategies You Must Know

Comparison of Rolling, Blue-Green, and Canary deployment strategiesRolling saves infra cost, Blue/Green gives instant switchover, and Canary minimizes blast radius.

  • Rolling Update: Replaces old instances with new ones in small batches. Low extra cost, but rollback takes time.
  • Blue/Green: Runs two identical environments (Blue = live, Green = new). Once Green passes health checks, the load balancer flips 100% of traffic to Green. Instant rollback by flipping back to Blue.
  • Canary Release: Routes 5% → 25% → 100% of traffic to the new version while watching error metrics. Smallest blast radius if a bug slips through.

5. CI/CD Interview Prep (Top Questions & Answers)

Q1. What is the difference between Continuous Delivery and Continuous Deployment?

Both automate building, testing, and packaging. Continuous Delivery stops at a manual approval gate before production, whereas Continuous Deployment automatically pushes every passing build to production.

Q2. Why use OIDC instead of storing AWS Access Keys in GitHub Secrets?

Static IAM keys never expire and are a major leak risk. With OIDC, GitHub Actions requests a short-lived token from AWS STS scoped to a specific repository and branch, which automatically expires after the job finishes.

Q3. How do you handle database migrations in a zero-downtime CI/CD pipeline?

Use the Expand-and-Contract pattern:

  1. Expand: Add new columns/tables (nullable or with defaults) in a backward-compatible way so old code still works.
  2. Deploy: Ship the new application code that writes to both or reads the new schema.
  3. Contract: Remove old columns in a later cleanup migration once no old app instances remain.

Q4. How do you speed up a slow CI pipeline?

  • Cache package manager directories (npm, pip, Docker layers).
  • Run independent jobs (lint, unit tests, security scan) in parallel.
  • Use matrix/sharded test runs for large suites.
  • Build the artifact once in CI and reuse it across staging and prod instead of rebuilding.

Q5. What should trigger an automated rollback?

Elevated HTTP 5xx error rates, p95/p99 latency spikes, failed health checks, or CloudWatch Alarm triggers during a canary/blue-green rollout.

Q6. What's the difference between npm install and npm ci in CI?

npm ci installs exact versions from package-lock.json, deletes node_modules first for a clean state, and fails if package.json and package-lock.json are out of sync—making CI builds reproducible.

Q7. How do you handle flaky tests in a pipeline?

Never just ignore them or hit "re-run" forever. Quarantine flaky tests into a non-blocking suite with a ticket to fix the root cause (usually shared state, race conditions, or real network/time dependencies), while keeping the main PR gate deterministic.


Next Steps

Want to put this into practice? Use this CI/CD workflow to auto-deploy the Serverless URL Shortener on AWS from Week 4 of the 4-Week Serverless Roadmap.

Found this useful?

Share it with your group