GitHub Actions CI/CD Best Practices: Slashing Build Times from 25 Mins to 3 Mins
Slow CI/CD pipelines are silent productivity killers. When every pull request takes 20 to 30 minutes to run linting, unit tests, and Docker builds, developer momentum evaporates, context switching skyrockets, and your GitHub Actions bill quickly inflates.
At Saksham Cloud Infrastructures, we routinely audit and refactor automated delivery pipelines for fast-growing startups and agencies. In almost every engagement, we find pipelines rebuilding unchanged npm dependencies, fetching un-cached Docker layers over the internet, and exposing long-lived AWS secret keys in plain environment variables.
In this guide, we break down actionable, production-tested techniques to cut your GitHub Actions pipeline duration by up to 80% while hardening security with OpenID Connect (OIDC).
1. Eliminate Long-Lived AWS Secret Keys with OIDC
Storing static AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in GitHub Repository Secrets is an outdated and high-risk security anti-pattern. If a developer accidentally logs secrets or a malicious pull request exfiltrates environment variables, your entire AWS account is compromised.
Instead, configure AWS OpenID Connect (OIDC) with GitHub Actions. OIDC uses short-lived, cryptographically signed JWT tokens that expire automatically after the workflow run finishes.
Production Workflow Example:
name: Deploy to Production EKS
on:
push:
branches: [main]
permissions:
id-token: write # Mandatory for requesting the OIDC JWT token
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Configure AWS Credentials via OIDC
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole
aws-region: us-east-1
audience: sts.amazonaws.com
Build secure IAM policies fast: You can visually generate granular IAM least-privilege trust policies for your AWS roles using our free AWS IAM Policy Generator.
2. Docker Layer Caching with GitHub Actions Cache (GHA)
The slowest part of a deployment pipeline is almost always running docker build. If you don't configure remote layer caching, Docker re-downloads base OS images and re-installs heavy npm/python packages on every single git commit.
By pairing Docker Buildx with the type=gha cache backend, unchanged Docker image layers are cached directly inside GitHub's distributed storage cache.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and Push Docker Image with Cache
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/app:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
Pro Tip: Need a clean multi-stage Dockerfile that separates build tools from lean production runtimes? Use our free Dockerfile Generator to generate optimized, lightweight container manifests.
3. Cache Package Managers (npm, pnpm, yarn) Correctly
Never run npm install without leveraging lockfile-based caching. Using @actions/setup-node with the built-in cache: 'npm' (or pnpm) feature eliminates 3 to 6 minutes of package download overhead on every run.
- name: Setup Node.js Environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm' # Automatically caches ~/.npm keyed against package-lock.json
- name: Install Clean Production Dependencies
run: npm ci --prefer-offline
4. Parallelize Independent Jobs with Dependency Graphs
A common pipeline anti-pattern is executing tasks sequentially in a single job:
Checkout -> Lint -> Test -> Security Scan -> Build -> Deploy
If lint, test, and security-scan are independent, run them as parallel jobs. GitHub Actions executes parallel jobs across separate runner VMs simultaneously, collapsing your feedback loop from 20 minutes to the duration of the single longest test.
jobs:
lint:
runs-on: ubuntu-latest
steps: [...]
test:
runs-on: ubuntu-latest
steps: [...]
security-scan:
runs-on: ubuntu-latest
steps: [...]
deploy:
needs: [lint, test, security-scan] # Runs only after all checks pass
runs-on: ubuntu-latest
steps: [...]
Scaffold your CI/CD pipelines visually: Don't write workflow YAML from scratch. Scaffold complete test, build, and cloud deploy workflows with our GitHub Actions Generator.
5. Implement Concurrency Groups to Cancel Outdated Builds
When a developer pushes 3 rapid commits to a pull request, running 3 full build pipelines wastes minutes and money.
Add a concurrency block with cancel-in-progress: true at the root of your workflow file. Whenever a new commit is pushed to the same branch or PR, GitHub automatically cancels the obsolete running job.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Need Custom CI/CD Pipeline Automation?
Accelerating your engineering release velocity while keeping your production deployments bulletproof requires senior DevOps expertise.
At Saksham Cloud Infrastructures, our cloud engineers help technology companies across the US, UK, and India design automated, zero-downtime GitOps pipelines using GitHub Actions, ArgoCD, and Kubernetes:
- Sub-5-minute release cycles with intelligent caching.
- Automated vulnerability scanning and SOC2 compliance checks.
- Zero-downtime canary and blue-green rollouts.
👉 Schedule a Free 15-Minute Architecture Audit or reach out directly at contact@sakshamcloudinfrastructures.in.
Written by Saksham Jain
Founder & Principal Cloud Architect • Saksham Cloud Infrastructures
Specializing in high-availability AWS and Azure architectures, Kubernetes GitOps pipelines, zero-trust cloud security, and actionable FinOps cost reduction strategies for global startups and engineering teams.
Need production-grade cloud architecture & engineering insights architecture?
Our senior infrastructure engineers help US, UK, and global teams architect zero-downtime AWS/Azure environments, automate CI/CD pipelines, and slash monthly cloud spend by up to 40%.