diff --git a/.github/workflows/remote-cache-production.yml b/.github/workflows/remote-cache-production.yml index 49f7923..2ed382a 100644 --- a/.github/workflows/remote-cache-production.yml +++ b/.github/workflows/remote-cache-production.yml @@ -20,6 +20,21 @@ jobs: name: production url: ${{ steps.deploy.outputs.url }} steps: + # A re-run deploys its original commit, including that commit's repository list and pull + # request opt-ins. Deploy only the current main head; roll back by reverting on main. This + # step fails early; `ci:deploy` checks again right before it changes production. + - name: Check that main still points to this commit + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 + with: + script: | + const branch = context.payload.repository.default_branch; + const { data } = await github.rest.repos.getBranch({ ...context.repo, branch }); + if (data.commit.sha !== context.sha) { + core.setFailed( + `${branch} is at ${data.commit.sha}, but this run would deploy ${context.sha}. ` + + `To roll back, revert the change on ${branch} and run the workflow again.`, + ); + } - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: persist-credentials: false diff --git a/docs/e2e-plan.md b/docs/e2e-plan.md index 3db6563..3c70c74 100644 --- a/docs/e2e-plan.md +++ b/docs/e2e-plan.md @@ -175,7 +175,7 @@ Staging policies belong to CI. Do not use these names for an operator-managed pr The workflow repeats `vp run check` and `vp run smoke`, then runs `vp run ci:deploy production`. `scripts/ci/production.ts` names the Worker, D1 database, and R2 bucket. `.github/production-repositories.jsonc` maps each cache namespace to a public GitHub repository. The command refuses other repositories, branches, and events, so repositories created by Deploy to Cloudflare cannot run it. Staging and production share `scripts/ci/deploy.ts`. Setup creates or reuses the named resources, applies migrations, and binds each namespace; then the Worker deploys once. Every namespace endpoint must then serve the new deployment ID. The run summary and the `production` environment list the endpoints, for example `https://voidzero-remote-cache..workers.dev/projects/rolldown`. -To bind another repository, add `"namespace": "owner/repository"` to `.github/production-repositories.jsonc`, merge the change, and run the workflow. Only public repositories can publish. The list decides which namespaces serve reads and accept uploads: each deployment enables every listed namespace and disables every other namespace, so a `pnpm operator policy` change lasts only until the next deployment. To withdraw a repository, remove its entry and run the workflow. For an immediate stop, also run `pnpm operator policy --namespace --enabled off`. Data in a disabled namespace expires with its retention; `pnpm operator purge` deletes it sooner. Deployments never change the cache-wide `pnpm operator deployment` switches or move a namespace to another repository. To roll back, re-run an earlier successful production run. A re-run deploys its original commit. +To bind another repository, add `"namespace": "owner/repository"` to `.github/production-repositories.jsonc`, merge the change, and run the workflow. Only public repositories can publish. The list decides which namespaces serve reads and accept uploads: each deployment enables every listed namespace and disables every other namespace, so a `pnpm operator policy` change lasts only until the next deployment. To withdraw a repository, remove its entry and run the workflow. For an immediate stop, also run `pnpm operator policy --namespace --enabled off`. Data in a disabled namespace expires with its retention; `pnpm operator purge` deletes it sooner. Deployments never change the cache-wide `pnpm operator deployment` switches or move a namespace to another repository. To roll back, revert the change on `main` and run the workflow. The workflow deploys only the current head of `main` and refuses a re-run of an earlier run: a re-run deploys its original commit, including that commit's repository list and pull request opt-ins, so it would silently undo later withdrawals and opt-outs. Runs made before this check was added don't have it, so don't re-run them. An entry can also be an object. `{ "repository": "owner/repository", "pullRequests": true }` also accepts uploads from `pull_request` runs in that repository whose PR targets its default branch; `"owner/repository"` is short for `"pullRequests": false`. Each deployment also sets every namespace's pull request opt-in from the list, overriding `pnpm operator policy --pull-requests`. This opt-in is interim, and only the `-replay` namespaces, which replay other repositories' CI in forks, have it. Code from any same-repository PR, including Renovate dependency branches, runs before review and can publish an entry that a later default-branch build restores. Enable it only for a repository whose maintainers accept that risk. The long-term fix is to [scope pull request uploads to the pull request](https://github.com/voidzero-dev/vite-plus-remote-cache-cloudflare/issues/16). Fork PRs stay out only because GitHub does not grant their workflows `id-token: write`; see [Pull request uploads](README.md#pull-request-uploads). diff --git a/scripts/ci/deploy.ts b/scripts/ci/deploy.ts index 004a20a..99c0772 100644 --- a/scripts/ci/deploy.ts +++ b/scripts/ci/deploy.ts @@ -45,6 +45,8 @@ export interface Target { // Namespace to public GitHub owner/repository. bindings: Readonly>; setupArgs?: string[]; + // Runs before setup changes anything and again just before prepare. Throw to stop the deployment. + verify?(): Promise; // Runs after every namespace is set up, before the Worker deployment. prepare?(config: Config, io: OperatorIO): Promise; } @@ -85,6 +87,7 @@ export async function deployTarget( await io.writeConfig(config); }, }; + await target.verify?.(); for (const namespace of namespaces) await runOperator( [ @@ -104,6 +107,7 @@ export async function deployTarget( setupIO, ); const config = await io.readConfig(); + await target.verify?.(); await target.prepare?.(config, io); const scopes = await query(io, config, 'SELECT scope_id FROM scopes'); config.vars['NAMESPACES'] = JSON.stringify(scopes.map((scope) => String(scope['scope_id']))); diff --git a/scripts/ci/production.ts b/scripts/ci/production.ts index ddaa314..f362781 100644 --- a/scripts/ci/production.ts +++ b/scripts/ci/production.ts @@ -1,5 +1,7 @@ +import { execFile } from 'node:child_process'; import { readFileSync } from 'node:fs'; import { URL } from 'node:url'; +import { promisify } from 'node:util'; import { parse, type ParseError } from 'jsonc-parser'; import { query } from '../operator.ts'; import type { RunContext, Target } from './deploy.ts'; @@ -18,6 +20,18 @@ export const production: ProductionConfig = { profile: 'free', }; +// The commit that a public repository's branch points to now. Reads the remote, not the checkout. +export async function branchHead(repository: string, ref: string): Promise { + const { stdout } = await promisify(execFile)( + 'git', + ['ls-remote', `https://github.com/${repository}.git`, ref], + { timeout: 30_000 }, + ); + const sha = /^([a-f0-9]{40})\t/.exec(stdout)?.[1]; + if (!sha) throw new Error(`Could not read ${ref} of ${repository}`); + return sha; +} + // Add or remove production repositories in this file. const repositoriesFile = new URL('../../.github/production-repositories.jsonc', import.meta.url); @@ -59,9 +73,10 @@ export function parseRepositories(text: string): Record, + context: Pick, config: ProductionConfig = production, repositories: string = readFileSync(repositoriesFile, 'utf8'), + head: () => Promise = () => branchHead(config.source, config.ref), ): Target { // Repositories created by Deploy to Cloudflare copy this workflow; they must not deploy it. if (context.repository !== config.source) @@ -82,6 +97,16 @@ export function productionTarget( bindings: Object.fromEntries( entries.map(([namespace, entry]) => [namespace, entry.repository]), ), + // A re-run deploys its original commit, and prepare applies that commit's list. Deploy only the + // commit main points to now, checked again right before the policy changes. + async verify() { + const current = await head(); + if (current !== context.sha) + throw new Error( + `${config.ref} is at ${current}, but this run would deploy ${context.sha}. ` + + 'To roll back, revert the change on main and run the workflow again.', + ); + }, // The list decides which namespaces serve reads, accept uploads, and accept pull request // uploads, overriding `pnpm operator policy`. Only changed rows are updated: the changed_policy // trigger bumps policy_version on any switch update, which stops in-flight uploads from publishing. diff --git a/test/ci.test.ts b/test/ci.test.ts index fd6832b..1f8cdb3 100644 --- a/test/ci.test.ts +++ b/test/ci.test.ts @@ -12,6 +12,7 @@ const dispatch = { repository: production.source, event: 'workflow_dispatch', ref: 'refs/heads/main', + sha, }; test('CI run context identifies the source commit and workflow attempt', () => { @@ -166,6 +167,42 @@ test('CI deployments set up every namespace before one Worker deployment and pre log.length = 0; await assert.rejects(run({ subdomain: 'other' }), /Workers subdomain/); + assert.deepEqual([...log], []); + + // verify runs before setup changes anything and again right before prepare. + repositories['acme/two'] = 2; + log.length = 0; + await run({ + async verify() { + log.push('verify'); + }, + }); + assert.equal(log[0], 'verify'); + assert.equal(log[log.indexOf('prepare') - 1], 'verify'); + + // A branch that advances after the first check stops the deployment before prepare. + log.length = 0; + let checks = 0; + await assert.rejects( + run({ + async verify() { + if (++checks > 1) throw new Error('main advanced'); + }, + }), + /main advanced/, + ); + assert.deepEqual(deploys(), []); + + // A stale run stops before setup changes anything. + log.length = 0; + await assert.rejects( + run({ + async verify() { + throw new Error('stale run'); + }, + }), + /stale run/, + ); assert.deepEqual(log, []); } finally { await h.close(); @@ -186,6 +223,15 @@ test('production deploys only manual default-branch runs from this repository', assert.throws(() => productionTarget(context, { ...production, name }), /staging/); }); +test('production deploys only the commit main points to when it checks', async () => { + const heads = [sha, 'b'.repeat(40)]; + const target = productionTarget(dispatch, production, '{ "test": "owner/repo" }', async () => + heads.shift()!, + ); + await target.verify!(); + await assert.rejects(target.verify!(), /refs\/heads\/main is at b{40}.*revert/); +}); + test('production repository lists map namespaces to public owner/repo names', () => { assert.deepEqual( parseRepositories(