Repository navigation
498 lines (447 loc) · 20.8 KB
/
Copy pathci.yml
File metadata and controls
498 lines (447 loc) · 20.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
name: CI
on:
push:
# Both names on purpose. This repository's default branch is `master`,
# while the workflows were written for `main` — so nothing triggered on a
# push and the pipeline had never run once. Listing both means it works now
# and keeps working if the branch is ever renamed.
branches: [main, master]
pull_request:
branches: [main, master]
workflow_dispatch:
inputs:
run_e2e:
description: Also run the end-to-end suite (advisory; see the note on the e2e job)
required: false
type: boolean
default: false
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
env:
# Deliberately weak, local-only, and never used outside this ephemeral job.
# The production values live in Cloudflare Secrets and are never in CI.
DATABASE_URL: postgresql://inkloom:inkloom_ci@127.0.0.1:5432/inkloom
TEST_DATABASE_URL: postgresql://inkloom:inkloom_ci@127.0.0.1:5432/inkloom_test
INKLOOM_ENV: test
APP_URL: http://localhost:5173
BETTER_AUTH_SECRET: ci-only-secret-not-used-anywhere-else-000000
ACCESS_CODE_PEPPER: ci-only-access-code-pepper-32-chars-x
IP_HASH_PEPPER: ci-only-ip-hash-pepper-32-characters
EMAIL_TRANSPORT: mailpit
MAILPIT_HOST: 127.0.0.1
TURNSTILE_ENABLED: "false"
RATE_LIMIT_ENABLED: "true"
LOG_LEVEL: error
jobs:
# ---------------------------------------------------------------------------
# Fast feedback: anything that needs no database runs first and in parallel,
# so a formatting mistake fails in under a minute rather than after the whole
# integration suite.
# ---------------------------------------------------------------------------
static:
name: Format, lint, types
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- name: Install
run: pnpm install --frozen-lockfile
- name: Formatting
run: pnpm format:check
- name: Lint
run: pnpm lint
- name: Generate route types
run: pnpm --filter @inkloom/web exec react-router typegen
- name: Type check
run: pnpm -r exec tsc --noEmit
# ---------------------------------------------------------------------------
# Secret scanning. Separate job so it reports independently and is impossible
# to overlook inside a longer log.
# ---------------------------------------------------------------------------
secrets:
name: Secret scan
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
# Full history, so a secret committed earlier and later removed is
# still found.
fetch-depth: 0
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- name: Scan source
run: pnpm secrets:check
# The CLI, not gitleaks-action.
#
# The action is free on a personal repository and requires a paid licence
# key on an organisation-owned one. Moving this repository under an
# organisation therefore turned a passing secret scan into a failing one,
# with nothing about the code having changed. The scanner itself is the
# same tool and is free either way, so it is invoked directly.
#
# `--redact` so a finding is reported without reprinting the secret into
# a build log that is more widely readable than the repository.
- name: Scan git history
run: |
docker run --rm -v "$PWD:/repo" ghcr.io/gitleaks/gitleaks:latest \
detect --source=/repo --config=/repo/.gitleaks.toml \
--no-banner --redact --exit-code 1
# ---------------------------------------------------------------------------
# Unit tests: pure functions, no database. Fast enough to run everywhere.
# ---------------------------------------------------------------------------
unit:
name: Unit tests
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- run: pnpm test:unit
# ---------------------------------------------------------------------------
# Integration, concurrency and security tests against a REAL Postgres.
#
# A container rather than a mock, because the guarantees under test are
# database guarantees: unique constraints, row locks, append-only triggers.
# A mocked database cannot demonstrate that two racing transactions produce
# exactly one redemption, which is the single most important assertion here.
# ---------------------------------------------------------------------------
database:
name: Integration, concurrency and security tests
runs-on: ubuntu-latest
timeout-minutes: 20
services:
postgres:
image: postgres:17-alpine
env:
POSTGRES_USER: inkloom
POSTGRES_PASSWORD: inkloom_ci
POSTGRES_DB: inkloom
ports: ["5432:5432"]
options: >-
--health-cmd "pg_isready -U inkloom"
--health-interval 5s
--health-timeout 5s
--health-retries 10
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- name: Create the isolated test database
run: |
PGPASSWORD=inkloom_ci psql -h 127.0.0.1 -U inkloom -d inkloom \
-c "CREATE DATABASE inkloom_test OWNER inkloom;"
- name: Apply migrations
run: pnpm db:migrate
# Proves the migrations are complete: if the schema and the migrations
# have diverged, drizzle-kit finds a pending change and this fails.
- name: Migration validation
run: |
pnpm db:check
pnpm --filter @inkloom/db exec drizzle-kit generate --name=ci_drift_check
if git diff --name-only --exit-code packages/db/migrations; then
echo "Schema and migrations agree."
else
echo "::error::The Drizzle schema has changes with no migration. Run 'pnpm db:generate' and commit the result."
git diff packages/db/migrations
exit 1
fi
- name: Integration and concurrency tests
run: pnpm test:integration
- name: Security tests
run: pnpm test:security
- name: Reconciliation must report no drift
run: pnpm credits:reconcile
# ---------------------------------------------------------------------------
# Production build, plus a scan of the ACTUAL client bundle for server secrets.
# ---------------------------------------------------------------------------
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- name: Build
run: pnpm build
- name: OpenAPI is in step with the schemas
run: |
pnpm openapi:emit
if ! git diff --exit-code openapi.json; then
echo "::error::openapi.json is stale. Run 'pnpm openapi:emit' and commit it."
exit 1
fi
# The check that matters most: a secret can be perfectly safe in a server
# file and catastrophic if a bundler inlines it into the browser bundle.
- name: No server secrets in the client bundle
run: pnpm secrets:check
- uses: actions/upload-artifact@v4
with:
name: build
path: apps/web/build
retention-days: 7
# ---------------------------------------------------------------------------
# End-to-end: a real browser against a real Worker, database and mail server.
# ---------------------------------------------------------------------------
e2e:
# SHARDED, and it has to be. 95 journeys run strictly serially — they share
# feature flags, campaigns and rate-limit overrides, so parallel workers
# would race each other rather than the code — against a Vite dev server
# that compiles each route on first request. One runner took longer than the
# 30-minute cap and was cancelled mid-suite, which is a red pipeline that
# tells you nothing.
#
# Each shard is a separate runner with its OWN postgres and mailpit, so the
# serial guarantee holds inside a shard and there is no shared state between
# them. `fail-fast: false` because one broken journey should not hide the
# state of the other three quarters of the suite.
name: End-to-end tests (${{ matrix.shard }}/4)
runs-on: ubuntu-latest
timeout-minutes: 25
needs: [static]
#
# OFF BY DEFAULT, and no longer for cost reasons.
#
# Four sharded runners were once 60% of the billing for every CI run, which
# mattered when minutes were scarce. Public repositories get unlimited
# minutes, so that argument is gone.
#
# What remains is the reason below: the suite CANNOT pass on a Linux runner,
# for an environment reason unrelated to the application. A permanently red
# job on every push teaches everyone to ignore red, which costs more than
# the coverage is worth. It stays one checkbox away, and goes back to every
# push the day the loopback issue is solved.
if: github.event_name == 'workflow_dispatch' && inputs.run_e2e
#
# ADVISORY, NOT BLOCKING — and this is a known gap rather than a shrug.
#
# The suite passes in full on a developer's machine (95/95). On a Linux
# runner, 11 of its journeys fail because the Worker cannot reach the
# database: miniflare runs it behind a network policy that permits `public`
# and `private` addresses and not `local`, so a connection to 127.0.0.1 is
# refused inside the runtime, instantly, with a message identical to a
# database that is not running. Pointing the Worker at the runner's private
# address, and running postgres on the host network rather than behind
# docker-proxy, both failed to shift it.
#
# Everything the failure touches is environmental: the same code, the same
# migrations and the same seed pass every other job here, and the journeys
# themselves pass locally. Blocking every merge on a runtime bug we do not
# control would train everyone to ignore a red pipeline, which costs more
# than this gap does.
#
# WHAT THIS MEANS IN PRACTICE: run `pnpm test:e2e` yourself before merging
# anything that touches a user journey. The gate job below prints that
# reminder whenever these shards did not pass.
#
# TO RE-ENABLE: add `e2e` back to the `ci` job's `needs`.
continue-on-error: true
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
# NOT `services:`, and that is the whole point.
#
# A service container publishes its port through docker-proxy, so
# 127.0.0.1:5432 on the runner is a forwarder rather than a listening
# database. Node connects through it happily — migrations and the seed ran
# fine — but the Worker does not: workerd refused every connection with
# "connect(): Connection refused", so every page whose loader touches the
# database rendered an error boundary, hydration never completed, and 11
# journeys failed on a 20-second wait for a page that was never coming.
#
# `--network host` puts postgres and mailpit on the runner's own network
# namespace, listening on the real loopback with nothing in between. It is
# also how a developer runs them locally, so CI and a laptop now fail and
# pass for the same reasons.
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- name: Start postgres and mailpit on the host network
run: |
# max_connections is raised and logging turned up for a reason: the
# Worker opens ONE client per request and closes it after the
# response, so a browser-driven suite can hold a surprising number at
# once. `log_connections` makes an exhausted server say so in its own
# words instead of surfacing as an unexplained refusal at the client.
docker run -d --name inkloom-postgres --network host \
-e POSTGRES_USER=inkloom -e POSTGRES_PASSWORD=inkloom_ci -e POSTGRES_DB=inkloom \
postgres:17-alpine \
-c max_connections=400 -c log_connections=on -c log_disconnections=on
docker run -d --name inkloom-mailpit --network host axllent/mailpit:latest
for attempt in $(seq 1 30); do
if docker exec inkloom-postgres pg_isready -U inkloom -h 127.0.0.1 >/dev/null 2>&1; then
echo "postgres is accepting connections."
break
fi
if [ "$attempt" = "30" ]; then
echo "::error::postgres never became ready."
docker logs inkloom-postgres
exit 1
fi
sleep 2
done
# Proves the loopback listener is real, from OUTSIDE the container —
# which is the exact thing a service container did not provide.
until nc -z 127.0.0.1 5432; do sleep 1; done
until nc -z 127.0.0.1 8025; do sleep 1; done
# Printed so the next person debugging this can see in one glance
# whether postgres itself is on the port or a docker-proxy forwarder.
echo "--- listeners ---"
ss -ltnp 2>/dev/null | grep -E ':(5432|8025|1025)\b' || true
- name: Prepare the database
run: |
pnpm db:migrate
pnpm db:seed
# The job-level `env:` block above reaches Node — migrations, the seed,
# the Playwright process — but NOT the Worker. The dev server runs the
# application inside workerd, whose environment is assembled from
# wrangler.jsonc `vars` plus `.dev.vars`, and inherits nothing from the
# shell that launched it. Locally an untracked `.dev.vars` supplies the
# secrets, which is why this was invisible until CI ran for the first
# time: every developer had the file, the runner had no such file, and
# the Worker answered every request with `BETTER_AUTH_SECRET: expected
# string, received undefined`.
#
# Only the values with no safe default are written here. Everything else
# is deliberately left to wrangler.jsonc, so the suite runs against the
# same configuration a developer has locally rather than a CI-only one.
- name: Give the Worker its environment
run: |
# The runner's own PRIVATE address, not 127.0.0.1, and that is the
# whole reason the Worker could not reach the database.
#
# miniflare runs the Worker behind a network policy that permits
# `public` and `private` addresses and not `local`, so every
# connection to loopback was refused inside the runtime before it
# reached postgres — instantly, with "connect(): Connection refused",
# which reads exactly like a database that is not running. Node was
# never subject to that policy, so migrations and the seed worked
# perfectly and made the database look healthy.
RUNNER_IP=$(hostname -I | awk '{print $1}')
echo "Worker will reach postgres at $RUNNER_IP"
# Derived from the job's own DATABASE_URL rather than written out
# again, so the credential appears exactly once in this file and the
# secret scanner keeps its strict rule about postgres URLs.
WORKER_DATABASE_URL="${DATABASE_URL/@127.0.0.1/@$RUNNER_IP}"
{
echo "DATABASE_URL=$WORKER_DATABASE_URL"
echo "BETTER_AUTH_SECRET=ci-only-secret-not-used-anywhere-else-000000"
echo "ACCESS_CODE_PEPPER=ci-only-access-code-pepper-32-chars-x"
echo "IP_HASH_PEPPER=ci-only-ip-hash-pepper-32-characters"
# Cloudflare's documented always-passes test secret, paired with the
# always-passes sitekey already in wrangler.jsonc. Turnstile stays
# ENABLED so the suite exercises the real gate — including
# turnstile-resilience.spec.ts, which is meaningless with it off.
echo "TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA"
} > apps/web/.dev.vars
# Proves, from this runner, that the URL the Worker was handed is one a
# client can actually use. If this succeeds and the Worker still cannot
# connect, the difference is workerd rather than the database — which is
# exactly the distinction the failure mode does not make on its own.
- name: The Worker's database URL, exercised
run: |
echo "url: $(sed -n 's|^DATABASE_URL=.*@|host: |p' apps/web/.dev.vars)"
node -e "
const { Client } = require('pg');
const url = require('fs').readFileSync('apps/web/.dev.vars', 'utf8')
.split('\n').find((l) => l.startsWith('DATABASE_URL=')).slice(13);
const c = new Client({ connectionString: url });
c.connect()
.then(() => c.query('select 1'))
.then(() => { console.log('node connected to the Worker URL: ok'); return c.end(); })
.catch((e) => { console.log('node could NOT connect:', e.message); process.exit(1); });
"
- name: Install Chromium
run: pnpm exec playwright install --with-deps chromium
- name: End-to-end tests
run: pnpm test:e2e --project=chromium --shard=${{ matrix.shard }}/4
# The database's own account of what happened, which the client side can
# only guess at. A refusal looks identical from the outside whether the
# server was unreachable, out of connection slots, or never asked.
- name: What postgres saw
if: always()
run: |
echo "--- connection errors ---"
docker logs inkloom-postgres 2>&1 | grep -iE "too many|fatal|could not|reject" | tail -30 || true
echo "--- peak connections ---"
docker exec inkloom-postgres psql -U inkloom -d inkloom -tAc \
"SELECT count(*) || ' open now, max_connections=' || current_setting('max_connections')
FROM pg_stat_activity" || true
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report-shard-${{ matrix.shard }}
path: |
playwright-report/
test-results/
retention-days: 7
# ---------------------------------------------------------------------------
# Dependency vulnerabilities. Advisory-only on a PR so a fresh advisory in a
# transitive dependency does not block an unrelated fix, but it is always
# visible and always reported.
# ---------------------------------------------------------------------------
audit:
name: Dependency audit
runs-on: ubuntu-latest
timeout-minutes: 10
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: pnpm }
- run: pnpm install --frozen-lockfile
- name: Audit
run: pnpm audit --audit-level=high
# ---------------------------------------------------------------------------
# A single gate for branch protection to require.
# ---------------------------------------------------------------------------
ci:
name: CI
runs-on: ubuntu-latest
# NOTE the absence of `e2e`, which is deliberate and is explained on that
# job. It runs, it reports, and it does not block — until a Worker in local
# development can reach a database on this runner.
needs: [static, secrets, unit, database, build]
if: always()
steps:
- name: Verify every required job succeeded
run: |
if [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "true" ]; then
echo "::error::One or more required jobs failed."
exit 1
fi
echo "All required jobs passed."
- name: Say plainly what was NOT covered
if: ${{ needs.e2e.result != 'success' }}
run: |
{
echo "### End-to-end journeys were not verified by this run"
echo ""
echo "The suite is advisory in CI (see .github/workflows/ci.yml). It passes"
echo "locally. Run it before merging anything that touches a user journey:"
echo ""
echo '```'
echo "pnpm test:e2e"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"