-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompute_deployment_plan.sh
More file actions
executable file
·122 lines (112 loc) · 5.22 KB
/
Copy pathcompute_deployment_plan.sh
File metadata and controls
executable file
·122 lines (112 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
# Decide which preview components to redeploy this push, based on:
# - the PR event action (opened/reopened/synchronize)
# - per-component change flags emitted by find_last_deploys.py,
# each diffed against a per-component last-successful-deploy SHA
# - a workflow/scripts-changed flag (forces a full redeploy)
#
# Writes deploy_backend / run_migrations / deploy_frontend / any_change to GITHUB_OUTPUT
# and a human-readable summary to GITHUB_STEP_SUMMARY.
#
# All inputs come from env vars set by the caller:
# EVENT_ACTION - github.event.action
# BACKEND_BASE - SHA of last successful backend deploy (or "")
# MIGRATIONS_BASE - SHA of last successful migrations run (or "")
# BACKEND_CHANGED - "true"/"false"/"" from find_last_deploys.py (diff vs backend base)
# MIGRATIONS_CHANGED - "true"/"false"/"" from find_last_deploys.py (diff vs migrations base)
# PR_BACKEND_CHANGED - "true"/"false" whole-PR fallback (find_last_deploys.py, diff vs PR base)
# PR_MIGRATIONS_CHANGED - "true"/"false" whole-PR fallback (find_last_deploys.py, diff vs PR base)
# PR_FRONTEND_CHANGED - "true"/"false" whole-PR fallback (find_last_deploys.py, diff vs PR base)
# WORKFLOW_CHANGED - "true"/"false"/"" from find_last_deploys.py (diff vs previous push)
set -euo pipefail
: "${EVENT_ACTION:?}"
: "${GITHUB_OUTPUT:?}"
: "${GITHUB_STEP_SUMMARY:?}"
BACKEND_BASE="${BACKEND_BASE:-}"
MIGRATIONS_BASE="${MIGRATIONS_BASE:-}"
BACKEND_CHANGED="${BACKEND_CHANGED:-}"
MIGRATIONS_CHANGED="${MIGRATIONS_CHANGED:-}"
PR_BACKEND_CHANGED="${PR_BACKEND_CHANGED:-false}"
PR_MIGRATIONS_CHANGED="${PR_MIGRATIONS_CHANGED:-false}"
PR_FRONTEND_CHANGED="${PR_FRONTEND_CHANGED:-false}"
WORKFLOW_CHANGED="${WORKFLOW_CHANGED:-}"
deploy_backend=false
run_migrations=false
deploy_frontend=false
if [ "$WORKFLOW_CHANGED" = "true" ]; then
deploy_backend=true
run_migrations=true
deploy_frontend=true
else
if [ "$EVENT_ACTION" != "synchronize" ]; then
BACKEND_CHANGED="$PR_BACKEND_CHANGED"
MIGRATIONS_CHANGED="$PR_MIGRATIONS_CHANGED"
fi
# No prior successful deploy on this branch -> deploy only if this PR
# actually changed that component. This keeps frontend-only PRs from
# provisioning Supabase/Modal just to show a Vercel preview.
if { [ -z "$BACKEND_BASE" ] && [ "$PR_BACKEND_CHANGED" = "true" ]; } ||
[ "$BACKEND_CHANGED" = "true" ]; then
deploy_backend=true
fi
if { [ -z "$MIGRATIONS_BASE" ] && [ "$PR_MIGRATIONS_CHANGED" = "true" ]; } ||
[ "$MIGRATIONS_CHANGED" = "true" ]; then
run_migrations=true
fi
if [ "$PR_FRONTEND_CHANGED" = "true" ] ||
[ "$deploy_backend" = "true" ] ||
[ "$run_migrations" = "true" ]; then
deploy_frontend=true
fi
fi
# A migrated schema must be boot-tested by the preview backend, and the branch
# DB password rotated by wait_for_supabase_branch.sh must be re-published to
# the Modal secret -- so any migration run also redeploys the backend.
if [ "$run_migrations" = "true" ]; then
deploy_backend=true
fi
any_change=false
if [ "$deploy_backend" = "true" ] ||
[ "$run_migrations" = "true" ] ||
[ "$deploy_frontend" = "true" ]; then
any_change=true
fi
# A preview backend is live after this run iff we deploy it now or a prior run
# already deployed one (BACKEND_BASE is that run's sha). The Vercel job uses this
# to keep NEXT_PUBLIC_API_URL pointed at the preview backend on frontend-only
# follow-up pushes -- otherwise a blank MODAL_API_URL falls back to prod and the
# preview frontend starts querying production. Stays false for frontend-only PRs
# that never provisioned a backend, so those correctly fall back to prod.
preview_backend_live=false
if [ "$deploy_backend" = "true" ] || [ -n "$BACKEND_BASE" ]; then
preview_backend_live=true
fi
# The generation identifies the preview state this run reconciles toward; it is
# the ownership token later fencing compares against (see
# record_preview_metrics.py, the canonical definition).
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
preview_generation="$(PR_NUMBER="${PR_NUMBER:?}" HEAD_SHA="${HEAD_SHA:?}" \
python "$script_dir/record_preview_metrics.py" generation)"
{
echo "deploy_backend=$deploy_backend"
echo "run_migrations=$run_migrations"
echo "deploy_frontend=$deploy_frontend"
echo "any_change=$any_change"
echo "preview_backend_live=$preview_backend_live"
echo "preview_generation=$preview_generation"
} >> "$GITHUB_OUTPUT"
{
echo "## Preview deployment plan"
echo
echo "- Event action: \`$EVENT_ACTION\`"
echo "- Preview generation: \`$preview_generation\`"
echo "- Last successful backend deploy: \`${BACKEND_BASE:-(none)}\`"
echo "- Last successful migration run: \`${MIGRATIONS_BASE:-(none)}\`"
echo "- Backend code changed since: \`${BACKEND_CHANGED:-n/a}\`"
echo "- Migrations changed since: \`${MIGRATIONS_CHANGED:-n/a}\`"
echo "- PR backend changes: \`$PR_BACKEND_CHANGED\`"
echo "- PR migration changes: \`$PR_MIGRATIONS_CHANGED\`"
echo "- PR frontend changes: \`$PR_FRONTEND_CHANGED\`"
echo "- Workflow/scripts changed since previous push: \`${WORKFLOW_CHANGED:-n/a}\`"
echo "- Plan: deploy_backend=\`$deploy_backend\` run_migrations=\`$run_migrations\` deploy_frontend=\`$deploy_frontend\` any_change=\`$any_change\`"
} >> "$GITHUB_STEP_SUMMARY"