- push to main builds, backs up prod (pg_dump 17), runs db-migrate + seeds, preflights and deploys; rollback = workflow_dispatch with rollback_tag - staging workflow, promote step and migrate-*.ts / old SQL removed - DB_URL = app role, DB_MIGRATE_URL = owner role - scripts/db-refresh-dev.sh (prod dump -> local dev DB), scripts/preview.sh <branch>
59 lines
2.2 KiB
Bash
Executable File
59 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run a branch against a throwaway copy of the dev database (itself a prod copy).
|
|
#
|
|
# scripts/preview.sh <branch> [port]
|
|
#
|
|
# Creates a git worktree in /tmp, clones DEV_DB_NAME into a temporary database
|
|
# (CREATE DATABASE ... TEMPLATE), runs that branch's Drizzle migrations on the copy,
|
|
# builds and serves it on <port> (default 5180). Ctrl-C removes the server,
|
|
# database and worktree. Prod and the dev DB itself are never modified.
|
|
#
|
|
# Uses .env from the main checkout: DB_URL (app role) and DB_MIGRATE_URL (owner role)
|
|
# pointing at the dev DB, plus DEV_ADMIN_URL (superuser, maintenance DB) for create/drop.
|
|
set -euo pipefail
|
|
|
|
branch="${1:?usage: scripts/preview.sh <branch> [port]}"
|
|
port="${2:-5180}"
|
|
repo="$(cd "$(dirname "$0")/.." && pwd)"
|
|
cd "$repo"
|
|
set -a
|
|
. ./.env
|
|
[[ -f .env.dev-refresh ]] && . ./.env.dev-refresh
|
|
set +a
|
|
: "${DB_URL:?}" "${DB_MIGRATE_URL:?}" "${DEV_ADMIN_URL:?set DEV_ADMIN_URL}"
|
|
DEV_DB_NAME="${DEV_DB_NAME:-personal_dev}"
|
|
|
|
safe="$(printf '%s' "$branch" | tr -c 'a-zA-Z0-9' '_' | tr 'A-Z' 'a-z' | cut -c1-40)"
|
|
db="preview_${safe}"
|
|
dir="/tmp/personal-website-preview-${safe}"
|
|
with_db() { bun -e 'const u=new URL(process.argv[1]); u.pathname="/"+process.argv[2]; console.log(u.toString())' "$1" "$2"; }
|
|
|
|
server_pid=""
|
|
cleanup() {
|
|
[[ -n "$server_pid" ]] && kill "$server_pid" 2>/dev/null || true
|
|
psql -q "$DEV_ADMIN_URL" -c "DROP DATABASE IF EXISTS \"$db\" WITH (FORCE)" || true
|
|
git -C "$repo" worktree remove --force "$dir" 2>/dev/null || true
|
|
echo "Preview cleaned up."
|
|
}
|
|
trap cleanup EXIT INT TERM
|
|
|
|
git fetch origin "$branch"
|
|
git worktree add --detach "$dir" "origin/$branch"
|
|
|
|
# TEMPLATE needs no other connections to the source; stop `bun run dev` first if it fails.
|
|
psql -v ON_ERROR_STOP=1 -q "$DEV_ADMIN_URL" -c "CREATE DATABASE \"$db\" TEMPLATE \"$DEV_DB_NAME\" OWNER personal"
|
|
|
|
export DB_URL="$(with_db "$DB_URL" "$db")"
|
|
export DB_MIGRATE_URL="$(with_db "$DB_MIGRATE_URL" "$db")"
|
|
export ORIGIN="http://localhost:$port" BETTER_AUTH_URL="http://localhost:$port" PORT="$port"
|
|
|
|
cd "$dir"
|
|
cp "$repo/.env" .env
|
|
bun install --frozen-lockfile
|
|
bun scripts/db-migrate.ts
|
|
bun run build
|
|
echo "Preview of $branch on http://localhost:$port (Ctrl-C to stop and clean up)"
|
|
bun build/index.js &
|
|
server_pid=$!
|
|
wait "$server_pid"
|