|
| 1 | +#!/bin/bash |
| 2 | +# scripts/ci-prestart-supabase.sh |
| 3 | +# |
| 4 | +# Pre-starts Supabase for affected packages in CI. |
| 5 | +# First instance pulls Docker images, rest start in parallel. |
| 6 | +# |
| 7 | +# Usage: ./scripts/ci-prestart-supabase.sh <package1> [package2] ... |
| 8 | +# Example: ./scripts/ci-prestart-supabase.sh core client |
| 9 | +# Example: ./scripts/ci-prestart-supabase.sh edge-worker |
| 10 | +# |
| 11 | +# Env vars: |
| 12 | +# NX_BASE - Base ref for affected check (default: origin/main) |
| 13 | +# NX_HEAD - Head ref for affected check (default: HEAD) |
| 14 | +# |
| 15 | +# Optimization: Supabase start has two slow phases: |
| 16 | +# 1. Docker image pull (~slow first time, then cached) |
| 17 | +# 2. Container startup (~28s even with cached images) |
| 18 | +# This script starts the first instance (pulls images), then starts |
| 19 | +# the rest in parallel to save ~1 minute on CI runs. |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +if [ $# -eq 0 ]; then |
| 24 | + echo "Usage: $0 <package1> [package2] ..." |
| 25 | + echo "Example: $0 core client" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 30 | +BASE=${NX_BASE:-origin/main} |
| 31 | +HEAD=${NX_HEAD:-HEAD} |
| 32 | + |
| 33 | +# Map packages to their directories |
| 34 | +declare -A PKG_DIRS=( |
| 35 | + ["core"]="pkgs/core" |
| 36 | + ["client"]="pkgs/client" |
| 37 | + ["edge-worker"]="pkgs/edge-worker" |
| 38 | + ["cli"]="pkgs/cli" |
| 39 | +) |
| 40 | + |
| 41 | +# Get all affected projects with supabase:ci-marker target |
| 42 | +AFFECTED=$(pnpm nx show projects --affected -t supabase:ci-marker --base="$BASE" --head="$HEAD" 2>/dev/null || echo "") |
| 43 | + |
| 44 | +# Filter to only packages specified as arguments |
| 45 | +DIRS=() |
| 46 | +for pkg in "$@"; do |
| 47 | + if [ -z "${PKG_DIRS[$pkg]:-}" ]; then |
| 48 | + echo "Warning: Unknown package '$pkg', skipping" |
| 49 | + continue |
| 50 | + fi |
| 51 | + if echo "$AFFECTED" | grep -q "^${pkg}$"; then |
| 52 | + DIRS+=("${PKG_DIRS[$pkg]}") |
| 53 | + echo "Package '$pkg' is affected" |
| 54 | + else |
| 55 | + echo "Package '$pkg' is not affected, skipping" |
| 56 | + fi |
| 57 | +done |
| 58 | + |
| 59 | +if [ ${#DIRS[@]} -eq 0 ]; then |
| 60 | + echo "No affected packages need Supabase" |
| 61 | + exit 0 |
| 62 | +fi |
| 63 | + |
| 64 | +echo "Starting Supabase for: ${DIRS[*]}" |
| 65 | + |
| 66 | +LOGDIR=$(mktemp -d) |
| 67 | +trap "rm -rf $LOGDIR" EXIT |
| 68 | + |
| 69 | +# First one pulls Docker images (must complete before others) |
| 70 | +echo "::group::Starting ${DIRS[0]} (pulling Docker images)" |
| 71 | +if ! "$SCRIPT_DIR/supabase-start-locked.sh" "${DIRS[0]}" 2>&1 | tee "$LOGDIR/first.log"; then |
| 72 | + echo "::endgroup::" |
| 73 | + echo "::error::Failed to start ${DIRS[0]}" |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | +echo "::endgroup::" |
| 77 | + |
| 78 | +# Rest in parallel (images cached, ~28s each but concurrent) |
| 79 | +if [ ${#DIRS[@]} -gt 1 ]; then |
| 80 | + REMAINING=$((${#DIRS[@]} - 1)) |
| 81 | + echo "Starting $REMAINING more instance(s) in parallel..." |
| 82 | + |
| 83 | + PIDS=() |
| 84 | + for dir in "${DIRS[@]:1}"; do |
| 85 | + name=$(basename "$dir") |
| 86 | + "$SCRIPT_DIR/supabase-start-locked.sh" "$dir" > "$LOGDIR/$name.log" 2>&1 & |
| 87 | + PIDS+=("$!:$dir:$name") |
| 88 | + done |
| 89 | + |
| 90 | + FAILED=0 |
| 91 | + for entry in "${PIDS[@]}"; do |
| 92 | + pid=${entry%%:*} |
| 93 | + rest=${entry#*:} |
| 94 | + dir=${rest%%:*} |
| 95 | + name=${rest#*:} |
| 96 | + if ! wait "$pid"; then |
| 97 | + echo "::error::Failed to start $dir" |
| 98 | + echo "::group::$name log" |
| 99 | + cat "$LOGDIR/$name.log" |
| 100 | + echo "::endgroup::" |
| 101 | + FAILED=1 |
| 102 | + else |
| 103 | + echo ":: $dir started" |
| 104 | + fi |
| 105 | + done |
| 106 | + |
| 107 | + if [ $FAILED -eq 1 ]; then |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +fi |
| 111 | + |
| 112 | +echo "All Supabase instances started" |
0 commit comments