README rewritten around three steps (table SQL, secrets, self-contained workflow), with a vendored full-script workflow, a crontab example and an npx-from-git CLI. Every link and uses: line that pointed at the 404 GitHub repo is gone. keepalive.sh no longer prints the anon key outside GitHub Actions (::add-mask:: only when GITHUB_ACTIONS=true). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
300 lines
12 KiB
Bash
300 lines
12 KiB
Bash
#!/usr/bin/env bash
|
|
# Supabase Keep-Alive Ping
|
|
# Pings a Supabase project's REST API and runs a cheap query with the anon key,
|
|
# retries on transient errors, writes a job summary, and warns before the 7-day
|
|
# inactivity window. Plain bash + curl; jq is used for the optional GitHub API checks.
|
|
set -uo pipefail
|
|
|
|
URL="${INPUT_URL:-}"
|
|
KEY="${INPUT_ANON_KEY:-}"
|
|
TABLE="${INPUT_TABLE:-}"
|
|
SELECT="${INPUT_SELECT:-*}"
|
|
RPC="${INPUT_RPC:-}"
|
|
RPC_BODY="${INPUT_RPC_BODY:-{\}}"
|
|
RETRIES="${INPUT_RETRIES:-3}"
|
|
TIMEOUT="${INPUT_TIMEOUT:-20}"
|
|
WARN_DAYS="${INPUT_WARN_AFTER_DAYS:-5}"
|
|
FAIL_ON_PAUSED="${INPUT_FAIL_ON_PAUSED:-true}"
|
|
TOKEN="${INPUT_GITHUB_TOKEN:-}"
|
|
|
|
SUMMARY="${GITHUB_STEP_SUMMARY:-/dev/null}"
|
|
OUTPUT="${GITHUB_OUTPUT:-/dev/null}"
|
|
API="${GITHUB_API_URL:-https://api.github.com}"
|
|
|
|
PAUSED_CODE=540 # Supabase custom status: project paused
|
|
GITHUB_DISABLE_DAYS=60 # GitHub disables cron in public repos after 60 idle days
|
|
GITHUB_DISABLE_WARN_AT=45 # warn from this many idle days
|
|
|
|
STATUS="ok"
|
|
PAUSED="false"
|
|
REST_CODE=""
|
|
QUERY_CODE=""
|
|
LAST_SUCCESS_DAYS=""
|
|
ROWS=() # summary table rows
|
|
WARNINGS=() # summary warnings
|
|
BODY_FILE="$(mktemp)"
|
|
trap 'rm -f "$BODY_FILE"' EXIT
|
|
|
|
log() { printf '%s\n' "$*"; }
|
|
warn() { WARNINGS+=("$*"); printf '::warning::%s\n' "$*"; }
|
|
fail() { printf '::error::%s\n' "$*"; STATUS="failed"; }
|
|
notice() { printf '::notice::%s\n' "$*"; }
|
|
set_output() { printf '%s=%s\n' "$1" "$2" >> "$OUTPUT"; }
|
|
|
|
# ---------- validation ----------
|
|
if [[ -z "$URL" ]]; then
|
|
printf '::error::input url is required (e.g. https://abcdefgh.supabase.co)\n'
|
|
exit 1
|
|
fi
|
|
if [[ -z "$KEY" ]]; then
|
|
printf '::error::input anon-key is required\n'
|
|
exit 1
|
|
fi
|
|
# Mask the key in GitHub Actions logs. Outside Actions this line would print the key, so skip it.
|
|
if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then printf '::add-mask::%s\n' "$KEY"; fi
|
|
URL="${URL%%+(/)}"; URL="${URL%/}"
|
|
if [[ ! "$URL" =~ ^https?://[A-Za-z0-9.-]+(:[0-9]+)?$ ]]; then
|
|
printf '::error::url must be the project origin only, like https://abcdefgh.supabase.co (got %s)\n' "$URL"
|
|
exit 1
|
|
fi
|
|
for n in "$RETRIES" "$TIMEOUT" "$WARN_DAYS"; do
|
|
if [[ ! "$n" =~ ^[0-9]+$ ]]; then
|
|
printf '::error::retries, timeout and warn-after-days must be whole numbers\n'
|
|
exit 1
|
|
fi
|
|
done
|
|
if [[ -n "$TABLE" && ! "$TABLE" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
|
printf '::error::table must be a plain identifier (letters, digits, underscore)\n'
|
|
exit 1
|
|
fi
|
|
if [[ -n "$RPC" && ! "$RPC" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
|
printf '::error::rpc must be a plain function name (letters, digits, underscore)\n'
|
|
exit 1
|
|
fi
|
|
HOST="${URL#*://}"
|
|
|
|
# ---------- HTTP with retries ----------
|
|
# request LABEL METHOD PATH [BODY] -> sets R_CODE R_MS R_ATTEMPTS, body in $BODY_FILE
|
|
R_CODE=""; R_MS=""; R_ATTEMPTS=0
|
|
request() {
|
|
local label="$1" method="$2" path="$3" body="${4:-}"
|
|
local attempt=0 delay=2 out code secs
|
|
while :; do
|
|
attempt=$((attempt + 1))
|
|
: > "$BODY_FILE"
|
|
if [[ -n "$body" ]]; then
|
|
out=$(curl -sS -o "$BODY_FILE" -w '%{http_code} %{time_total}' --max-time "$TIMEOUT" \
|
|
-X "$method" -H "apikey: $KEY" -H "Authorization: Bearer $KEY" \
|
|
-H "Content-Type: application/json" -H "Accept: application/json" \
|
|
--data "$body" "$URL$path" 2>/dev/null) || out="000 0"
|
|
else
|
|
out=$(curl -sS -o "$BODY_FILE" -w '%{http_code} %{time_total}' --max-time "$TIMEOUT" \
|
|
-X "$method" -H "apikey: $KEY" -H "Authorization: Bearer $KEY" \
|
|
-H "Accept: application/json" "$URL$path" 2>/dev/null) || out="000 0"
|
|
fi
|
|
code="${out%% *}"; secs="${out##* }"
|
|
[[ "$code" =~ ^[0-9]{3}$ ]] || code="000"
|
|
R_CODE="$code"; R_MS=$(awk -v s="$secs" 'BEGIN{printf "%d", s*1000}')
|
|
R_ATTEMPTS=$attempt
|
|
log "$label: $method $path -> HTTP $code (${R_MS} ms, attempt $attempt)"
|
|
case "$code" in
|
|
"$PAUSED_CODE") return 0 ;; # paused: no point retrying
|
|
000|429|5[0-9][0-9])
|
|
if (( attempt <= RETRIES )); then
|
|
log " transient (HTTP $code), retrying in ${delay}s"
|
|
sleep "$delay"; delay=$((delay * 2)); continue
|
|
fi
|
|
return 0 ;;
|
|
*) return 0 ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
add_row() { ROWS+=("| $1 | \`$2\` | $3 | $4 ms | $5 |"); }
|
|
|
|
body_snippet() {
|
|
tr -d '\n' < "$BODY_FILE" | cut -c1-200
|
|
}
|
|
|
|
check_paused() {
|
|
if [[ "$R_CODE" == "$PAUSED_CODE" ]]; then
|
|
PAUSED="true"
|
|
STATUS="paused"
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# ---------- 1. REST root ----------
|
|
request "REST root" GET "/rest/v1/"
|
|
REST_CODE="$R_CODE"
|
|
add_row "REST root" "GET /rest/v1/" "$R_CODE" "$R_MS" "$R_ATTEMPTS"
|
|
if check_paused; then
|
|
:
|
|
elif [[ "$R_CODE" == "401" || "$R_CODE" == "403" ]]; then
|
|
fail "anon key rejected by $HOST (HTTP $R_CODE). Check that anon-key belongs to this project: $(body_snippet)"
|
|
elif [[ "$R_CODE" == "000" ]]; then
|
|
fail "could not reach $HOST (DNS, TLS or timeout after ${TIMEOUT}s, $R_ATTEMPTS attempts)"
|
|
elif [[ ! "$R_CODE" =~ ^2 ]]; then
|
|
fail "REST root answered HTTP $R_CODE: $(body_snippet)"
|
|
fi
|
|
|
|
# ---------- 2. table read ----------
|
|
if [[ -n "$TABLE" && "$PAUSED" == "false" ]]; then
|
|
request "Table read" GET "/rest/v1/${TABLE}?select=${SELECT}&limit=1"
|
|
QUERY_CODE="$R_CODE"
|
|
add_row "Table read" "GET /rest/v1/${TABLE}?select=${SELECT}&limit=1" "$R_CODE" "$R_MS" "$R_ATTEMPTS"
|
|
if check_paused; then
|
|
:
|
|
elif [[ "$R_CODE" == "200" || "$R_CODE" == "206" ]]; then
|
|
log " rows returned: $(tr -d '\n' < "$BODY_FILE" | cut -c1-80)"
|
|
elif [[ "$R_CODE" == "404" ]]; then
|
|
fail "table '$TABLE' not found through the REST API (HTTP 404). Check the name and that its schema is exposed: $(body_snippet)"
|
|
elif [[ "$R_CODE" == "401" || "$R_CODE" == "403" ]]; then
|
|
fail "anon role may not read '$TABLE' (HTTP $R_CODE). Grant select to anon, or use rpc: instead: $(body_snippet)"
|
|
else
|
|
fail "table read answered HTTP $R_CODE: $(body_snippet)"
|
|
fi
|
|
fi
|
|
|
|
# ---------- 3. rpc call ----------
|
|
if [[ -n "$RPC" && "$PAUSED" == "false" ]]; then
|
|
request "RPC call" POST "/rest/v1/rpc/${RPC}" "$RPC_BODY"
|
|
QUERY_CODE="$R_CODE"
|
|
add_row "RPC call" "POST /rest/v1/rpc/${RPC}" "$R_CODE" "$R_MS" "$R_ATTEMPTS"
|
|
if check_paused; then
|
|
:
|
|
elif [[ "$R_CODE" == "200" || "$R_CODE" == "204" ]]; then
|
|
log " result: $(tr -d '\n' < "$BODY_FILE" | cut -c1-80)"
|
|
elif [[ "$R_CODE" == "404" ]]; then
|
|
fail "function '$RPC' not found through the REST API (HTTP 404). Create it and grant execute to anon: $(body_snippet)"
|
|
else
|
|
fail "rpc call answered HTTP $R_CODE: $(body_snippet)"
|
|
fi
|
|
fi
|
|
|
|
if [[ -z "$TABLE" && -z "$RPC" && "$STATUS" == "ok" ]]; then
|
|
warn "Only the REST root was pinged. Supabase pauses on low database activity, and the root endpoint may not count as a query. Set table: or rpc: so a real database read runs."
|
|
fi
|
|
|
|
if [[ "$PAUSED" == "true" ]]; then
|
|
msg="$HOST answered HTTP 540: the project is paused. Restore it from the Supabase dashboard (Project -> Restore project); this action cannot unpause a project."
|
|
if [[ "$FAIL_ON_PAUSED" == "true" ]]; then printf '::error::%s\n' "$msg"; else warn "$msg"; fi
|
|
fi
|
|
|
|
# ---------- 4. early warnings via GitHub API ----------
|
|
iso_to_epoch() {
|
|
local iso="$1"
|
|
date -u -d "$iso" +%s 2>/dev/null || date -j -u -f "%Y-%m-%dT%H:%M:%SZ" "$iso" +%s 2>/dev/null || echo ""
|
|
}
|
|
days_since() {
|
|
local epoch="$1" now
|
|
now=$(date -u +%s)
|
|
echo $(( (now - epoch) / 86400 ))
|
|
}
|
|
gh_api() {
|
|
curl -sS --max-time 15 -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" \
|
|
-H "X-GitHub-Api-Version: 2022-11-28" "$API$1" 2>/dev/null
|
|
}
|
|
|
|
if [[ -n "$TOKEN" && -n "${GITHUB_REPOSITORY:-}" ]] && command -v jq >/dev/null 2>&1; then
|
|
# 4a. previous successful run of this workflow
|
|
WF_REF="${GITHUB_WORKFLOW_REF:-}"
|
|
WF_FILE="${WF_REF%%@*}"; WF_FILE="${WF_FILE##*/}"
|
|
if [[ -n "$WF_FILE" ]]; then
|
|
runs_json=$(gh_api "/repos/$GITHUB_REPOSITORY/actions/workflows/$WF_FILE/runs?status=success&per_page=1&exclude_pull_requests=true")
|
|
last_iso=$(printf '%s' "$runs_json" | jq -r '.workflow_runs[0].run_started_at // empty' 2>/dev/null)
|
|
if [[ -n "$last_iso" ]]; then
|
|
last_epoch=$(iso_to_epoch "$last_iso")
|
|
if [[ -n "$last_epoch" ]]; then
|
|
LAST_SUCCESS_DAYS=$(days_since "$last_epoch")
|
|
log "Previous successful run: $last_iso ($LAST_SUCCESS_DAYS days ago)"
|
|
if (( LAST_SUCCESS_DAYS >= WARN_DAYS )); then
|
|
warn "Last successful keep-alive run was $LAST_SUCCESS_DAYS days ago (threshold $WARN_DAYS). Supabase pauses free projects after about 7 idle days; check the schedule and recent failures."
|
|
fi
|
|
fi
|
|
else
|
|
api_msg=$(printf '%s' "$runs_json" | jq -r '.message // empty' 2>/dev/null)
|
|
if [[ -n "$api_msg" ]]; then
|
|
notice "Could not read previous runs ($api_msg). Grant 'actions: read' to github-token to enable the last-success warning."
|
|
else
|
|
notice "No previous successful run of $WF_FILE found; this looks like the first one."
|
|
fi
|
|
fi
|
|
|
|
# 4b. cron schedule sanity: weekly or monthly leaves no margin
|
|
wf_json=$(gh_api "/repos/$GITHUB_REPOSITORY/contents/.github/workflows/$WF_FILE?ref=${GITHUB_SHA:-}")
|
|
wf_text=$(printf '%s' "$wf_json" | jq -r '.content // empty' 2>/dev/null | base64 -d 2>/dev/null || true)
|
|
if [[ -n "$wf_text" ]]; then
|
|
while IFS= read -r cron; do
|
|
[[ -z "$cron" ]] && continue
|
|
read -r _min _hour dom _mon dow <<< "$cron"
|
|
if [[ "${dom:-*}" != "*" ]]; then
|
|
warn "Schedule '$cron' runs on fixed days of the month, i.e. less than daily. Daily (e.g. '17 6 * * *') keeps a margin inside the 7-day window."
|
|
elif [[ "${dow:-*}" =~ ^[0-7]$ ]]; then
|
|
warn "Schedule '$cron' runs once a week, the same length as the 7-day inactivity window. GitHub can delay or skip scheduled runs; run daily instead."
|
|
fi
|
|
done < <(printf '%s\n' "$wf_text" | grep -E '^[[:space:]]*-?[[:space:]]*cron:' | sed -E "s/.*cron:[[:space:]]*['\"]?([^'\"#]+)['\"]?.*/\1/" | sed -E 's/[[:space:]]+$//')
|
|
fi
|
|
fi
|
|
|
|
# 4c. GitHub disables schedules in public repos after 60 days without activity
|
|
repo_json=$(gh_api "/repos/$GITHUB_REPOSITORY")
|
|
pushed_iso=$(printf '%s' "$repo_json" | jq -r '.pushed_at // empty' 2>/dev/null)
|
|
is_private=$(printf '%s' "$repo_json" | jq -r '.private // empty' 2>/dev/null)
|
|
if [[ -n "$pushed_iso" ]]; then
|
|
pushed_epoch=$(iso_to_epoch "$pushed_iso")
|
|
if [[ -n "$pushed_epoch" ]]; then
|
|
idle_days=$(days_since "$pushed_epoch")
|
|
log "Last push to $GITHUB_REPOSITORY: $pushed_iso ($idle_days days ago)"
|
|
if [[ "$is_private" != "true" ]] && (( idle_days >= GITHUB_DISABLE_WARN_AT )); then
|
|
warn "No push to this public repository for $idle_days days. GitHub disables scheduled workflows after $GITHUB_DISABLE_DAYS days without repository activity, and this keep-alive would stop silently. Push any commit to reset the clock."
|
|
fi
|
|
fi
|
|
fi
|
|
elif [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then
|
|
notice "Skipping GitHub API checks (no token, no repository context, or jq missing)."
|
|
fi
|
|
|
|
# ---------- 5. outputs + job summary ----------
|
|
set_output "status" "$STATUS"
|
|
set_output "rest-status" "$REST_CODE"
|
|
set_output "query-status" "$QUERY_CODE"
|
|
set_output "paused" "$PAUSED"
|
|
set_output "last-success-days" "$LAST_SUCCESS_DAYS"
|
|
|
|
{
|
|
case "$STATUS" in
|
|
ok) echo "## Supabase keep-alive: ok" ;;
|
|
paused) echo "## Supabase keep-alive: project is PAUSED" ;;
|
|
*) echo "## Supabase keep-alive: failed" ;;
|
|
esac
|
|
echo
|
|
echo "Project: \`$HOST\` "
|
|
echo "Time: $(date -u +"%Y-%m-%d %H:%M:%S UTC") "
|
|
if [[ -n "$LAST_SUCCESS_DAYS" ]]; then echo "Previous successful run: $LAST_SUCCESS_DAYS day(s) ago "; fi
|
|
echo
|
|
echo "| Check | Request | HTTP | Latency | Attempts |"
|
|
echo "|---|---|---|---|---|"
|
|
for r in "${ROWS[@]}"; do echo "$r"; done
|
|
if [[ -z "$TABLE" && -z "$RPC" ]]; then
|
|
echo
|
|
echo "No table or rpc configured, so no database query ran."
|
|
fi
|
|
if (( ${#WARNINGS[@]} > 0 )); then
|
|
echo
|
|
echo "### Warnings"
|
|
for w in "${WARNINGS[@]}"; do echo "- $w"; done
|
|
fi
|
|
if [[ "$STATUS" == "paused" ]]; then
|
|
echo
|
|
echo "Restore it in the Supabase dashboard: open the project and click **Restore project**. Free projects can be restored for one year after pausing."
|
|
fi
|
|
} >> "$SUMMARY"
|
|
|
|
case "$STATUS" in
|
|
ok) exit 0 ;;
|
|
paused) [[ "$FAIL_ON_PAUSED" == "true" ]] && exit 1 || exit 0 ;;
|
|
*) exit 1 ;;
|
|
esac
|