agentleFS
louisbrulenaudetlouisbrulenaudet/monorepo-template/.cursor/rules/ops/cd.mdc

monorepo-template / ops

Cursor rule18 starsChanged 59 days agoApache-2.0View on GitHub

GitHub Actions CD - called by release.yml after the tag, no push-tags trigger, cf workers versions create then a 100% deployment in deployOrder, scoped API token on the production environment, GitHub-owned deployment record

  • Reads credentials
  • Commits and pushes

The file

We split this cd.mdc into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Never deploy from pull_request or pull_request_target (the latter's base context plus an untrusted checkout is a pwn pattern).

    line 19
  2. Very specific:

    Deploying every green main: only a newly created release tag deploys, gated on create-release-tag.sh's created output.

    line 55

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
cd.mdc
---
description: "GitHub Actions CD - called by release.yml after the tag, no push-tags trigger, cf workers versions create then a 100% deployment in deployOrder, scoped API token on the production environment, GitHub-owned deployment record"
alwaysApply: false
globs: .github/workflows/cd.yml,.github/actions/cd/**,.github/actions/lib/**
---

# Continuous Deployment

The pipeline is [`.github/workflows/cd.yml`](../../../.github/workflows/cd.yml); read it for the step list. How a release reaches it: [release.mdc](release.mdc). Hardening, script conventions, and expression contexts: [ci.mdc](ci.mdc), which loads with every workflow - never diverge "just for CD". Human-facing secret/variable tables: the README deploy section. Worker secrets vs vars: [workers-config.mdc](../backend/workers-config.mdc).

## Status

**CD is paused** until the `production` GitHub Environment secrets are configured. The pause is the repository variable `CD_ENABLED`, checked by `release.yml`'s `deploy` job; set it to `true` in the same act as adding the secrets. It deliberately lives at the caller, not in this workflow: a job skipped inside a `workflow_call` target reports success to the caller, so a paused CD used to leave a green Release run with the tag cut and nothing shipped. Gating at the caller keeps the skip visible and `workflow_dispatch` usable for a manual redeploy.

## Trigger

- **`workflow_call` and `workflow_dispatch` only.** `release.yml` calls this workflow after cutting a tag it confirmed did not exist; `workflow_dispatch` takes a `tag` input and is the redeploy / rollforward path.
- **No `push: tags:` trigger; adding one is a regression.** The release tag is created with `GITHUB_TOKEN`, whose events start no workflow runs (GitHub `GITHUB_TOKEN` docs; changesets/action#669, changesets/changesets#1545), so that trigger never fires.
- Never deploy from `pull_request` or `pull_request_target` (the latter's base context plus an untrusted checkout is a pwn pattern).
- **Concurrency `cd-production`, `cancel-in-progress: false`, `queue: max`**: canceling would drop an in-flight ship, and with the default `queue: single` a third trigger replaces the pending one, silently losing that release.
- **Checkout `ref: refs/tags/${{ inputs.tag }}`**: the ship matches the tagged commit, not the branch tip at job start, and the qualified ref stops a same-named branch shadowing the tag. `Resolve release version` fails unless that commit is on `main` (compare API), so neither a dispatch nor a moved tag ships an ungated commit. The tag must be exactly `vX.Y.Z`: a changesets pre-mode tag (`v1.0.0-next.0`) fails closed there instead of shipping at 100%, by design. `RELEASE_SHA` (the tagged commit) is what upload messages and Release notes record; `GITHUB_SHA` points at the dispatching ref on redeploys.
- **The build step is also the type gate**: `pnpm turbo run check-types build --filter='./apps/*...'`. `build` does not depend on `check-types` and this job has no remote cache, so dropping `check-types` would upload bundles no step type-checked. Uploads are `--prebuilt`: they ship exactly the Build Output (`apps/<app>/.cloudflare/output/`) this step wrote, in the mode it recorded. The trailing `...` pulls in the internal packages the apps ship, and expands that way only while `futureFlags.filterUsingTasks` stays off ([turborepo.mdc](../core/turborepo.mdc)).

## Auth and environment

- **The job declares `environment: {name: production, url: …}`**: protection rules, required reviewers, and environment secrets belong there; never move Cloudflare credentials into unprotected repository secrets "for convenience". Job grants beyond ci.md's baseline: `contents: write` (the Release) and `deployments: write` (the environment record).
- **Cloudflare credentials are never passed in by a caller.** `on.workflow_call` declares no `secrets:` at all - not even Turbo's, so the production build never restores remote-cache output a PR run could have planted. `CLOUDFLARE_API_TOKEN` / `CLOUDFLARE_ACCOUNT_ID` resolve from the `production` environment in step-level `env:` on the individual `cf` steps; install, build, and smoke never see them. Never switch the caller to `secrets: inherit`, never lift the credentials to job-level `env:`, and give a new `cf` step its own copy of the two-line credentials `env:` block - the `Require deploy inputs` gate vouches only for its own copy.
- **Both Cloudflare values are required**; the workflow fails closed if either is empty. Use a scoped Workers API token (Edit Cloudflare Workers, account-scoped), never a Global API Key or a committed value; add Secrets Store Edit only if the Worker binds Secrets Store.
- **`VITE_API_BASE_URL` is a repository/environment variable, not a secret**: public build-time config for `front-app` and the `environment.url`. Still required; empty fails.
- cf reads `CLOUDFLARE_API_TOKEN` / `CLOUDFLARE_ACCOUNT_ID` non-interactively, exactly as Wrangler did. `CF_ACCOUNT_ID` is a deprecated alias - never set it - and CD never runs `cf auth login`.
- **No OIDC, correctly**: Cloudflare has no OIDC path here, and npm trusted publishing is irrelevant (nothing publishes to npm), so `id-token: write` would be privilege with no consumer.

## Upload versions, then promote

- **CD never runs `cf deploy` or a deploy action.** Each app's `upload` script runs `cf workers versions create --prebuilt --mode production` with the workspace-pinned cf, then `promote-versions.sh` runs `pnpm --filter=<app> exec cf workers deployments create --worker <worker_name> --strategy percentage --versions '[{"version_id":"<id>","percentage":100}]'` - the same CLI as local, no floating action pin. cf has no versions-deploy or rollback command, and there is no `promote` script.
- **Uploads are read machine-readably**: with `WRANGLER_OUTPUT_FILE_PATH` set, cf appends an ND-JSON record (`"type": "version-upload"` with `worker_name`, `version_id`, `preview_url`, …) to that file, and CD reads `version_id` and `worker_name` from it. cf prints no JSON for `versions create` on stdout (upstream bug `versions-upload-no-json-output`), so never scrape stdout; a per-app path also lets the uploads run concurrently without clobbering each other.
- **Every upload is tagged and messaged with the release version + `RELEASE_SHA`** (`--tag` / `--message`). cf always uploads strict (no `--strict` flag): in CI a conflict (dashboard edits, remote-secret overrides) aborts with exit 1 and `version_id: null`, and a missing `version_id` or `worker_name` fails the step. `upload-versions.sh` passes the flags straight after the script name, never after `--`: pnpm 12 forwards a literal `--` to the script and cf then ignores every following flag, so the version would upload without its tag and message. `turbo run … -- <flags>` is unaffected, since turbo strips the separator.
- **The root `deploy` / `upload` scripts pass `--force`** (skip cache reads), so a local `pnpm run deploy` / `upload` never ships a Build Output restored from the remote cache, which same-repo PR runs can write.
- **Traffic policy is 100%.** Gradual percentages exist; do not add them without observability and a rollback runbook.
- **A version upload applies no triggers**: `previewUrls`, `workersDev`, routes, custom domains, and crons stay as they were ([deployment management](https://developers.cloudflare.com/workers/versions-and-deployments/deployment-management/)). After the first production deploy, run `pnpm --filter=<app> exec cf workers triggers deploy --mode production` once per app, and again after changing any of them in `cloudflare.config.ts`.
- **The app set is discovered, never listed**: `.github/actions/lib/apps.mjs` reads every `apps/*/package.json`, so a new Worker ships by existing, and `upload-versions.sh` / `promote-versions.sh` loop over it. It **fails closed** on an app missing an integer `monorepo.deployOrder` and on two apps sharing one, instead of skipping or tiebreaking - a tag cut for a release that never shipped one of its Workers, or shipped them in an undeclared order, is the failure this prevents.
- **Every app declares `monorepo.healthPath`**: the public probe path, or `null` when it has no public HTTP surface (RPC-only `worker-*`, `queue-*`). `apps.mjs` fails closed on an app declaring neither, since an app nobody declared a probe for would ship unverified. The smoke is `.github/actions/lib/smoke.mjs`, the same script as skill `run-app` and the Previews. `--url http-gateway=$VITE_API_BASE_URL` addresses the gateway by role, so `cd.yml` names no app. `--expect-version "$VERSION"` keeps probing, up to ~25 s like any `--url` target, until the health body reports the release just promoted, so a smoke answered by the previous version during propagation fails instead of passing. It also checks `X-Request-Id`, the JSON 404 envelope, and that CORS refuses a foreign origin, with GET and OPTIONS only. CD knows no other origin, so every other app with a non-null `healthPath` is named in a `::notice::` as promoted-but-unsmoked rather than passed over silently.
- **Promotes are sequential, ordered by `monorepo.deployOrder`**: lower first, gateways before the SPAs that call them (`worker-api` 1, `front-app` 2). If a later promote fails while an earlier one is live at 100%, production is split across two releases: `promote-versions.sh` names every already-promoted app, the `cf workers deployments list --worker <worker_name>` command showing its previous version id, and the `cf workers deployments create` command restoring it (a rollback is a deployment of the previous version at 100%). Never parallelize the promotes without handling that partial state; declare a new app's position in its own `package.json`, never by reordering the loop.
- **Uploads run concurrently; only promotes are ordered.** An upload changes no traffic, so its order does not matter and one failing cannot split production. `upload-versions.sh` backgrounds one `pnpm --filter` per app, each with its own `WRANGLER_OUTPUT_FILE_PATH` temp file, then `wait`s on every pid: a failed upload does **not** abort its siblings, so the step reports every failing app in one pass. The version-id table (`version-ids.tsv`: app, dir, `version_id`, `worker_name`) is written afterwards in discovery order, never completion order, because `promote-versions.sh` reads it as the `deployOrder` contract; `create-github-release.sh` reads the same table. `worker_name` comes from the upload record because `cf workers deployments create --worker` takes the deployed Worker name (`<app>-production`), not the package name. Each app's output is captured and replayed inside a `::group::` so concurrent streams do not interleave. Concurrency is unbounded; bound it here if the app count ever makes N simultaneous cf processes the constraint.

## Release and deployment records

- **GitHub creates the deployment record** because the job references an environment. Never re-create deployments through the REST API: an earlier pipeline did, producing a duplicate record and marking GitHub's own inactive.
- **Only the GitHub Release is ours to write**: created or updated in-workflow with `gh release create --verify-tag`, listing each deployed app's version id with its `apps/<app>/CHANGELOG.md` section when present, except a section that only says "No changes in this release." (the `fixed` group writes that for an app no changeset named).

## Deliberately not used

- **Workers Builds**: external GitHub Actions is the chosen path so CI and CD share one pipeline; never dual-enable Cloudflare Builds on the same production Workers. Branch Previews follow the same choice ([previews.mdc](previews.mdc)).
- **Secrets baked into `bindings.text()` values or workflow YAML**: runtime secrets stay in Cloudflare (`bindings.secret()`), build secrets in GitHub Environments.
- **Deploying every green `main`**: only a newly created release tag deploys, gated on `create-release-tag.sh`'s `created` output.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.