Building a Secure CI/CD Pipeline with GitHub Actions
Design a GitHub Actions pipeline that assumes the build environment is hostile: pinned actions, least-privilege permissions, OIDC federation, and security gates before deploy.
- Difficulty
- intermediate
- Prerequisites
- A GitHub repository, AWS or GCP with OIDC support
- Tools
- GitHub Actions, AWS, GCP
On this page
A CI/CD pipeline that deploys to production should not trust its inputs. The workflow file itself, the action tags it calls, the pull requests it checks out, and the artifacts it ships all become code execution inside your pipeline. This article walks through a GitHub Actions setup that treats the pipeline as hostile by default: least-privilege permissions, commit-SHA pinning, OpenID Connect federation, and security gates that run before the pipeline ever receives deploy credentials.
Threat model: assume the pipeline is the weakest link
Two failure modes cover most incidents. First, a compromised or malicious third-party action executes inside your repository and can read or exfiltrate secrets. Second, a pull request from a fork inherits your workflows but not your secrets, so reviewers must reason carefully about what a fork build can and cannot do. The design below answers both with boring, auditable defaults.
Give the pipeline least privilege
name: ci
on:
push:
branches: ["main"]
pull_request:
permissions: {}
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Check out repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
- name: Set up Node
uses: actions/setup-node@<sha> # v4 — pin to a full commit SHA
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm test
- run: npm run build
Pin actions to full commit SHAs
Mutable tags such as @v4 move silently when a new release ships. A pinned action references a full commit SHA, which cannot move without a deliberate commit. Keep the tag as a comment so humans can still read the intended version.
- name: Check out repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
- name: Upload coverage artifact
uses: actions/upload-artifact@<sha> # v4 — pin to a full commit SHA
with:
name: coverage
path: coverage
Federate short-lived credentials with OpenID Connect
Long-lived cloud access keys stored as repository secrets are a standing invitation to any pipeline compromise. OpenID Connect lets the runner mint short-lived tokens for your cloud provider, scoped to a single role, through the identity provider configured for the repository.
deploy:
if: github.ref == "refs/heads/main" && github.event_name == "push"
runs-on: ubuntu-latest
needs: [test]
permissions:
contents: read
id-token: write
environment: production
steps:
- name: Check out repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Assume deploy role
uses: aws-actions/configure-aws-credentials@<sha> # v4 — pin to a full commit SHA
with:
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }}
aws-region: us-east-1
Add security gates before deploy credentials exist
Keep the security job fully read-only and independent from the deploy job. Secrets scanning, dependency review, and filesystem/container scans should be able to run without any cloud credentials at all.
security:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
steps:
- name: Check out repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Detect leaked secrets
uses: gitleaks/gitleaks-action@<sha> # v2 — pin to a full commit SHA
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Review dependency changes
uses: actions/dependency-review-action@<sha> # v4 — pin to a full commit SHA
- name: Scan configuration files
uses: aquasecurity/trivy-action@<sha> # v0 — pin to a full commit SHA
with:
scan-type: fs
scan-ref: .
format: sarif
exit-code: "1"