-
Notifications
You must be signed in to change notification settings - Fork 0
357 lines (320 loc) · 15.4 KB
/
Copy pathsupabase-migrations.yml
File metadata and controls
357 lines (320 loc) · 15.4 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Apply pending SQL migrations from supabase/migrations/ to the linked Supabase project.
#
# development: Supabase Cloud project, linked via SUPABASE_ACCESS_TOKEN + project-ref.
# production: self-hosted Supabase, reached over a Tailscale connection and pushed to
# directly via a Postgres connection string (no cloud "link" concept exists
# for self-hosted, so we skip `supabase link` entirely for prod).
#
# Requires environment secrets configured in GitHub:
# - development environment: SUPABASE_ACCESS_TOKEN, SUPABASE_DB_PASSWORD, SUPABASE_PROJECT_ID
# - production environment: TS_OAUTH_CLIENT_ID, TS_OAUTH_SECRET, PROD_DB_URL
#
# Intended workflow (default branch = `dev`):
# - PR → `dev`: comment `/infra-ci` (exact) to dry-run against Supabase Cloud using the
# `development` environment. issue_comment runs on the default branch, so the environment's
# "Selected branches" policy for `dev` just works — no `refs/pull/*/merge` exception needed.
# - Merge → `dev` (push): deploy-dev applies migrations to Supabase Cloud.
# - PR → `main`: automatic notice only (schema was already validated/pushed on the way into
# `dev`; local types verification is in supabase-db-types-pr.yml).
# - Merge → `main` (push): deploy-prod applies migrations to self-hosted Postgres via Tailscale.
# - workflow_dispatch: manual deploy (branch determines environment).
#
# Commit status context for branch protection: `infra / supabase-dry-run`
# (posted on the PR head SHA for `/infra-ci` runs).
#
# Concurrency: one run at a time per branch/ref so overlapping supabase db push jobs do not
# contend on remote migration history.
name: Supabase migrations
on:
push:
branches:
- dev
- main
paths:
- 'supabase/migrations/**'
- '.github/workflows/supabase-migrations.yml'
pull_request:
branches:
- main
paths:
- 'supabase/migrations/**'
- '.github/workflows/supabase-migrations.yml'
issue_comment:
types: [created]
workflow_dispatch:
# Pin CLI for reproducible db push / dry-run; bump when you intentionally adopt a new CLI.
env:
SUPABASE_CLI_VERSION: 2.84.4
# Commit status context — keep stable; branch protection may require this name.
INFRA_STATUS_CONTEXT: infra / supabase-dry-run
permissions:
contents: read
issues: write
pull-requests: read
statuses: write
concurrency:
group: supabase-migrations-${{ github.workflow }}-${{ github.ref }}-${{ github.event.issue.number || github.event.pull_request.number || github.run_id }}
cancel-in-progress: false
jobs:
# Automatic lightweight gate for PRs into main (no secrets).
notice-main-pr:
if: |
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- name: Note migrations will deploy to production on merge
run: |
echo '::notice title=Supabase migrations::This PR targets main. Cloud dry-run already happened on the way into dev. Merging will run deploy-prod (self-hosted via Tailscale).'
# Comment-triggered dry-run against Supabase Cloud (PR → dev).
dry-run:
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request != null &&
github.event.comment.body == '/infra-ci'
runs-on: ubuntu-latest
environment: development
env:
SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DB_PASSWORD }}
SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
steps:
- name: Verify commenter has write access
uses: actions/github-script@v8
with:
script: |
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.actor,
});
const perms = data.user?.permissions;
const hasWriteAccess =
perms?.admin === true ||
perms?.maintain === true ||
perms?.push === true ||
['admin', 'write'].includes(data.permission);
if (!hasWriteAccess) {
core.setFailed(`@${context.actor} does not have permission to trigger /infra-ci.`);
throw new Error('Permission denied');
}
- name: React to comment
uses: actions/github-script@v8
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket',
});
- name: Resolve PR and path changes
id: pr
uses: actions/github-script@v8
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
if (!pr.head.repo) {
core.setFailed('PR head repository is unavailable.');
throw new Error('PR head repo is null');
}
if (pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
core.setFailed('Fork PRs cannot use /infra-ci (development secrets are not available).');
throw new Error('Fork PR');
}
core.setOutput('sha', pr.head.sha);
core.setOutput('repo', pr.head.repo.full_name);
core.setOutput('base', pr.base.ref);
core.setOutput('base_sha', pr.base.sha);
const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
owner: context.repo.owner,
repo: context.repo.repo,
basehead: `${pr.base.sha}...${pr.head.sha}`,
});
const paths = [
'supabase/migrations/',
'.github/workflows/supabase-migrations.yml',
];
const changed = (comparison.files || []).some((f) =>
paths.some((p) => f.filename === p || f.filename.startsWith(p))
);
core.setOutput('changed', changed ? 'true' : 'false');
- name: Set commit status (pending)
uses: actions/github-script@v8
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: '${{ steps.pr.outputs.sha }}',
state: 'pending',
context: process.env.INFRA_STATUS_CONTEXT,
description: 'Supabase dry-run starting…',
target_url: `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
});
- name: Skip when PR does not target dev
id: gate
run: |
set -euo pipefail
if [ "${{ steps.pr.outputs.base }}" != "dev" ]; then
echo "::notice title=Supabase dry-run skipped::/infra-ci cloud dry-run only runs on PRs targeting dev (base was '${{ steps.pr.outputs.base }}')."
echo "run=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "${{ steps.pr.outputs.changed }}" != "true" ]; then
echo "::notice title=Supabase dry-run skipped::No migration / workflow path changes in this PR."
echo "run=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "run=true" >> "$GITHUB_OUTPUT"
- uses: actions/checkout@v6
if: steps.gate.outputs.run == 'true'
with:
repository: ${{ steps.pr.outputs.repo }}
ref: ${{ steps.pr.outputs.sha }}
- uses: supabase/setup-cli@v2
if: steps.gate.outputs.run == 'true'
with:
version: ${{ env.SUPABASE_CLI_VERSION }}
- name: Link project (non-interactive)
if: steps.gate.outputs.run == 'true'
run: supabase link --project-ref "$SUPABASE_PROJECT_ID"
- name: Dry-run migrations
if: steps.gate.outputs.run == 'true'
run: supabase db push --dry-run --yes
- name: Set commit status (final)
if: always() && steps.pr.outputs.sha != ''
uses: actions/github-script@v8
with:
script: |
const skipped = '${{ steps.gate.outputs.run }}' !== 'true';
const jobOk = '${{ job.status }}' === 'success';
const state = jobOk ? 'success' : 'failure';
const description = skipped
? 'Skipped (not targeting dev, or no migration changes)'
: state === 'success'
? 'Supabase dry-run passed'
: 'Supabase dry-run failed';
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: '${{ steps.pr.outputs.sha }}',
state,
context: process.env.INFRA_STATUS_CONTEXT,
description,
target_url: `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
});
# --- Development: Supabase Cloud, linked via access token ---
deploy-dev:
if: |
(github.event_name == 'push' || github.event_name == 'workflow_dispatch') &&
github.ref == 'refs/heads/dev'
environment: development
runs-on: ubuntu-latest
env:
SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DB_PASSWORD }}
SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
with:
version: 10.33.0
run_install: false
- uses: actions/setup-node@v6
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- uses: supabase/setup-cli@v2
with:
version: ${{ env.SUPABASE_CLI_VERSION }}
- name: Link project (non-interactive)
run: supabase link --project-ref "$SUPABASE_PROJECT_ID"
- name: Push migrations to Supabase
run: supabase db push --yes
- name: Verify Database types match linked project (no auto-commit)
run: |
set -euo pipefail
TYPES_PRETTIER="$GITHUB_WORKSPACE/prettier.database-types.json"
awk '/^export type Json/{flag=1} flag' packages/supabase/src/lib/database.types.ts > /tmp/db-types.committed.ts
supabase gen types typescript --linked --schema public > /tmp/db-types.generated.ts
pnpm exec prettier --config "$TYPES_PRETTIER" --write /tmp/db-types.committed.ts /tmp/db-types.generated.ts
# Cloud vs local/self-hosted gen types may disagree on this metadata block; strip it
# before comparing (same as supabase-db-types-pr.yml).
perl -0pi -e 's/^ \/\/ Allows to automatically instantiate createClient with right options\n \/\/ instead of createClient<Database, \{ PostgrestVersion: '\''XX'\'' \}>\(URL, KEY\)\n __InternalSupabase: \{\n PostgrestVersion: "14\.1"\n \}\n//ms' /tmp/db-types.committed.ts /tmp/db-types.generated.ts
perl -0pi -e 's/\n+\z/\n/' /tmp/db-types.committed.ts /tmp/db-types.generated.ts
if diff -q /tmp/db-types.committed.ts /tmp/db-types.generated.ts >/dev/null; then
echo "Database types match linked project."
exit 0
fi
echo "::error::packages/supabase/src/lib/database.types.ts does not match the linked Supabase project after db push."
echo "Regenerate: pnpm dlx supabase gen types typescript --linked --schema public > packages/supabase/src/lib/database.types.ts"
echo "Then: pnpm exec prettier --write packages/supabase/src/lib/database.types.ts"
diff -u /tmp/db-types.committed.ts /tmp/db-types.generated.ts || true
exit 1
# --- Production: self-hosted Supabase, reached over Tailscale, pushed via db-url ---
deploy-prod:
if: |
(github.event_name == 'push' || github.event_name == 'workflow_dispatch') &&
github.ref == 'refs/heads/main'
environment: production
runs-on: ubuntu-latest
env:
PROD_DB_URL: ${{ secrets.PROD_DB_URL }}
# Works around a known Supabase CLI bug where ?sslmode=disable in the db-url
# is silently ignored unless --debug is passed (supabase/cli#4839). Setting
# PGSSLMODE directly avoids the bug without needing --debug's verbose logging.
# Safe here since the connection already runs inside the Tailscale tunnel.
PGSSLMODE: disable
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
with:
version: 10.33.0
run_install: false
- uses: actions/setup-node@v6
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- uses: supabase/setup-cli@v2
with:
version: ${{ env.SUPABASE_CLI_VERSION }}
# Joins the self-hosted DB's tailnet so the runner can reach it privately.
# Ephemeral OAuth-client auth means the CI node cleans itself up after the job.
- name: Connect to Tailscale
uses: tailscale/github-action@v3
with:
oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
tags: tag:ci
# No `supabase link` here: self-hosted has no cloud project-ref to link against.
# `db push` tracks migration history in the target database itself, so pushing
# straight at a connection string works the same way linking would.
- name: Push migrations to Supabase
run: supabase db push --db-url "$PROD_DB_URL" --yes
- name: Verify Database types match production database (no auto-commit)
run: |
set -euo pipefail
TYPES_PRETTIER="$GITHUB_WORKSPACE/prettier.database-types.json"
awk '/^export type Json/{flag=1} flag' packages/supabase/src/lib/database.types.ts > /tmp/db-types.committed.ts
PGSSLMODE=disable supabase gen types typescript --db-url "$PROD_DB_URL" --schema public > /tmp/db-types.generated.ts
pnpm exec prettier --config "$TYPES_PRETTIER" --write /tmp/db-types.committed.ts /tmp/db-types.generated.ts
# Self-hosted gen types omits __InternalSupabase; committed file (from Cloud) often has it.
# Strip before comparing — same as supabase-db-types-pr.yml / deploy-dev.
perl -0pi -e 's/^ \/\/ Allows to automatically instantiate createClient with right options\n \/\/ instead of createClient<Database, \{ PostgrestVersion: '\''XX'\'' \}>\(URL, KEY\)\n __InternalSupabase: \{\n PostgrestVersion: "14\.1"\n \}\n//ms' /tmp/db-types.committed.ts /tmp/db-types.generated.ts
perl -0pi -e 's/\n+\z/\n/' /tmp/db-types.committed.ts /tmp/db-types.generated.ts
if diff -q /tmp/db-types.committed.ts /tmp/db-types.generated.ts >/dev/null; then
echo "Database types match production database."
exit 0
fi
echo "::error::packages/supabase/src/lib/database.types.ts does not match the production database after db push."
echo "Regenerate: pnpm dlx supabase gen types typescript --db-url <PROD_DB_URL> --schema public > packages/supabase/src/lib/database.types.ts"
echo "Then: pnpm exec prettier --write packages/supabase/src/lib/database.types.ts"
diff -u /tmp/db-types.committed.ts /tmp/db-types.generated.ts || true
exit 1