Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Add nightly cleanup of stale Neon preview branches (#3121)

Luke Vella9a94c97b15c0
diff --git a/.github/workflows/delete-stale-preview-branches.yml b/.github/workflows/delete-stale-preview-branches.yml
new file mode 100644
index 000000000..2abc93a9e
--- /dev/null
+++ b/.github/workflows/delete-stale-preview-branches.yml
@@ -0,0 +1,66 @@
+name: Delete stale preview branches
+# -----------------------------------------------------------------
+# Workflow Purpose:
+# Preview deployments get a Neon branch containing a copy of
+# production data. This job deletes any preview/* branch older
+# than the TTL so those copies never outlive the retention
+# commitments in our DPA. The Vercel integration recreates a
+# branch (with fresh data) on the next push to that git branch.
+# Trigger: Nightly at 03:30 UTC, or manually.
+# Config: NEON_API_KEY secret (project-scoped), NEON_PROJECT_ID variable
+# -----------------------------------------------------------------
+on:
+ schedule:
+ - cron: "30 3 * * *"
+ workflow_dispatch:
+
+env:
+ TTL_DAYS: 7
+
+jobs:
+ delete-stale-preview-branches:
+ runs-on: ubuntu-latest
+ permissions: {}
+ steps:
+ - name: Delete preview branches older than TTL
+ env:
+ NEON_API_KEY: ${{ secrets.NEON_API_KEY }}
+ NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
+ run: |
+ set -euo pipefail
+ base="https://console.neon.tech/api/v2/projects/${NEON_PROJECT_ID}"
+ cutoff=$(date -u -d "${TTL_DAYS} days ago" +%Y-%m-%dT%H:%M:%SZ)
+
+ stale=""
+ cursor=""
+ while :; do
+ page=$(curl -sfG "${base}/branches" \
+ --data-urlencode "limit=200" \
+ ${cursor:+--data-urlencode "cursor=${cursor}"} \
+ -H "Authorization: Bearer ${NEON_API_KEY}" -H "Accept: application/json")
+ stale+=$(echo "$page" | jq -r --arg cutoff "$cutoff" '
+ .branches[]
+ | select(.name | startswith("preview/"))
+ | select(.default == false and .protected == false)
+ | select(.created_at < $cutoff)
+ | .id + " " + .name')$'\n'
+ cursor=$(echo "$page" | jq -r '.pagination.next // empty')
+ [ -n "$cursor" ] || break
+ done
+
+ if ! grep -q '[^[:space:]]' <<< "$stale"; then
+ echo "No preview branches older than ${TTL_DAYS} days."
+ exit 0
+ fi
+
+ failed=0
+ while read -r id name; do
+ [ -n "$id" ] || continue
+ echo "Deleting $name ($id, older than ${TTL_DAYS} days)"
+ if ! curl -sf -X DELETE "${base}/branches/${id}" \
+ -H "Authorization: Bearer ${NEON_API_KEY}" -H "Accept: application/json" > /dev/null; then
+ echo "::error::Failed to delete $name ($id); continuing"
+ failed=1
+ fi
+ done <<< "$stale"
+ exit "$failed"