name: CI

# A release tag needs this workflow's CI check to have passed on the tagged
# commit, so it runs on every branch a release can come from.
on:
  push:
    branches: [main, "release/**"]
  pull_request:

# Cancel superseded runs on the same ref (a force-push mid-run).
concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      # corepack pins pnpm from `packageManager` in package.json; in CI it must
      # fetch that version without an interactive prompt.
      COREPACK_ENABLE_DOWNLOAD_PROMPT: 0
    steps:
      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0

      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: 24

      # pnpm via corepack, pinned by `packageManager` — the same version you
      # develop with, so `--frozen-lockfile` below always matches the committed
      # pnpm-lock.yaml. (Commit that lockfile; this step relies on it.)
      - name: Enable pnpm (corepack)
        run: corepack enable

      - name: Install
        run: pnpm install --frozen-lockfile

      # Validate the config, bindings, and that the generated
      # src/schema.ts · src/worker.ts · src/middleware.ts trio is still in sync
      # with astroid.config.ts. A fast fail before the heavier steps.
      # `run` is load-bearing: `doctor` is a pnpm built-in, so a bare
      # `pnpm doctor` runs pnpm's own self-check and never reaches Astroid's.
      - name: Doctor
        run: pnpm run doctor

      # No-ops on a fresh scaffold; runs automatically once you add a `lint`
      # script (oxlint/biome) — same for `test` (vitest) further down.
      - name: Lint
        run: pnpm run --if-present lint

      - name: Type-check
        run: pnpm check

      - name: Test
        run: pnpm run --if-present test

      - name: Build
        run: pnpm build

  # One required status to protect main with, and the check a release reads, so
  # adding a job above doesn't mean editing branch protection or the release.
  # List every job in `needs`.
  ci:
    name: CI
    runs-on: ubuntu-latest
    needs: [build]
    if: always()
    steps:
      - name: Fail if any job did not succeed
        run: |
          echo "build=${{ needs.build.result }}"
          [ "${{ needs.build.result }}" = "success" ]
