Design production-grade CI/CD pipelines using GitHub Actions with matrix builds, environment protection rules, and automated rollback strategies.
Beyond Basic Workflows
Most teams start with a simple GitHub Actions workflow that runs tests on push. But production-grade CI/CD requires much more: matrix builds across runtime versions, environment protection rules, secrets management, caching strategies, artifact handling, and automated rollback triggers.
This guide covers advanced patterns that we implement when setting up DevOps pipelines for engineering teams shipping to production multiple times per day.
Pipeline Architecture
A robust CI/CD pipeline has distinct stages with clear gates between them:
Build Stage: Compile, lint, and run unit tests. Fail fast on code quality issues.
Test Stage: Integration tests, E2E tests, and security scans run in parallel. Each test suite runs in its own job for isolation and parallelism.
Deploy Stage: Progressive deployment through staging, canary, and production environments with manual approval gates.
Advanced Workflow Configuration
name: Production Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
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 run build
- uses: actions/upload-artifact@v4
if: matrix.node-version == 20
with:
name: build-output
path: dist/
retention-days: 5
test:
needs: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
test-suite: [unit, integration, e2e]
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: test_db
POSTGRES_PASSWORD: test_pass
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm run test:${{ matrix.test-suite }}
env:
DATABASE_URL: postgres://postgres:test_pass@localhost:5432/test_db
Key Design Decisions
The concurrency block cancels in-progress runs when new commits arrive on the same branch - essential for busy repositories where developers push frequently.
Setting fail-fast: false on the test matrix ensures all test suites complete even if one fails. This gives developers complete failure information in a single run rather than requiring multiple push-and-wait cycles.
Environment Protection Rules
GitHub Environments provide approval gates and deployment tracking:
deploy-staging:
needs: [test]
runs-on: ubuntu-latest
environment:
name: staging
url: https://staging.example.com
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
path: dist/
- name: Deploy to staging
run: |
aws s3 sync dist/ s3://staging-bucket/ --delete
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.CF_DIST_ID }} \
--paths "/*"
deploy-production:
needs: [deploy-staging]
runs-on: ubuntu-latest
environment:
name: production
url: https://example.com
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
path: dist/
- name: Deploy to production
run: |
aws s3 sync dist/ s3://production-bucket/ --delete
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.PROD_CF_DIST_ID }} \
--paths "/*"
Configure the production environment in GitHub to require manual approval from team leads. The workflow pauses at the deploy-production job until an authorized reviewer approves.
Caching for Speed
Aggressive caching cuts build times by 60-80 percent:
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
~/.npm
node_modules
.next/cache
key: deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
deps-${{ runner.os }}-
Automated Rollback
Implement post-deployment health checks that trigger automatic rollback:
- name: Smoke test
id: smoke
continue-on-error: true
run: |
for i in {1..5}; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://example.com/healthz)
if [ "$STATUS" != "200" ]; then
echo "Health check failed with status $STATUS"
exit 1
fi
sleep 5
done
- name: Rollback on failure
if: steps.smoke.outcome == 'failure'
run: |
aws s3 sync s3://production-backup/ s3://production-bucket/ --delete
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.PROD_CF_DIST_ID }} \
--paths "/*"
echo "::error::Deployment rolled back due to failed health checks"
exit 1
Security Scanning
Add SAST and dependency scanning as required checks:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
severity: 'CRITICAL,HIGH'
exit-code: '1'
Best Practices
- Keep workflows DRY using reusable workflows and composite actions
- Pin action versions to specific SHA hashes, not tags, for supply chain security
- Use OIDC for AWS authentication instead of long-lived access keys
- Store deployment metadata (commit SHA, timestamp, actor) for audit trails
- Set up monitoring and alerting to catch issues that automated tests miss
Implementing these patterns transforms CI/CD from a simple automation layer into a reliable software delivery system.
Frequently asked questions
How do I cancel outdated GitHub Actions runs when a new commit is pushed?
Add a concurrency block whose group combines github.workflow and github.ref, and set cancel-in-progress to true. A new push to the same branch then cancels the run already in progress for that workflow. The setting matters most on busy repositories where developers push frequently.
How do I test against several Node.js versions in GitHub Actions?
Define a strategy matrix with node-version set to [18, 20, 22] and pass matrix.node-version to actions/setup-node@v4 with cache set to npm. Each version then runs npm ci, lint and build as its own job. To keep a single copy of the build output, upload the artifact from one version only, for example with an if condition of matrix.node-version == 20 on the actions/upload-artifact@v4 step.
What does setting fail-fast to false do in a GitHub Actions test matrix?
With fail-fast set to false, every suite in the matrix runs to completion even when one of them fails. For a matrix of unit, integration and e2e suites, developers get complete failure information from a single run and skip the repeated push-and-wait cycles otherwise needed to find each failure.
How do I require manual approval before a GitHub Actions production deployment?
Point the deploy job at a GitHub Environment, for example an environment named production with its url, and configure that environment in GitHub to require manual approval from team leads. The workflow then pauses at the deploy-production job until an authorized reviewer approves. Making deploy-production need deploy-staging keeps every release passing through staging first.
How much does dependency caching speed up GitHub Actions builds?
Aggressive caching cuts build times by 60 to 80 percent. Use actions/cache@v4 on ~/.npm, node_modules and .next/cache with a key built from the runner OS and hashFiles('package-lock.json'). A restore-keys entry of deps- plus the runner OS serves as the fallback key.
How can a GitHub Actions workflow roll back a failed deployment automatically?
Add a smoke test step with continue-on-error set to true that curls the /healthz endpoint five times, five seconds apart, and exits 1 on any status other than 200. A rollback step that runs only when steps.smoke.outcome equals failure then syncs s3://production-backup/ back into the production bucket, invalidates CloudFront and exits with an error so the run is marked as failed.
How do I run a PostgreSQL database for integration tests in GitHub Actions?
Declare a postgres:16 service container on the test job with POSTGRES_DB and POSTGRES_PASSWORD set and port 5432 mapped to 5432. Add health options that run pg_isready with a 10s interval, a 5s timeout and 5 retries. Tests then connect through a DATABASE_URL that points at localhost:5432.
How do I secure a GitHub Actions pipeline that deploys to AWS?
Use OIDC for AWS authentication in place of long-lived access keys, and pin action versions to specific SHA hashes instead of tags for supply chain security. A security job running the Trivy scanner in fs mode with severity CRITICAL,HIGH and exit-code 1 fails the pipeline on serious findings and belongs among the required checks. Recording the commit SHA, timestamp and actor of every deployment gives an audit trail.
Read how we handle it in DevOps as a Service.
Related Articles
From Code to Production: A Guide to Automating Laravel Deployments with GitHub Actions
Learn how to build a fully automated CI/CD pipeline for Laravel using GitHub Actions, covering SSH key setup, workflow configuration, testing, and deployment to production servers.
Server & DevOpsYour Images Are Probably Not What Makes LCP Slow
Cloudflare's BEACON data from 10,000 large sites shows where slow LCP comes from. On pages rated poor, downloading the LCP resource took 119 ms, while the first byte, late discovery and blocked rendering each took one and a half to two seconds.
Server & DevOpsHow to Run Cron in Kubernetes So Jobs Never Overlap or Vanish
Scheduled work in Kubernetes fails in two quiet ways, a slow job that starts a second copy of itself and a schedule that silently stops firing. Both are configuration, not luck. This covers the CronJob fields that control concurrency, missed schedules, history retention and failure handling, with the actual defaults, so the nightly billing run is still there in the morning and there is a log to read when it is not.