Production CI/CD Pipelines with GitHub Actions, Docker & Automated Testing

The Modern Continuous Delivery Mandate

In high-velocity software engineering teams, manual deployments are an unacceptable operational liability. Manual SSH commands, ad-hoc build scripts, and untested branch merges inevitably lead to deployment downtime, environment drift, and regression leaks.

A production-grade CI/CD pipeline establishes automated quality gates: every pull request is linted, unit tested, verified for security vulnerabilities, built into an immutable container image, and deployed to staging or production without human intervention.

In this guide, we construct an end-to-end continuous integration and deployment pipeline using GitHub Actions, Docker Buildx with layer caching, Trivy security scanners, and zero-downtime deployment triggers.


Production CI/CD Architecture Flow

Continuous Delivery Pipeline Architecture:

Git Push / PR ──► GitHub Actions Runner
                        │
         ┌──────────────┴──────────────┐
         ▼                             ▼
   [Lint & Typecheck]            [Unit Tests]
         │                             │
         └──────────────┬──────────────┘
                        ▼
             [Security Vulnerability Scan]
             (Trivy CVE & Dependency Audit)
                        │
                        ▼
             [Docker Buildx Build & Push]
             (GitHub Container Registry ghcr.io)
                        │
                        ▼
             [Zero-Downtime Production Deploy]
             (SSH / Webhook / Container Rolling Update)

Complete GitHub Actions Workflow YAML

Save the following workflow configuration in .github/workflows/deploy.yml in your repository root:

name: Production CI/CD Pipeline

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

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  # -------------------------------------------------------------
  # Job 1: Automated Quality & Test Gate
  # -------------------------------------------------------------
  test-and-verify:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v4

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

      - name: Install Dependencies
        run: npm ci

      - name: Execute Linter & Type Check
        run: |
          npm run lint
          npm run typecheck

      - name: Execute Automated Test Suite
        run: npm test -- --coverage

  # -------------------------------------------------------------
  # Job 2: Security & Vulnerability Scanning
  # -------------------------------------------------------------
  security-audit:
    needs: test-and-verify
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Run Trivy Vulnerability Scanner
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          ignore-unfixed: true
          severity: 'CRITICAL,HIGH'
          exit-code: '1'

  # -------------------------------------------------------------
  # Job 3: Build & Push Docker Container (Only on Main)
  # -------------------------------------------------------------
  build-and-deploy:
    needs: [test-and-verify, security-audit]
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=sha,format=long
            type=ref,event=branch
            latest

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Trigger Rolling Deployment via SSH
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.PROD_SERVER_IP }}
          username: ${{ secrets.PROD_SERVER_USER }}
          key: ${{ secrets.PROD_SSH_PRIVATE_KEY }}
          script: |
            cd /opt/production-app
            docker compose pull app
            docker compose up -d --no-deps --build app
            docker image prune -f

Best Practices for Fast & Secure Pipelines

  1. Use GitHub Actions Cache (type=gha): Docker layer caching via Buildx prevents rebuilding unchanged npm packages or OS layers, cutting build times from 6 minutes to under 30 seconds.
  2. Never Hardcode Secrets: Always store database credentials, server IPs, and SSH keys in GitHub Repository Secrets.
  3. Pin Action Versions: Pin GitHub Actions to major versions (e.g. actions/checkout@v4) to receive security patches without breaking breaking API changes.
  4. Prune Old Images: Always run docker image prune -f on production servers after deployments to prevent lingering build artifacts from consuming server disk space.

Frequently Asked Questions (FAQ)

How does Docker layer caching work in GitHub Actions?

By specifying cache-from: type=gha and cache-to: type=gha,mode=max, Docker exports intermediate layer tarballs to GitHub’s distributed cache service. Subsequent CI runs pull existing layers over internal 10Gbps network links rather than rebuilding them from scratch.

How do I achieve zero-downtime during rolling deployments?

Combine Nginx or Traefik reverse proxies with Docker healthchecks. In Docker Compose, configure a rolling update policy: start the new container, verify the healthcheck endpoint returns HTTP 200, and only then drain traffic from the old container and shut it down.


Continue expanding your technical depth with our related engineering guides:

  • The Complete Docker Handbook: Comprehensive container architecture and production Dockerfile optimization. Explore the complete guide: The Complete Docker Handbook.
  • Nginx Reverse Proxy & SSL Hardening: Hardening Nginx reverse proxies with TLS 1.3 and security headers. Explore the complete guide: Nginx Reverse Proxy & SSL Hardening.

Published by Mohit Kumar under DevOps & Cloud Infrastructure. Read our complete guide on Deploying Node.js & PostgreSQL with Docker Compose.

Related Modular Guides & Next Steps

Authoritative Reference: Consult the GitHub Actions Official Documentation for normative standards and release specifications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top