Skip to Content
Mengty Learns
CI/CD

GitHub Actions Workflow

How to write a minimal, supply-chain-safe GitHub Actions CI workflow with SHA-pinned actions and deterministic installs.

how-tointermediateTested with node 22.22.3Updated Nov 26, 2026

TL;DR

A GitHub Actions workflow lives in .github/workflows/*.yml, runs jobs on GitHub-hosted runners in response to events like push or pull_request, and should pin third-party actions to full 40-character commit SHAs, declare minimal permissions, and use npm ci for deterministic installs.

Why it exists

Manual "works on my machine" checks break as soon as multiple commits or contributors land on a repo. CI automates lint, tests, and builds on every change in a clean environment—provided the workflow itself doesn't introduce mutable dependencies or overly broad token permissions.

Mental model

Think of a workflow YAML file as four nested layers:

  1. Trigger (on:) — which repository events start a run (push, pull_request, workflow_dispatch).
  2. Permissions (permissions:) — what the GITHUB_TOKEN is allowed to touch (default to contents: read).
  3. Jobs (jobs.<id>) — isolated virtual machines (runs-on: ubuntu-latest) that run in parallel by default.
  4. Steps (steps:) — sequential shell commands (run:) or reusable actions (uses:) inside a job.
GitHub Actions workflow diagram
A simple CI workflow from push to test.

Do it

Create .github/workflows/ci.yml with SHA-pinned actions, permissions: contents: read, and npm ci:

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

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: "22"
          cache: npm
      - run: npm ci
      - run: npm test

You can look up the real commit SHA for any action release tag before pinning it:

git ls-remote https://github.com/actions/checkout.git refs/tags/v4.2.2

Gotchas

Gotcha:

Pin every third-party action to a full 40-character commit SHA with a version comment (# v4.2.2), never a mutable major tag like @v4. A tag can be force-pushed to point at malicious code; a commit SHA is immutable.

  • npm install in CI: npm install can silently update package-lock.json or install newer semver matches. Always use npm ci (or pnpm install --frozen-lockfile) so CI fails loudly when the lockfile is out of sync.
  • Default GITHUB_TOKEN scope: Always set explicit permissions: at the workflow or job level so a compromised step cannot write to the repository.

Production checklist

  • Pin all uses: references to 40-char commit SHAs with # vX.Y.Z comments.
  • Declare explicit least-privilege permissions: (usually contents: read).
  • Use npm ci / pnpm install --frozen-lockfile and enable dependency caching.
  • Validate workflow syntax with actionlint before merging.

Check yourself

Check Yourself

1. Why should you pin GitHub Actions to a 40-character commit SHA instead of @v4?

2. What is the main advantage of npm ci over npm install in a CI job?

Sources