Repository navigation
552 lines (475 loc) · 21.5 KB
/
Copy pathci.yml
File metadata and controls
552 lines (475 loc) · 21.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
permissions:
contents: read
concurrency:
group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
env:
NODE_VERSION: 24.18.0
PNPM_VERSION: 8.15.9
jobs:
backend-tests:
name: Backend tests
runs-on: ubuntu-24.04
timeout-minutes: 15
env:
WAITQUEUE_REDIS_INTEGRATION_URL: redis://127.0.0.1:6379/15
services:
redis:
image: redis:7.4.10-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 2s
--health-timeout 2s
--health-retries 20
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: ${{ env.NODE_VERSION }}
check-latest: false
- name: Activate pnpm
shell: bash
run: |
set -Eeuo pipefail
corepack enable
corepack prepare "pnpm@${PNPM_VERSION}" --activate
- name: Locate pnpm store
id: pnpm-store
shell: bash
run: |
set -Eeuo pipefail
echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
- name: Restore pnpm store
uses: actions/cache@v6
with:
path: ${{ steps.pnpm-store.outputs.path }}
key: ${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-backend-${{ hashFiles('wait-queue/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-backend-
- name: Install backend dependencies
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir wait-queue install --frozen-lockfile
- name: Run backend tests
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir wait-queue test
dashboard-typecheck:
name: Dashboard typecheck
runs-on: ubuntu-24.04
timeout-minutes: 15
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: ${{ env.NODE_VERSION }}
check-latest: false
- name: Activate pnpm
shell: bash
run: |
set -Eeuo pipefail
corepack enable
corepack prepare "pnpm@${PNPM_VERSION}" --activate
- name: Locate pnpm store
id: pnpm-store
shell: bash
run: |
set -Eeuo pipefail
echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
- name: Restore pnpm store
uses: actions/cache@v6
with:
path: ${{ steps.pnpm-store.outputs.path }}
key: ${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-dashboard-${{ hashFiles('admin-dashboard/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-dashboard-
- name: Install dashboard dependencies
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir admin-dashboard install --frozen-lockfile
- name: Run dashboard typecheck
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir admin-dashboard typecheck
- name: Check dashboard design contract
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir admin-dashboard check:design
production-build:
name: Production build
runs-on: ubuntu-24.04
timeout-minutes: 20
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: ${{ env.NODE_VERSION }}
check-latest: false
- name: Activate pnpm
shell: bash
run: |
set -Eeuo pipefail
corepack enable
corepack prepare "pnpm@${PNPM_VERSION}" --activate
- name: Locate pnpm store
id: pnpm-store
shell: bash
run: |
set -Eeuo pipefail
echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
- name: Restore pnpm store
uses: actions/cache@v6
with:
path: ${{ steps.pnpm-store.outputs.path }}
key: ${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-all-${{ hashFiles('wait-queue/pnpm-lock.yaml', 'admin-dashboard/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ runner.arch }}-node-${{ env.NODE_VERSION }}-pnpm-${{ env.PNPM_VERSION }}-all-
- name: Install all dependencies
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm install:all
- name: Build production artifacts
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm build
- name: Build and verify static dashboard demo
shell: bash
env:
PAGES_BASE_PATH: /waitqueue.js
run: |
set -Eeuo pipefail
corepack pnpm --dir admin-dashboard build:pages
corepack pnpm --dir admin-dashboard check:pages
dependency-audit:
name: Dependency audit
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: ${{ env.NODE_VERSION }}
check-latest: false
- name: Activate pnpm
shell: bash
run: |
set -Eeuo pipefail
corepack enable
corepack prepare "pnpm@${PNPM_VERSION}" --activate
- name: Audit backend production dependencies
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir wait-queue audit --prod --audit-level high
- name: Audit dashboard production dependencies
shell: bash
run: |
set -Eeuo pipefail
corepack pnpm --dir admin-dashboard audit --prod --audit-level high
compose-smoke:
name: Compose smoke
needs:
- backend-tests
- dashboard-typecheck
- production-build
- dependency-audit
runs-on: ubuntu-24.04
timeout-minutes: 30
env:
COMPOSE_PROJECT_NAME: waitqueue-ci-${{ github.run_id }}-${{ github.run_attempt }}
BIND_ADDRESS: 127.0.0.1
API_PORT: 3000
DASHBOARD_PORT: 3001
DB_DATABASE: waitqueue_ci
DB_USER: waitqueue_ci
HOOK_URL_ALLOWLIST: https://worker.example.com,http://127.0.0.1:3101
steps:
- name: Check out repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Create ephemeral database credentials
shell: bash
run: |
set -Eeuo pipefail
db_password="$(openssl rand -hex 24)"
db_root_password="$(openssl rand -hex 24)"
api_token="$(openssl rand -hex 32)"
echo "::add-mask::$db_password"
echo "::add-mask::$db_root_password"
echo "::add-mask::$api_token"
{
echo "DB_PASSWORD=$db_password"
echo "DB_ROOT_PASSWORD=$db_root_password"
echo "WAITQUEUE_API_TOKEN=$api_token"
} >> "$GITHUB_ENV"
- name: Validate Compose configuration
shell: bash
run: |
set -Eeuo pipefail
docker compose config --quiet
- name: Build and start the stack
shell: bash
run: |
set -Eeuo pipefail
docker compose up --build --detach --wait --wait-timeout 300
- name: Verify HTTP health endpoints
shell: bash
run: |
set -Eeuo pipefail
health_json="$(curl --fail --silent --show-error --retry 10 --retry-all-errors --retry-delay 2 \
"http://127.0.0.1:${API_PORT}/waitqueue/health")"
jq --exit-status \
'.code == 0 and .data.status == "ok"' \
<<< "$health_json" > /dev/null
live_json="$(curl --fail --silent --show-error \
"http://127.0.0.1:${API_PORT}/health/live")"
jq --exit-status \
'.code == 0 and .data.status == "ok"' \
<<< "$live_json" > /dev/null
ready_json="$(curl --fail --silent --show-error --retry 10 --retry-all-errors --retry-delay 2 \
"http://127.0.0.1:${API_PORT}/waitqueue/ready")"
jq --exit-status \
'.code == 0 and .data.status == "ready" and .data.dependencies.mysql == "ok" and .data.dependencies.redis == "ok"' \
<<< "$ready_json" > /dev/null
ready_alias_json="$(curl --fail --silent --show-error \
"http://127.0.0.1:${API_PORT}/health/ready")"
jq --exit-status \
'.code == 0 and .data.status == "ready" and .data.dependencies.mysql == "ok" and .data.dependencies.redis == "ok"' \
<<< "$ready_alias_json" > /dev/null
unauthenticated_metrics_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:${API_PORT}/metrics")"
test "$unauthenticated_metrics_status" = "401"
unauthenticated_metrics_slash_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:${API_PORT}/metrics/")"
test "$unauthenticated_metrics_slash_status" = "401"
wrong_token_metrics_slash_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--header 'Authorization: Bearer definitely-wrong' \
"http://127.0.0.1:${API_PORT}/metrics/")"
test "$wrong_token_metrics_slash_status" = "401"
metrics_body="$(curl --fail --silent --show-error \
--header "Authorization: Bearer ${WAITQUEUE_API_TOKEN}" \
"http://127.0.0.1:${API_PORT}/metrics")"
grep --fixed-strings --quiet '# TYPE waitqueue_queue_waiting_tasks gauge' <<< "$metrics_body"
grep --fixed-strings --quiet '# TYPE waitqueue_callback_attempts_total counter' <<< "$metrics_body"
if grep --extended-regexp --quiet 'namespace=|task_id=|taskId=|token=|hook_url=|hookUrl=' <<< "$metrics_body"; then
exit 1
fi
unauthenticated_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:${API_PORT}/waitqueue/admin/overview")"
test "$unauthenticated_status" = "401"
wrong_token_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--header 'Authorization: Bearer definitely-wrong' \
"http://127.0.0.1:${API_PORT}/waitqueue/admin/overview")"
test "$wrong_token_status" = "401"
authenticated_json="$(curl --fail --silent --show-error \
--header "Authorization: Bearer ${WAITQUEUE_API_TOKEN}" \
"http://127.0.0.1:${API_PORT}/waitqueue/admin/overview")"
jq --exit-status \
'.code == 0 and .data.summary.queueCount == 0 and .data.summary.retrying == 0 and .data.summary.deadLetters == 0 and .data.summary.oldestWaitingAt == null and .data.summary.oldestWaitingAgeSeconds == null and (.data.metricsStartedAt | type) == "string"' \
<<< "$authenticated_json" > /dev/null
private_hook_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request POST \
--header "Authorization: Bearer ${WAITQUEUE_API_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{"namespace":"blocked","hookUrl":"http://127.0.0.1:3101/callback"}' \
"http://127.0.0.1:${API_PORT}/waitqueue/queue/newQueue")"
test "$private_hook_status" = "400"
oversized_status="$(python3 -c \
'import json; print(json.dumps({"namespace":"large","hookUrl":"https://worker.example.com/" + "x" * 40000}))' \
| curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request POST \
--header "Authorization: Bearer ${WAITQUEUE_API_TOKEN}" \
--header 'Content-Type: application/json' \
--data-binary @- \
"http://127.0.0.1:${API_PORT}/waitqueue/queue/newQueue")"
test "$oversized_status" = "413"
dashboard_html="$(curl --fail --silent --show-error --retry 10 --retry-all-errors --retry-delay 2 \
"http://127.0.0.1:${DASHBOARD_PORT}/")"
grep --fixed-strings --quiet 'data-product="waitqueue-console"' <<< "$dashboard_html"
node -e '
let html = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => { html += chunk; });
process.stdin.on("end", () => {
const css = html.match(/<style id="antd-ssr">([\s\S]*?)<\/style>/)?.[1] || "";
if (css.length < 1000 || !/\.ant-(app|btn|layout|menu|table)/.test(css)) process.exit(1);
});
' <<< "$dashboard_html"
dashboard_live_json="$(curl --fail --silent --show-error \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/health/live")"
jq --exit-status \
'.code == 0 and .data.status == "ok"' \
<<< "$dashboard_live_json" > /dev/null
dashboard_metrics_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/metrics")"
test "$dashboard_metrics_status" = "404"
proxy_content_type_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request POST \
--header 'Content-Type: text/plain' \
--data 'not-json' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/queue/newQueue")"
test "$proxy_content_type_status" = "415"
proxy_jsonp_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request POST \
--header 'Content-Type: application/jsonp' \
--data '{}' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/queue/newQueue")"
test "$proxy_jsonp_status" = "415"
untrusted_host_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--header "Host: attacker.example:${DASHBOARD_PORT}" \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/admin/overview")"
test "$untrusted_host_status" = "403"
proxy_oversized_status="$(python3 -c \
'import json; print(json.dumps({"namespace":"large","hookUrl":"https://worker.example.com/" + "x" * 40000}))' \
| curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
--request POST \
--header 'Content-Type: application/json' \
--data-binary @- \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/queue/newQueue")"
test "$proxy_oversized_status" = "413"
proxy_queue_json="$(curl --fail --silent --show-error \
--request POST \
--header 'Authorization: Bearer browser-controlled-value' \
--header 'Cookie: session=browser-controlled-value' \
--header 'Content-Type: application/json' \
--data '{"namespace":"ci","hookUrl":"https://worker.example.com/callback","currMaxCount":2,"crontab":{"run":"0 0 0 1 1 *","check":"0 0 0 1 1 *","expire":"0 0 0 1 1 *"}}' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/queue/newQueue")"
jq --exit-status '.code == 0' <<< "$proxy_queue_json" > /dev/null
proxy_task_json="$(curl --fail --silent --show-error \
--request POST \
--header 'Content-Type: application/json' \
--data '{"namespace":"ci","hookUrl":"https://worker.example.com/callback","taskId":"ci-proxy-task"}' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/scheduler/addTask")"
jq --exit-status '.code == 0' <<< "$proxy_task_json" > /dev/null
proxied_json="$(curl --fail --silent --show-error \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/admin/overview")"
jq --exit-status \
'.code == 0 and .data.summary.queueCount == 1 and .data.summary.waiting == 1' \
<<< "$proxied_json" > /dev/null
queue_id="$(jq --raw-output '.data.queues[0].queueId' <<< "$proxied_json")"
dead_entry_id='ci-dead-entry-1'
dead_task_id='ci-dead-task'
dead_metadata="$(jq --compact-output --null-input \
--arg entryId "$dead_entry_id" \
'{entryId: $entryId, retryCount: 2, failedAt: 1700000000000, reason: "callback_failed", token: "pending:ci-old-claim"}')"
docker compose exec -T redis redis-cli HSET \
"TaskQueue:ci:${queue_id}:deadLetterHashKv" "$dead_task_id" "$dead_metadata" > /dev/null
docker compose exec -T redis redis-cli ZADD \
"TaskQueue:ci:${queue_id}:deadLetterZset" 1700000000000 "$dead_task_id" > /dev/null
docker compose exec -T redis redis-cli HSET \
"TaskQueue:ci:${queue_id}:taskStateHashKv" "$dead_task_id" dead > /dev/null
docker compose exec -T redis redis-cli HSET \
"TaskQueue:ci:${queue_id}:taskGenerationHashKv" "$dead_task_id" "$dead_entry_id" > /dev/null
dead_letters_json="$(curl --fail --silent --show-error --get \
--data-urlencode "queueId=${queue_id}" \
--data-urlencode 'limit=10' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/admin/deadLetters")"
jq --exit-status \
--arg entryId "$dead_entry_id" --arg taskId "$dead_task_id" \
'.code == 0 and .data.total == 1 and .data.items[0].entryId == $entryId and .data.items[0].taskId == $taskId' \
<<< "$dead_letters_json" > /dev/null
rejected_query_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/admin/deadLetters?queueId=${queue_id}&token=not-forwarded")"
test "$rejected_query_status" = "400"
replay_json="$(jq --compact-output --null-input \
--argjson queueId "$queue_id" --arg taskId "$dead_task_id" --arg entryId "$dead_entry_id" \
'{queueId: $queueId, taskId: $taskId, entryId: $entryId}')"
curl --fail --silent --show-error \
--request POST \
--header 'Content-Type: application/json' \
--data "$replay_json" \
"http://127.0.0.1:${DASHBOARD_PORT}/waitqueue/admin/deadLetters/replay" \
| jq --exit-status '.code == 0 and .data.isOk == true' > /dev/null
replayed_waiting="$(docker compose exec -T redis redis-cli LLEN \
"TaskQueue:ci:${queue_id}:waitingQueue")"
test "$replayed_waiting" = "2"
docker compose exec -T dashboard sh -eu -c \
'! grep --recursive --fixed-strings --quiet "$WAITQUEUE_API_TOKEN" /app/.next/static'
- name: Verify migration history and idempotency
shell: bash
run: |
set -Eeuo pipefail
expected_count=0
while IFS= read -r migration_path; do
migration_name="${migration_path##*/}"
if [[ "$migration_name" =~ ^V[1-9][0-9]*__.+\.sql$ ]]; then
expected_count=$((expected_count + 1))
fi
done < <(find wait-queue/sql -maxdepth 1 -type f -print)
if (( expected_count == 0 )); then
echo "No versioned migrations were discovered" >&2
exit 1
fi
before_count="$(docker compose exec -T mysql sh -eu -c '
MYSQL_PWD="$MYSQL_PASSWORD" mysql \
--batch --skip-column-names \
--user="$MYSQL_USER" "$MYSQL_DATABASE" \
--execute="SELECT COUNT(*) FROM waitqueue_schema_migrations"
')"
if [[ "$before_count" != "$expected_count" ]]; then
echo "Expected $expected_count migration records, found $before_count" >&2
exit 1
fi
rerun_output="$(docker compose run --rm --no-deps -T migrate)"
printf '%s\n' "$rerun_output"
if ! grep --extended-regexp --quiet '"applied":0([,}])' <<< "$rerun_output"; then
echo "The idempotency run did not report zero applied migrations" >&2
exit 1
fi
after_count="$(docker compose exec -T mysql sh -eu -c '
MYSQL_PWD="$MYSQL_PASSWORD" mysql \
--batch --skip-column-names \
--user="$MYSQL_USER" "$MYSQL_DATABASE" \
--execute="SELECT COUNT(*) FROM waitqueue_schema_migrations"
')"
if [[ "$after_count" != "$before_count" ]]; then
echo "Migration history changed on the idempotency run" >&2
exit 1
fi
- name: Print diagnostics on failure
if: failure()
shell: bash
run: |
set -Eeuo pipefail
docker compose ps --all || true
docker compose logs --no-color --timestamps || true
- name: Remove CI stack and volumes
if: always()
shell: bash
run: |
set -Eeuo pipefail
docker compose down --volumes --remove-orphans --timeout 20