cs.oldnet

All sheets / CI/CD & Release

GitHub Actions

Workflow syntax and patterns for CI/CD pipelines.

Workflow skeleton

name: CI
on:
  push: { branches: [main] }
  pull_request:
  workflow_dispatch:            # manual run button
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm }
      - run: npm ci
      - run: npm test

Triggers

on:
  push:
    branches: [main, 'release/**']
    paths: ['src/**', '!docs/**']
    tags: ['v*']
  pull_request: { types: [opened, synchronize] }
  schedule: [{ cron: '0 6 * * 1' }]   # UTC
  workflow_call:                      # reusable workflow
  release: { types: [published] }

Jobs: needs, if, matrix, outputs

jobs:
  build:
    strategy:
      fail-fast: false
      matrix: { node: [20, 22], os: [ubuntu-latest, macos-latest] }
    runs-on: ${{ matrix.os }}
    outputs: { version: ${{ steps.v.outputs.version }} }
    steps:
      - id: v
        run: echo "version=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
  deploy:
    needs: build
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    environment: production          # approvals + env secrets
    runs-on: ubuntu-latest
    steps:
      - run: echo ${{ needs.build.outputs.version }}

Secrets, vars, env

env: { NODE_ENV: production }
steps:
  - run: ./deploy.sh
    env:
      TOKEN: ${{ secrets.API_TOKEN }}   # masked in logs
      REGION: ${{ vars.AWS_REGION }}    # non-secret config

Contexts: github, env, vars, secrets, matrix, needs, steps, runner, inputs.

AWS via OIDC (no stored keys)

permissions: { id-token: write, contents: read }
steps:
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
      aws-region: us-east-1
  - run: aws s3 sync dist s3://my-bucket --delete

The IAM role trusts token.actions.githubusercontent.com and restricts sub to repo:org/repo:ref:refs/heads/main.

Caching & artifacts

- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
    restore-keys: npm-${{ runner.os }}-
- uses: actions/upload-artifact@v4
  with: { name: dist, path: dist/, retention-days: 7 }
- uses: actions/download-artifact@v4
  with: { name: dist }

Cache = reuse between runs (deps). Artifact = pass files between jobs / keep build outputs.

Reusable workflows & composite actions

# caller
jobs:
  deploy:
    uses: org/repo/.github/workflows/deploy.yml@main
    with: { env: prod }
    secrets: inherit
# callee
on:
  workflow_call:
    inputs: { env: { type: string, required: true } }

Composite action (action.yml, runs: using: composite) bundles steps; reusable workflow bundles whole jobs.

Deployment patterns

  • Build once, promote: same artifact/image digest through dev → staging → prod.
  • Environments with required reviewers and wait timers as release gates.
  • Tag images with commit SHA; never deploy latest.
  • Run smoke tests post-deploy; auto-rollback on failure.
  • concurrency per environment to avoid overlapping deploys.

Security hardening

  • Least privilege permissions: at the top; default to contents: read.
  • Pin third-party actions to a full commit SHA.
  • Beware pull_request_target + checkout of PR code (secret exfiltration).
  • Never echo untrusted input (github.event.issue.title) into run: directly; pass via env.
  • Prefer OIDC over long-lived cloud keys.

Debugging

  • Re-run with debug logging (ACTIONS_STEP_DEBUG=true secret).
  • act to run workflows locally.
  • gh run list, gh run view --log-failed, gh run rerun --failed.