Files
recflare/packages/tools/bin/run-wrangler-deploy
T
2026-06-30 13:03:46 -04:00

70 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
set -eu
# Extract name and version from package.json using jq
NAME=$(jq -r '.name' package.json)
VERSION=$(get-version)
# Resolve the base domain (and this worker's subdomain), then deploy onto the
# custom domain via `--domain`. This keeps the real domain out of versioned files
# — committed wrangler.jsonc has no routes, and the base domain is passed as the
# DOMAIN var at runtime.
#
# The domain comes from the RECFLARE_DOMAIN env var. For local dev that's set in a
# gitignored .env at the repo root; in CI it's an exported secret. An already-set
# RECFLARE_DOMAIN wins and skips the file. Per-app subdomain overrides come from
# RECFLARE_SUBDOMAINS (a JSON object, e.g. {"playersettings":"settings"}).
if [ -z "${RECFLARE_DOMAIN:-}" ]; then
ENV_FILE="$(git rev-parse --show-toplevel)/.env"
if [ -f "$ENV_FILE" ]; then
set -a
. "$ENV_FILE"
set +a
fi
fi
if [ -z "${RECFLARE_DOMAIN:-}" ]; then
echo "error: RECFLARE_DOMAIN is not set — export it or add it to .env (see .env.example)" >&2
exit 1
fi
DOMAIN="$RECFLARE_DOMAIN"
SUBDOMAINS_JSON=${RECFLARE_SUBDOMAINS:-}
[ -n "$SUBDOMAINS_JSON" ] || SUBDOMAINS_JSON='{}'
DIR=$(basename "$PWD")
# Per-app subdomain override, falling back to the worker's directory name.
SUBDOMAIN=$(printf '%s' "$SUBDOMAINS_JSON" | jq -r --arg d "$DIR" '.[$d] // $d')
HOST="$SUBDOMAIN.$DOMAIN"
# Splice the shared `recflare` D1 database id from RECFLARE_D1 into a generated
# config. All D1-backed workers bind the same database, so RECFLARE_D1 is a single
# id. The committed wrangler.jsonc carries a "local" placeholder so it needs no
# per-developer edits; the real id stays in the gitignored .env / CI secrets.
# Workers without a D1 binding simply have no placeholder to replace.
CONFIG="wrangler.jsonc"
if grep -q '"database_id": *"local"' wrangler.jsonc 2>/dev/null; then
DB_ID=${RECFLARE_D1:-}
if [ -z "$DB_ID" ]; then
echo "error: RECFLARE_D1 is not set — add the recflare D1 id to .env (see .env.example)" >&2
exit 1
fi
CONFIG="wrangler.generated.jsonc"
# Generated alongside the original so its relative paths (main, migrations_dir)
# still resolve. Gitignored; removed on exit so `wrangler dev` is unaffected.
trap 'rm -f wrangler.generated.jsonc' EXIT
sed -E 's/("database_id"[[:space:]]*:[[:space:]]*")[^"]*(")/\1'"$DB_ID"'\2/' \
wrangler.jsonc >wrangler.generated.jsonc
fi
# Deploy with wrangler using the extracted values as binding variables
echo "Deploying worker $NAME version $VERSION to $HOST"
wrangler deploy \
--config "$CONFIG" \
--var NAME:"$NAME" \
--var SENTRY_RELEASE:"$VERSION" \
--var DOMAIN:"$DOMAIN" \
--domain "$HOST" \
--minify \
"$@"