diff --git a/docs/DEPLOY.md b/docs/DEPLOY.md index fc5fb9a..42a5ea7 100644 --- a/docs/DEPLOY.md +++ b/docs/DEPLOY.md @@ -10,7 +10,8 @@ Gitea -> webhook -> the container pulls and redeploys itself. No manual `scp`/`r the webhook secret from `.env`), checks `ref == refs/heads/main`, and that the push touched a path this container owns (table below). Otherwise it answers 202 and does nothing. 3. It runs `infra/deploy/.sh ` under a lock (one deploy at a time per container): - - `git fetch` + `git reset --hard ` in `/srv/photoreport` + - `git fetch`; a sha that is not the tip of `origin/main` is logged and skipped (exit 0, anti-downgrade, BIT-34038) + - `git reset --hard ` in `/srv/photoreport` - `pnpm install --frozen-lockfile` / `cargo build --release -p ` as needed - build only the affected package (`pnpm --filter build`) - API only: `prisma migrate deploy` diff --git a/infra/deploy-receiver/src/deploy-guard.test.ts b/infra/deploy-receiver/src/deploy-guard.test.ts new file mode 100644 index 0000000..0fed0ce --- /dev/null +++ b/infra/deploy-receiver/src/deploy-guard.test.ts @@ -0,0 +1,42 @@ +import { execFileSync, spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; + +const SCRIPT = path.resolve(__dirname, "../../deploy/deploy.sh"); +const tmp = mkdtempSync(path.join(tmpdir(), "deploy-guard-")); +afterAll(() => rmSync(tmp, { recursive: true, force: true })); + +const git = (cwd: string, ...args: string[]) => + execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", ...args], { cwd, encoding: "utf8" }).trim(); + +describe("deploy.sh anti-downgrade (BIT-34038)", () => { + it("skips an old ancestor SHA with exit 0 and leaves the checkout untouched", () => { + const origin = path.join(tmp, "origin.git"); + const work = path.join(tmp, "work"); + git(tmp, "init", "-q", "--bare", "-b", "main", origin); + git(tmp, "clone", "-q", origin, work); + git(work, "commit", "-q", "--allow-empty", "-m", "old"); + const oldSha = git(work, "rev-parse", "HEAD"); + git(work, "commit", "-q", "--allow-empty", "-m", "new"); + const tip = git(work, "rev-parse", "HEAD"); + git(work, "push", "-q", "origin", "main"); + + const r = spawnSync("bash", [SCRIPT, "pending", oldSha], { + env: { ...process.env, DEPLOY_REPO_DIR: work, DEPLOY_BRANCH: "main" }, + encoding: "utf8", + }); + expect(r.status).toBe(0); + expect(r.stdout).toContain("SKIP"); + expect(git(work, "rev-parse", "HEAD")).toBe(tip); + + // The tip itself is deployed (the "workers" service is a no-op build). + const ok = spawnSync("bash", [SCRIPT, "workers", tip], { + env: { ...process.env, DEPLOY_REPO_DIR: work, DEPLOY_BRANCH: "main" }, + encoding: "utf8", + }); + expect(ok.status).toBe(0); + expect(ok.stdout).toContain("succeeded"); + }); +}); diff --git a/infra/deploy/deploy.sh b/infra/deploy/deploy.sh index d54d829..99432f3 100755 --- a/infra/deploy/deploy.sh +++ b/infra/deploy/deploy.sh @@ -187,6 +187,17 @@ run_service() { esac } +# Anti-downgrade (BIT-34038): a redelivered webhook for an older commit is still an ancestor of origin/main, so the +# ancestry check in checkout() lets it through and `reset --hard` would move the site backwards (BIT-34036). Only the +# tip of origin/main is deployed; anything else is logged and skipped with exit 0 (the receiver reports success, no +# action). The newer push has its own webhook. The rollback below goes through checkout() directly, unaffected. +git fetch --quiet origin "$BRANCH" || die "git fetch origin $BRANCH failed" +TIP="$(git rev-parse "origin/$BRANCH")" +if [[ "$SHA" != "$TIP" ]]; then + log "SKIP: $SHA is not the tip of origin/$BRANCH ($TIP); refusing to deploy $SERVICE (anti-downgrade)" + exit 0 +fi + PREVIOUS="$(git rev-parse HEAD)" log "deploying $SERVICE: $PREVIOUS -> $SHA" backup_env