Back to the academy

How to build a professional GitHub Action and a CI/CD pipeline

Workflow anatomy, caching and parallel jobs per domain, secrets and quality gates, and automated npm publishing with a deployment verified by a healthcheck.

Intermediate · GitHub Actions · YAML · npm · pip

Anatomy of a workflow in .github/workflows/

A workflow is a YAML file with three layers: the events that trigger it (`push`, `pull_request`), the `jobs` that run in parallel on isolated runners and the `steps` each job runs in order. Every job starts on a clean machine: it checks out the repository, installs the runtime and its dependencies, and runs commands. If a step exits with a non-zero code, the job fails and the PR is flagged red.

❌ Antipattern

on: push
jobs:
  everything:
    runs-on: ubuntu-latest
    steps:
      - run: npm install && npm test && npm run build && ./deploy.sh

✅ Stoic pattern

name: ci
on:
  pull_request:
  push:
    branches: [master]
jobs:
  frontend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm run type-check
      - run: npm test

Key takeaways

  • One step per responsibility: when something fails, the log points at exactly what broke.
  • In CI, `npm ci` and never `npm install`: it installs exactly what the lockfile pins.
  • Pin every action to a version (`@v4`) so the pipeline never changes on its own.

Speed: caching, matrices and parallel tests per domain

A slow pipeline ends up being skipped. Dependency caching (`cache: npm` or `cache: pip`) avoids downloading hundreds of packages on every run. A matrix launches the same job over several domains or versions at once, and splitting frontend, backend and engine into independent jobs means each one only waits for itself: a CSS change should not wait for the Python tests.

❌ Antipattern

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: cd frontend && npm install && npm test
      - run: cd backend && npm install && npm test
      - run: cd engine && pip install -r requirements.txt && pytest

✅ Stoic pattern

jobs:
  node:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        suite: [frontend, backend]
    defaults:
      run:
        working-directory: ${{ matrix.suite }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: ${{ matrix.suite }}/package-lock.json
      - run: npm ci && npm test

Key takeaways

  • Cache dependencies keyed on the lockfile: the cache is only invalidated when they change.
  • Jobs per domain in parallel: total time is the slowest job, not the sum of all of them.
  • A matrix avoids copy-pasting the same job for every package or version.

Secrets, environment variables and quality gates

A token is never written in the YAML or in the repository: it is stored in GitHub Secrets and injected as an environment variable only into the step that needs it. Linters and the type-check are quality gates: if they fail, the pipeline stops before spending minutes on tests or builds. And the main branch is protected by requiring those checks to be green before merging.

❌ Antipattern

env:
  NPM_TOKEN: npm_8f2a1c...   # pasted in plain text
steps:
  - run: npm test || true    # can never fail

✅ Stoic pattern

steps:
  - run: npm run lint
  - run: npm run type-check
  - run: npm test
  - run: npm publish
    env:
      NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

Key takeaways

  • Secrets live in GitHub Secrets and are exposed only to the step that uses them.
  • Lint and type-check first: failing fast is cheaper than failing late.
  • Never silence a failure with `|| true`: a gate that never closes is not a gate.

Automated npm publishing and deployment with a healthcheck

In co-gherkin, releasing a version means creating a tag: the workflow runs on the `push` of `v*` tags, executes the whole suite and only then publishes to npm with the token stored as a secret. Continuous deployment follows the same idea: after deploying, a healthcheck calls the health endpoint and, if it does not answer in time, the job fails and alerts instead of leaving a broken version in production.

❌ Antipattern

# from a laptop, after "it works on my machine"
npm version patch
npm publish
scp -r dist/ server:/var/www/

✅ Stoic pattern

on:
  push:
    tags: ['v*']
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org
      - run: npm ci && npm test
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  deploy:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - run: ./scripts/deploy.sh
      - run: curl --fail --retry 5 --retry-delay 10 https://example.com/health

Key takeaways

  • A release is a tag: the pipeline reproduces it identically every time, with no manual steps.
  • Publish only after the whole suite has passed in the same workflow.
  • A deployment does not end when files are copied, but when the healthcheck answers.