-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (122 loc) · 5 KB
/
Copy pathmigrations-preview.yml
File metadata and controls
148 lines (122 loc) · 5 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
name: Migrations Preview
on:
workflow_run:
workflows:
- Deploy Preview
types:
- completed
workflow_dispatch:
permissions: {}
jobs:
migrations-preview:
name: Migrations Preview
runs-on: ubuntu-latest
environment: preview
if: github.event_name == 'workflow_dispatch' ||
github.event.workflow_run.conclusion == 'success'
permissions:
contents: read
steps:
- name: Checkout repository (trusted base)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
- name: Determine deployed SHA
id: context
run: |
set -euo pipefail
if [ "${GITHUB_EVENT_NAME}" = 'workflow_dispatch' ]; then
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
echo 'trigger_event=workflow_dispatch' >> "$GITHUB_OUTPUT"
echo 'head_branch=main' >> "$GITHUB_OUTPUT"
exit 0
fi
echo "sha=${{ github.event.workflow_run.head_sha }}" >> "$GITHUB_OUTPUT"
echo "trigger_event=${{ github.event.workflow_run.event }}" >> "$GITHUB_OUTPUT"
echo "head_branch=${{ github.event.workflow_run.head_branch }}" >> "$GITHUB_OUTPUT"
echo "head_repo_full_name=${{ github.event.workflow_run.head_repository.full_name }}" >> "$GITHUB_OUTPUT"
echo "is_fork=${{ github.event.workflow_run.head_repository.fork }}" >> "$GITHUB_OUTPUT"
shell: bash
- name: Gate migrations (skip forks/hotfix/unexpected triggers)
id: gate
run: |
set -euo pipefail
# For workflow_dispatch we allow manual runs (environment protections still apply).
if [ "${GITHUB_EVENT_NAME}" = 'workflow_dispatch' ]; then
echo 'should_migrate=true' >> "$GITHUB_OUTPUT"
exit 0
fi
# This workflow is intended to run after a successful preview deployment.
if [ '${{ steps.context.outputs.trigger_event }}' != 'pull_request' ]; then
echo 'should_migrate=false' >> "$GITHUB_OUTPUT"
exit 0
fi
# Skip fork PRs (Deploy Preview is skipped for forks, but the workflow_run
# event can still conclude "success" when jobs are skipped).
if [ '${{ steps.context.outputs.is_fork }}' = 'true' ]; then
echo 'should_migrate=false' >> "$GITHUB_OUTPUT"
exit 0
fi
if [ '${{ steps.context.outputs.head_repo_full_name }}' != '${{ github.repository }}' ]; then
echo 'should_migrate=false' >> "$GITHUB_OUTPUT"
exit 0
fi
case '${{ steps.context.outputs.head_branch }}' in
hotfix/*)
echo 'should_migrate=false' >> "$GITHUB_OUTPUT"
exit 0
;;
esac
echo 'should_migrate=true' >> "$GITHUB_OUTPUT"
- name: Checkout deployed SHA (validated)
# CodeQL can flag privileged workflows which checkout untrusted refs via actions/checkout.
# We checkout main first (trusted) to load local actions/tooling, then fetch+detach the
# deployed SHA. This SHA comes from the upstream Deploy workflow context.
if: steps.gate.outputs.should_migrate == 'true'
run: |
set -euo pipefail
sha='${{ steps.context.outputs.sha }}'
git fetch origin "$sha" --depth=1
git checkout --detach "$sha"
- name: Setup Node.js
if: steps.gate.outputs.should_migrate == 'true'
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ">=24.16.0 <25"
cache: "npm"
- name: Install dependencies
if: steps.gate.outputs.should_migrate == 'true'
run: npm ci
- name: Verify database schema is up to date
id: verify
if: steps.gate.outputs.should_migrate == 'true'
env:
ASTRO_DB_REMOTE_URL: ${{ secrets.ASTRO_DB_REMOTE_URL }}
ASTRO_DB_APP_TOKEN: ${{ secrets.ASTRO_DB_APP_TOKEN }}
run: |
set -uo pipefail
set +e
output=$(npx astro db verify 2>&1)
exit_code=$?
set -e
printf '%s\n' "$output"
if [ "$exit_code" -ne 0 ]; then
exit "$exit_code"
fi
if printf '%s' "$output" | grep -Fq 'Database schema is up to date.'; then
echo 'needs_push=false' >> "$GITHUB_OUTPUT"
exit 0
fi
if printf '%s' "$output" | grep -Fq 'Database schema is out of date.'; then
echo 'needs_push=true' >> "$GITHUB_OUTPUT"
exit 0
fi
echo 'Unexpected output from astro db verify' >&2
exit 1
- name: Push database migrations (preview)
if: steps.gate.outputs.should_migrate == 'true' &&
steps.verify.outputs.needs_push == 'true'
env:
ASTRO_DB_REMOTE_URL: ${{ secrets.ASTRO_DB_REMOTE_URL }}
ASTRO_DB_APP_TOKEN: ${{ secrets.ASTRO_DB_APP_TOKEN }}
run: npx astro db push --remote