Skip to content

Commit 23b15a2

Browse files
committed
fix(ci): only migrate an environment from its own branch
1 parent ea86f85 commit 23b15a2

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎.github/workflows/migrate.yml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ jobs:
4848
cancel-in-progress: false
4949

5050
steps:
51+
# An environment only migrates from its own branch, so a manual dispatch
52+
# from a feature branch cannot apply that branch's migrations to a shared
53+
# database. Under workflow_call github.ref is the caller's ref (ci.yml's
54+
# push to main/staging/dev). This fails rather than skips: ci.yml's
55+
# promote gates on migrate succeeding, and a skipped run must never read
56+
# as a migration that happened. It runs before any secret is read.
57+
- name: Require the environment's own branch
58+
env:
59+
ENVIRONMENT: ${{ inputs.environment }}
60+
REF: ${{ github.ref }}
61+
run: |
62+
case "${ENVIRONMENT}" in
63+
production) expected=refs/heads/main ;;
64+
staging) expected=refs/heads/staging ;;
65+
dev) expected=refs/heads/dev ;;
66+
*)
67+
echo "::error::Unknown migrate environment '${ENVIRONMENT}'"
68+
exit 1
69+
;;
70+
esac
71+
if [ "${REF}" != "${expected}" ]; then
72+
echo "::error::Refusing to migrate ${ENVIRONMENT} from ${REF}; ${ENVIRONMENT} migrates only from ${expected}"
73+
exit 1
74+
fi
75+
5176
- name: Checkout code
5277
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
5378
with:

‎scripts/migrations-workflow.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { readFileSync } from 'node:fs'
33
import { describe, expect, it } from 'vitest'
44

55
const workflow = readFileSync(new URL('../.github/workflows/migrate.yml', import.meta.url), 'utf8')
6-
const step = workflow.match(/^ {8}run: \|\r?\n((?: {10}.*(?:\r?\n|$)|\r?\n)+)/m)?.[1]
6+
const step = workflow.match(
7+
/^ {6}- name: Apply database schema changes\r?\n(?: {8}.*\r?\n)*? {8}run: \|\r?\n((?: {10}.*(?:\r?\n|$)|\r?\n)+)/m
8+
)?.[1]
79
if (!step) throw new Error('Migration workflow must contain its schema application shell step')
810
const script = step.replace(/^ {10}/gm, '')
911

0 commit comments

Comments
 (0)