Repository navigation
320 lines (299 loc) · 12.6 KB
/
Copy pathpython-react-ci.yml
File metadata and controls
320 lines (299 loc) · 12.6 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
name: Reusable Python+React CI
# Reusable CI workflow for homelabforge Python+React repos.
# Consumers wrap this via a thin .github/workflows/ci.yml that calls:
#
# jobs:
# ci:
# uses: homelabforge/shared-workflows/.github/workflows/python-react-ci.yml@v1.7.2
# with:
# enable-translations: true
# security-tripwire-script: .github/scripts/security-tripwire.sh
# # Optional: split the long suites across runners (1-6, default 1).
# backend-test-shards: 2
# frontend-test-shards: 3
# e2e-test-shards: 2
# pg-migrations-shards: 3
#
# Inputs default to the homelab-standard values. Bun version comes from
# the consumer repo's .bun-version file (single source of truth).
#
# The backend/frontend/e2e/api-freshness matrix lives in the shared
# _python-react-tests.yml building block (called as the `tests` job
# below) so CI and publish stay in lockstep. CI-specific jobs
# (pg-migrations, docker-build-test) stay here.
on:
workflow_call:
inputs:
python-version:
type: string
default: "3.14"
bun-version-file:
type: string
default: ".bun-version"
bun-version:
# Escape hatch — leave empty to use bun-version-file.
type: string
default: ""
node-version-file:
# vitest, eslint, tsc and Playwright run on Node even under `bun run`
# (their bins are `#!/usr/bin/env node`). Without this they got the
# runner image's default Node, which drifts from the consumer's.
type: string
default: ".nvmrc"
node-version:
# Escape hatch: leave empty to use node-version-file.
type: string
default: ""
enable-e2e:
type: boolean
default: true
enable-translations:
type: boolean
default: false
enable-bootstrap-token:
type: boolean
default: false
enable-api-freshness-check:
type: boolean
default: true
backend-test-shards:
# Runners the backend pytest run is split across, 1-6. Above 1 the
# consumer's conftest needs the shard hook (README, "Test shards").
type: number
default: 1
frontend-test-shards:
# Runners vitest is split across, 1-6. Uses vitest's own --shard.
type: number
default: 1
e2e-test-shards:
# Runners Playwright is split across, 1-6. Uses Playwright's own --shard.
type: number
default: 1
enable-pg-migrations:
# Run the consumer's docker-compose.test.yml stack and execute
# pytest against its PostgreSQL sidecar. Off by default — only
# mygarage ships a compose stack today (added in v2.27.0-rc2).
# See pg-migrations-* inputs below for paths/service.
type: boolean
default: false
pg-migrations-compose-file:
type: string
default: "docker-compose.test.yml"
pg-migrations-service:
type: string
default: "mygarage-test"
pg-migrations-pytest-path:
type: string
default: "tests/migrations/"
pg-migrations-shards:
# Runners the PG pytest run is split across, 1-6, each with its own
# sidecar. Above 1 the consumer's conftest needs the shard hook.
type: number
default: 1
runner-os:
type: string
default: ubuntu-latest
security-tripwire-script:
type: string
default: ""
permissions:
contents: read
jobs:
tests:
# backend + frontend + e2e + api-freshness, shared with publish.
# Must be a full owner/repo path pinned to this release's own tag: a `./`
# local path resolves against the CONSUMER repo (which has no such file)
# when this reusable workflow is called cross-repo, causing a 0s
# startup_failure. Bump the @ref on every release.
uses: homelabforge/shared-workflows/.github/workflows/_python-react-tests.yml@v1.7.2
with:
python-version: ${{ inputs.python-version }}
bun-version-file: ${{ inputs.bun-version-file }}
bun-version: ${{ inputs.bun-version }}
node-version-file: ${{ inputs.node-version-file }}
node-version: ${{ inputs.node-version }}
enable-e2e: ${{ inputs.enable-e2e }}
enable-translations: ${{ inputs.enable-translations }}
enable-bootstrap-token: ${{ inputs.enable-bootstrap-token }}
enable-api-freshness-check: ${{ inputs.enable-api-freshness-check }}
runner-os: ${{ inputs.runner-os }}
security-tripwire-script: ${{ inputs.security-tripwire-script }}
backend-test-shards: ${{ inputs.backend-test-shards }}
frontend-test-shards: ${{ inputs.frontend-test-shards }}
e2e-test-shards: ${{ inputs.e2e-test-shards }}
pg-migrations-shard:
# Why this job exists:
# The repo's existing test-backend job runs pytest against
# SQLite. Migration 054 in mygarage v2.27.0-rc1 shipped with two
# PG-only bugs (DATETIME column type, ADD CONSTRAINT IF NOT
# EXISTS) that all CI passes ignored — `Base.metadata.create_all`
# was always run before the migrations, so by the time the
# migration's literal SQL ran the columns/constraints already
# existed and the idempotency guards short-circuited. The
# migration's actual SQL never executed against PG in any test.
#
# Consumers can opt in to a PG matrix via a docker-compose stack
# that pairs a postgres:16-alpine sidecar with a test image and
# a pre-migration SQL baseline. Migration tests load the
# baseline into a clean PG schema and exercise the new
# migration's SQL for real.
name: PostgreSQL Migration Tests (${{ matrix.shard }}/${{ strategy.job-total }})
if: inputs.enable-pg-migrations
# DELIBERATELY UNGATED, and the `needs:` that used to be here is the
# reason. This job consumes nothing the tests job produces: it builds
# backend/Dockerfile.test itself and talks to its own compose sidecar. So
# a dependency bought fail-fast and nothing else, and it charged that
# fail-fast to every GREEN run, which is the common case.
#
# MEASURED on mygarage run 33859704139 (v3.3.0 on main), job wall clocks:
# Backend 2.2m, Frontend 4.3m, E2E 3.7m, API-freshness 0.4m, this job
# 5.5m, Docker Build Test 2.1m. Gated on [tests] this job could not start
# until E2E finished at ~8m, so it ended at ~13.5m and WAS the critical
# path. Ungated it starts at 0 and ends at ~5.5m, and the critical path
# becomes the tests chain at ~8m. That is ~5 minutes off every CI run.
# v1.7.0 splits it across runners; see the CHANGELOG for the numbers.
#
# The cost is one wasted ~5m runner slot on a PR whose backend tests
# fail. Every consumer of this workflow is a public repo, where runner
# minutes are free, so the trade is one-sided. Re-gate if that changes.
#
# Note for anyone tempted to gate on just the backend job instead: you
# cannot. `needs:` in a caller addresses the called reusable workflow as
# a whole, never one job inside it, so after the suite moved into
# _python-react-tests.yml the only choices are the whole aggregate or
# nothing. (It was needs: [test-backend] before that extraction.)
runs-on: ${{ inputs.runner-os }}
# Was 15. Once mygarage moved its whole suite onto PG this job hit 14m45s
# on a green run, so 15 was about to start killing good builds.
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
shard: ${{ fromJSON('[[],[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5],[1,2,3,4,5,6]]')[inputs.pg-migrations-shards] }}
# Route consumer-supplied inputs through env so they reach the shell as
# data, never as expanded workflow expressions.
env:
PG_COMPOSE_FILE: ${{ inputs.pg-migrations-compose-file }}
PG_SERVICE: ${{ inputs.pg-migrations-service }}
PG_PYTEST_PATH: ${{ inputs.pg-migrations-pytest-path }}
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Build test image
# Plain docker build, straight into the local image store. The gha
# cache spent 32s exporting and ~25s loading to save a ~9s build
# (mygarage run 37449264165).
run: docker build -t "$PG_SERVICE:local" -f backend/Dockerfile.test backend
- name: Bring up PostgreSQL sidecar
env:
MYGARAGE_TEST_UID: "1001"
MYGARAGE_TEST_GID: "1001"
run: |
docker compose -f "$PG_COMPOSE_FILE" \
-p "$PG_SERVICE" up -d postgres-test
- name: Wait for postgres healthy
run: |
for _ in $(seq 1 30); do
health=$(docker compose -f "$PG_COMPOSE_FILE" \
-p "$PG_SERVICE" ps postgres-test \
--format '{{.Health}}' || true)
if [ "$health" = "healthy" ]; then
echo "postgres-test healthy"
exit 0
fi
sleep 2
done
echo "postgres-test did not become healthy in 60s" >&2
docker compose -f "$PG_COMPOSE_FILE" \
-p "$PG_SERVICE" logs postgres-test
exit 1
- name: Run pytest against PG sidecar
env:
MYGARAGE_TEST_UID: "1001"
MYGARAGE_TEST_GID: "1001"
PYTEST_SHARD_INDEX: ${{ matrix.shard }}
PYTEST_SHARD_COUNT: ${{ strategy.job-total }}
# Relative to pytest's rootdir. The service mounts backend/ there,
# so it lands at backend/ on the runner.
PYTEST_SHARD_REPORT: pytest-shard-report.json
run: |
# Word-splitting on $PG_PYTEST_PATH is intentional: consumers may
# pass several space-separated test paths.
# shellcheck disable=SC2086
docker compose -f "$PG_COMPOSE_FILE" \
-p "$PG_SERVICE" run --rm \
-e "PYTEST_SHARD_INDEX=$PYTEST_SHARD_INDEX" \
-e "PYTEST_SHARD_COUNT=$PYTEST_SHARD_COUNT" \
-e "PYTEST_SHARD_REPORT=$PYTEST_SHARD_REPORT" \
"$PG_SERVICE" \
pytest $PG_PYTEST_PATH -v --tb=short
- name: Upload shard report
if: ${{ !cancelled() && inputs.pg-migrations-shards > 1 }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: pytest-shard-report-pg-${{ matrix.shard }}
path: backend/pytest-shard-report.json
if-no-files-found: error
# A "re-run failed jobs" shard uploads the same name again. The
# shards that passed don't, so theirs has to last past a weekend.
overwrite: true
retention-days: 7
- name: Tear down stack
if: always()
run: |
docker compose -f "$PG_COMPOSE_FILE" \
-p "$PG_SERVICE" down -v || true
pg-migrations:
name: PostgreSQL Migration Tests
# always() for the same reason as the suite's keepers. Not a required
# check today, but the name is the one people look for.
if: always() && inputs.enable-pg-migrations
needs: [pg-migrations-shard]
runs-on: ${{ inputs.runner-os }}
timeout-minutes: 5
steps:
- name: Every PG shard passed
env:
TESTS: ${{ needs.pg-migrations-shard.result }}
run: |
echo "PostgreSQL Migration Tests shards: $TESTS"
test "$TESTS" = success
- name: Download shard reports
if: inputs.pg-migrations-shards > 1
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: pytest-shard-report-pg-*
path: shard-reports
- name: Every test module ran exactly once
if: inputs.pg-migrations-shards > 1
uses: $/.github/actions/verify-pytest-shards
with:
reports-dir: shard-reports
count: ${{ inputs.pg-migrations-shards }}
docker-build-test:
name: Docker Build Test
runs-on: ${{ inputs.runner-os }}
timeout-minutes: 20
env:
BUILDKIT_PROGRESS: plain
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Read bun version
id: bun
env:
BUN_VERSION_FILE: ${{ inputs.bun-version-file }}
run: echo "version=$(cat "$BUN_VERSION_FILE")" >> "$GITHUB_OUTPUT"
- name: Build Docker image
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
push: false
tags: ci-check:test
build-args: |
BUN_VERSION=${{ steps.bun.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false