Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions .github/workflows/ci-mock-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
name: "CI - Mock API: Run Checks and Deploy Worker"

# Deliberately minimal, and deliberately NOT modelled on ci-website-preview.yml.
# That workflow uploads *versions* of the shared `common-grants` Worker and
# aliases them per PR; this is a separate service with its own name
# (`cg-mock-api` in mock-api/wrangler.jsonc), so a plain `wrangler deploy` is the
# right primitive. Do not merge the two flows.
#
# Deploys target a PERSONAL Cloudflare account and a `*.workers.dev` URL — a
# known interim state for the #1077 experiment, not a productionized setup. Moving
# to the org account is tracked separately; see mock-api/FINDINGS.md (#1077-T7).

on:
workflow_call:
pull_request:
paths:
- mock-api/**
- .github/workflows/ci-mock-api.yml
# Deploying is manual on purpose: the experiment is timeboxed and a single
# fixed Worker name means every deploy claims the same slot, so an automatic
# deploy-per-push would just fight itself.
workflow_dispatch:

# One deploy at a time — `cg-mock-api` is a single deployment slot. Deliberately NOT
# keyed on `github.ref`: every ref deploys to the same Worker, so a ref-scoped group
# would put two dispatches from different branches in different groups and let them
# race the one slot — exactly what this is meant to prevent.
concurrency:
group: mock-api-deploy
cancel-in-progress: false

jobs:
validate:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v6

- name: Install Node.js
uses: actions/setup-node@v6
with:
node-version-file: .nvmrc

- name: Install pnpm
uses: pnpm/action-setup@v6

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

- name: Build Worker bundle
run: pnpm --filter @common-grants/mock-api run build

- name: Run checks
run: pnpm --filter @common-grants/mock-api run checks

# The conformance suite (#1077-T4) validates the fixture against the docs
# pipeline's generated per-version schemas, which are gitignored build
# output. This step is that pipeline — it compiles the website's TypeSpec
# and emits `website/public/schemas/yaml/versions/`. Kept as its own step
# rather than folded into `run ci` so the cross-package dependency is
# visible in the CI log; the suite fails loudly if it is ever skipped.
- name: Generate versioned schemas (for conformance tests)
run: pnpm --filter @common-grants/mock-api run schemas

- name: Run tests
run: pnpm --filter @common-grants/mock-api run test

- name: Audit dependencies
# Gated to PRs into `main` (and main-context workflow_call invocations) so
# advisories that live on `main` and can only be fixed there don't block
# PRs into HOLD-* batching branches or other non-main bases. The HOLD → main
# checkpoint PR re-runs this audit, so anything live at merge time is gated
# at the actionable boundary.
#
# `workflow_dispatch` is added to the sibling workflows' condition because
# here `validate` gates a real `wrangler deploy`. `base_ref` is empty for
# dispatch, so without this the condition collapses to `ref == main` and a
# dispatch from any other branch would skip the audit — silently publishing
# a public Worker whose lockfile was never audited.
if: |
github.event_name == 'workflow_dispatch'
|| github.base_ref == 'main'
|| github.ref == 'refs/heads/main'
run: pnpm --filter @common-grants/mock-api run audit

deploy:
runs-on: ubuntu-latest
needs: validate
# Manual dispatch only, and only after validate passes.
if: github.event_name == 'workflow_dispatch'

steps:
- name: Checkout code
uses: actions/checkout@v6

- name: Install Node.js
uses: actions/setup-node@v6
with:
node-version-file: .nvmrc

- name: Install pnpm
uses: pnpm/action-setup@v6

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

- name: Deploy Worker
id: deploy
working-directory: ./mock-api
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
# pipefail matters: without it the pipeline reports `tee`'s exit code and a
# failed deploy would pass. Steps with no `shell:` key run under GitHub's
# default `bash -e {0}`; only an explicit `shell: bash` gets
# `--noprofile --norc -eo pipefail {0}`. So this is load-bearing, not decoration.
run: |
set -o pipefail
pnpm exec wrangler deploy 2>&1 | tee deploy.log

# The deployed URL is the ticket's verification artifact, so pull it out of
# the deploy log rather than leaving it buried there.
- name: Resolve deployed URL
id: url
working-directory: ./mock-api
run: |
url="$(grep -Eo 'https://[a-z0-9.-]+\.workers\.dev' deploy.log | head -n 1)"
if [ -z "$url" ]; then
echo "No workers.dev URL found in wrangler output" >&2
exit 1
fi
echo "url=$url" >> "$GITHUB_OUTPUT"

# A deploy that succeeds while serving a broken Worker should fail the run,
# so assert the two routes #1077-T5 names as its verification: the health
# route, and a versioned opportunity list that actually returns records.
- name: Smoke-test the deployed Worker
env:
URL: ${{ steps.url.outputs.url }}
run: |
set -euo pipefail

echo "Health route:"
curl -fsS --retry 5 --retry-connrefused --retry-delay 2 "$URL/" | tee health.json
jq -e '.name == "@common-grants/mock-api" and (.supportedVersions | index("0.3.0"))' health.json > /dev/null

echo "Opportunity list (v0.3.0):"
curl -fsS --retry 3 --retry-connrefused --retry-delay 2 "$URL/v0.3.0/common-grants/opportunities" | tee list.json > /dev/null
count="$(jq '.items | length' list.json)"
echo " returned $count records"
test "$count" -gt 0

- name: Summarize deployment
env:
URL: ${{ steps.url.outputs.url }}
run: |
{
echo "### Mock API deployed"
echo
echo "- URL: \`$URL\`"
echo "- Health: \`$URL/\`"
echo "- Opportunities (v0.3.0): \`$URL/v0.3.0/common-grants/opportunities\`"
echo "- Smoke test: passed"
} >> "$GITHUB_STEP_SUMMARY"
20 changes: 20 additions & 0 deletions mock-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Dependencies
node_modules/

# Build output
dist/

# Wrangler local state
.wrangler/

# Coverage reports
coverage/

# Environment variables
.dev.vars
.env
.env.local
.env.*.local

# TypeScript cache
*.tsbuildinfo
10 changes: 10 additions & 0 deletions mock-api/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"semi": true,
"trailingComma": "es5",
"singleQuote": false,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}
144 changes: 144 additions & 0 deletions mock-api/DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Deployment overhead (#1077-T5)

The "deployment overhead" rubric input for the #1077 experiment: what it actually
takes to get this Worker onto a real URL, and what is still missing before that
setup would be acceptable outside a spike.

Fold this into `FINDINGS.md` at #1077-T7.

## What exists

`.github/workflows/ci-mock-api.yml`, path-filtered to `mock-api/**` plus the
workflow file itself. Two jobs:

| Job | Trigger | Does |
| ---------- | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `validate` | `pull_request`, `workflow_dispatch`, `workflow_call` | build → checks → generate versioned schemas → test → audit |
| `deploy` | `workflow_dispatch` only, `needs: validate` | `wrangler deploy` → resolve the `*.workers.dev` URL → smoke-test health + v0.3.0 list → write both to the run summary |

Deliberately a plain `wrangler deploy`, not the `wrangler versions upload
--preview-alias` flow that `ci-website-preview.yml` uses via
`.github/scripts/upload-worker-preview.sh`. That flow versions the _shared_
`common-grants` Worker; this is a separate service with its own name, so the
simpler primitive is the correct one.

## Secrets

No new secrets. Reuses the two the website preview flow already relies on, both
confirmed present on the repo:

| Secret | Added |
| ----------------------- | ---------- |
| `CLOUDFLARE_API_TOKEN` | 2026-08-06 |
| `CLOUDFLARE_ACCOUNT_ID` | 2026-08-06 |

They point at a **personal** Cloudflare account, so the deployed URL is a personal
`*.workers.dev` subdomain. Sufficient per the ticket, but it means the experiment's
one public artifact lives outside org control — the single biggest gap between this
and a productionized deploy. Moving to the org account is tracked separately.

## Cost / duration

Measured locally (Apple silicon, warm `node_modules`); CI adds checkout, Node and
pnpm setup, and a cold `pnpm install --frozen-lockfile`, which dominates:

| Step | Local |
| ------------------------------------------------------------ | ------- |
| `build` (`wrangler deploy --dry-run`) | ~1s |
| `checks` (eslint + prettier + tsc ×2) | ~3s |
| `schemas` (website TypeSpec compile + versioned-schema emit) | ~3s |
| `test` (232 tests) | ~1s |
| **validate total, excluding install** | **~8s** |

Worker bundle: 29.80 KiB (7.48 KiB gzipped).

The `schemas` step being only ~3s is the useful surprise here — the conformance
test's dependency on the website pipeline is cheap in wall-clock terms. The cost of
that coupling is structural, not temporal (see below).

> **Pending a real run.** The CI-measured duration of `validate` and `deploy`, and
> the deployed URL, are not filled in yet — see "Not yet verified".

## Not yet verified

**No deploy has been run.** `wrangler deploy` publishes a public service, so it
needs an explicit human decision, and two things block CI from doing it today:

1. This branch is not pushed to GitHub.
2. `workflow_dispatch` only exposes a "Run workflow" control once the workflow file
exists on the **default branch**. This is a one-time bootstrap gate, not a
standing requirement: once `ci-mock-api.yml` has landed on `main` once, the ref
picker can target any branch carrying the same file, so later feature branches
won't each need their own merge-to-`main` cycle. But today it means the deploy
job cannot be dispatched from this branch at all — a consequence of choosing
manual dispatch to keep the experiment cheap, not anticipated in the plan.

## Runbook: first deploy (manual, one time)

Run from the repo root. Step 1 opens a browser for Cloudflare OAuth, so it can't be
automated in a headless session.

1. Authenticate wrangler against the Cloudflare account that should own the Worker:

```bash
pnpm --filter @common-grants/mock-api exec wrangler login
```

2. Confirm the account it picked, so the Worker doesn't land somewhere unexpected:

```bash
pnpm --filter @common-grants/mock-api exec wrangler whoami
```

3. Deploy. This publishes a **public** Worker at
`https://cg-mock-api.<your-subdomain>.workers.dev`:

```bash
pnpm --filter @common-grants/mock-api run deploy
```

4. Verify the two routes the ticket names, substituting the URL wrangler printed:

```bash
curl -s https://cg-mock-api.<your-subdomain>.workers.dev/ | jq
```

```bash
curl -s https://cg-mock-api.<your-subdomain>.workers.dev/v0.3.0/common-grants/opportunities | jq '.items | length'
```

Expected: the first prints `name` plus all four `supportedVersions`; the second
prints `11`.

If attaching this to a personal account is unwanted, `wrangler deploy --temporary`
uses a throwaway preview account — but the URL is ephemeral, and #1077-T6 and
#1077-T7 need a stable origin for the docs "Try it out" and the SDK continuity
check, so they would need a real deploy anyway.

### Alternatives to deploying by hand

- Merge `ci-mock-api.yml` to `main`, then use the Actions "Run workflow" button. The
workflow's own smoke test verifies both routes for you.
- Add a `push` trigger scoped to the experiment branch — auto-deploys, and gives up
the "manual, therefore cheap" property the ticket asked for.

### Deployed URL

Not yet deployed.

## Gaps to flag for #1077-T7

- **Personal account.** The public URL is not org-controlled.
- **Single deployment slot.** One fixed Worker name (`cg-mock-api`) means no
staging/per-env split; a `concurrency` group keeps deploys from racing, but that
is a guard, not a solution.
- **Path filter misses its real dependency.** The trigger watches `mock-api/**`,
per the ticket. But the conformance suite validates against schemas generated
from `website/`, so a protocol or pipeline change under `lib/core/**` or
`website/**` can break `mock-api` tests _without_ running this workflow. The
reverse coupling is unguarded too. Widening the filter would catch it at the cost
of running mock-api CI on most PRs.
- **No rollback path** beyond redeploying an older commit.
- **Smoke test covers two routes only** — the health route and the v0.3.0 list. The
detail and search endpoints, the other three versions, and CORS/preflight are
exercised by unit tests but never against the deployed Worker.
Loading
Loading