diff --git a/.github/workflows/cleanup-preview.yml b/.github/workflows/cleanup-preview.yml new file mode 100644 index 0000000..7b0e388 --- /dev/null +++ b/.github/workflows/cleanup-preview.yml @@ -0,0 +1,34 @@ +name: Cleanup Preview Branch + +on: + pull_request: + types: [closed] + +permissions: + contents: read + +jobs: + cleanup: + name: Delete Xata preview branch + runs-on: ubuntu-latest + steps: + - name: Install Xata CLI + run: | + curl -fsSL https://xata.io/install.sh | bash + echo "$HOME/.config/xata/bin" >> $GITHUB_PATH + + - name: Delete preview branch + env: + XATA_API_KEY: ${{ secrets.XATA_API_KEY }} + XATA_ORG_ID: ${{ secrets.XATA_ORG_ID }} + XATA_MAIN_PROJECT_ID: ${{ secrets.XATA_MAIN_PROJECT_ID }} + REF: ${{ github.event.pull_request.head.ref }} + run: | + set -euo pipefail + SAFE_REF=$(echo -n "$REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) + BRANCH_NAME="preview-${SAFE_REF}" + echo "Deleting Xata branch $BRANCH_NAME" + xata branch delete "$BRANCH_NAME" \ + --organization "$XATA_ORG_ID" \ + --project "$XATA_MAIN_PROJECT_ID" \ + --yes || true diff --git a/package.json b/package.json index 019d893..9293b6c 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", + "vercel-build": "bash scripts/vercel-build.sh", "start": "next start", "lint": "eslint", "db:generate": "drizzle-kit generate", diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh new file mode 100755 index 0000000..0334367 --- /dev/null +++ b/scripts/vercel-build.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Vercel build entry point. For preview deploys, fork a Xata branch off `main` +# and point DATABASE_URL at it so each PR gets isolated metadata storage. +# Production builds (main) fall through to a plain `next build` that reads the +# DATABASE_URL configured in the Vercel project. + +set -euo pipefail + +# The CLI reads XATA_API_KEY directly from the env; org/project come in via flags. +export XATA_API_KEY="$XATA_VERCEL_API_KEY" + +if ! command -v xata >/dev/null 2>&1; then + curl -fsSL https://xata.io/install.sh | bash + export PATH="$HOME/.config/xata/bin:$PATH" +fi + +xata version + +if [[ "${VERCEL_ENV:-}" == "preview" && -n "${VERCEL_GIT_COMMIT_REF:-}" ]]; then + # Sanitize the git ref into a legal Xata branch name (cap at 50 chars so + # the "preview-" prefix still leaves room under Xata's 63-char limit). + SAFE_REF=$(echo -n "$VERCEL_GIT_COMMIT_REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) + BRANCH_NAME="preview-${SAFE_REF}" + + XATA_FLAGS=(--organization "$XATA_ORG_ID" --project "$XATA_MAIN_PROJECT_ID") + + # Recreate from scratch on every push so the preview reflects the PR's + # current schema + seed without drift across rebuilds. + xata branch delete "$BRANCH_NAME" "${XATA_FLAGS[@]}" --yes || true + xata branch create --name "$BRANCH_NAME" "${XATA_FLAGS[@]}" + xata branch wait-ready "$BRANCH_NAME" "${XATA_FLAGS[@]}" + + DSN=$(xata branch url "$BRANCH_NAME" --type primary "${XATA_FLAGS[@]}") + echo "DATABASE_URL=$DSN" >> .env.production + + DATABASE_URL="$DSN" npm run db:migrate +fi + +npm run build