Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/cleanup-preview.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions scripts/vercel-build.sh
Original file line number Diff line number Diff line change
@@ -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
Loading