opencode/.github/release/deploy-update.sh
Dax Raad 7bac5bad02 fix(deploy): run Cloudflare deploys from verified artifacts behind pinned targets
Repository code outside .github/ needs no review, but every deploy job held a
Cloudflare token strong enough to write any Worker on the account (the data
sync Workflow PUT has no per-Worker permission). A dev merge could therefore
overwrite production Workers.

Split each service into a build job with no environment, secrets, or OIDC token
that uploads a SHA256SUMS artifact, and a deploy job in dev|production that
checks out only .github/release/, verifies the artifact against the build job's
digest, installs the cf/wrangler CLI pinned here, and runs only deploy-*.sh.

The deploy scripts treat the artifact as data and refuse it unless its resolved
config equals the pinned target in .github/release/targets/ exactly: Worker
names, routes, custom domains, crons, Workflow names, and bindings. For cf
services the pinned config is what cf reads. That is what makes an account-wide
token safe, and it keeps production out of reach of a dev merge.

Adds the cf 1.0.0-beta.12 and wrangler 4.110.0 lockfiles the deploy jobs
install with npm ci --ignore-scripts.
2026-11-11 04:51:07 -05:00

38 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# deploy-update.sh <production> <dir>: deploys the update Worker from a verified
# bundle artifact (index.js built by `wrangler deploy --dry-run`) with the
# wrangler CLI pinned in wrangler/package-lock.json.
#
# The build job has no secrets, so the bundle is untrusted by design: its code
# can change freely. The rest of the Worker does not come from the artifact at
# all: the name, routes, custom domain, D1 database, and Pipelines stream are
# the complete pinned wrangler.json in targets/update/production/, which is what
# wrangler reads. Only production deploys this Worker.
. "$(dirname "$0")/lib.sh"
mode=$1
src=$(cd "$2" && pwd)
release=$(cd "$(dirname "$0")" && pwd)
[ "$mode" = production ] || die "The update Worker deploys only to production, not $mode"
target="$release/targets/update/production/wrangler.json"
[ -f "$target" ] || die "No pinned update target"
: "${CLOUDFLARE_API_TOKEN:?Set CLOUDFLARE_API_TOKEN}"
# Pinned in targets/ and in the token issuer's config (anomalyco/secrets).
export CLOUDFLARE_ACCOUNT_ID=15d29c8639fd3733b1b5486a2acfd968
unexpected=$(cd "$src" && find . ! -type d ! -type f -o -type f ! -path ./SHA256SUMS ! -path ./index.js)
[ -z "$unexpected" ] || die "Unexpected files in the update artifact: $unexpected"
[ -f "$src/index.js" ] || die "The update artifact has no index.js"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cp "$src/index.js" "$work/index.js"
cp "$target" "$work/wrangler.json"
wrangler="$release/wrangler/node_modules/.bin/wrangler"
[ -x "$wrangler" ] || die "Install the pinned wrangler CLI first: npm ci --ignore-scripts in .github/release/wrangler"
cd "$work"
"$wrangler" deploy --config wrangler.json --no-bundle