opencode/.github/release/deploy-site.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

54 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# deploy-site.sh <dev|production> <dir>: deploys the site Worker from a verified
# Build Output artifact (services/site/.cloudflare/output) with the cf CLI
# pinned in cf/package-lock.json, which the workflow installs before the token
# is in reach.
#
# The build job has no secrets, so the Worker's code and assets are untrusted
# by design: the site can change freely. Its config may not. The artifact's
# root config and Worker config must equal targets/site/<mode>/*.json exactly,
# and those pinned copies are what cf reads: another Worker name, route,
# domain, or binding is refused before the token is used.
. "$(dirname "$0")/lib.sh"
mode=$1
src=$(cd "$2" && pwd)
release=$(cd "$(dirname "$0")" && pwd)
case "$mode" in
dev | production) ;;
*) die "Unknown site mode: $mode" ;;
esac
target="$release/targets/site/$mode"
[ -f "$target/root.json" ] && [ -f "$target/worker.json" ] || die "No pinned site targets for $mode"
: "${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 ./v0/config.json \
! -path ./v0/workers/default/worker.config.json \
! -path './v0/workers/default/bundle/*' \
! -path './v0/workers/default/assets/*')
[ -z "$unexpected" ] || die "Unexpected files in the site artifact: $unexpected"
[ -f "$src/v0/workers/default/bundle/index.mjs" ] || die "The site artifact has no bundle/index.mjs"
[ -d "$src/v0/workers/default/assets" ] || die "The site artifact has no assets directory"
expect_json "$src/v0/config.json" "$target/root.json" "The site's root config"
expect_json "$src/v0/workers/default/worker.config.json" "$target/worker.json" "The site's Worker config"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
output="$work/.cloudflare/output/v0"
mkdir -p "$output/workers/default"
cp "$target/root.json" "$output/config.json"
cp "$target/worker.json" "$output/workers/default/worker.config.json"
cp -R "$src/v0/workers/default/bundle" "$output/workers/default/bundle"
cp -R "$src/v0/workers/default/assets" "$output/workers/default/assets"
cf="$release/cf/node_modules/.bin/cf"
[ -x "$cf" ] || die "Install the pinned cf CLI first: npm ci --ignore-scripts in .github/release/cf"
cd "$work"
"$cf" deploy --prebuilt --mode "$mode"