-
Notifications
You must be signed in to change notification settings - Fork 53
540 lines (474 loc) · 26.5 KB
/
deploy.yml
File metadata and controls
540 lines (474 loc) · 26.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
name: Deploy SPA CF Workers[Worker一体化部署]
on:
push:
branches: [main, master]
paths:
- "apps/ui/**"
- "apps/worker/**"
- "apps/attempt-worker/**"
workflow_dispatch:
inputs:
from_panel:
description: "是否由部署控制面板触发 / triggered from deployment control panel"
required: false
default: "auto"
deploy_action:
description: "部署动作:update/init(默认 update)"
required: false
default: "update"
deploy_target:
description: "部署目标:frontend/backend/both/auto(默认 auto)"
required: false
default: "auto"
apply_migrations:
description: "数据库迁移:true/false/auto(默认 auto,仅迁移文件变更时执行)"
required: false
default: "auto"
repository_dispatch:
types: [deploy-spa-button]
jobs:
check-config:
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.check.outputs.should_deploy }}
deploy_action: ${{ steps.plan.outputs.deploy_action }}
deploy_target: ${{ steps.plan.outputs.deploy_target }}
apply_migrations: ${{ steps.plan.outputs.apply_migrations }}
deploy_ui: ${{ steps.plan.outputs.deploy_ui }}
deploy_worker: ${{ steps.plan.outputs.deploy_worker }}
should_migrate: ${{ steps.plan.outputs.should_migrate }}
ui_changed: ${{ steps.changes.outputs.ui_changed }}
worker_changed: ${{ steps.changes.outputs.worker_changed }}
migrations_changed: ${{ steps.changes.outputs.migrations_changed }}
steps:
- name: 📥 检出代码
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: 🔍 检查部署配置
id: check
run: |
if [[ "${{ github.event_name }}" == "repository_dispatch" ]]; then
echo "should_deploy=true" >> $GITHUB_OUTPUT
echo "✅ 手动触发(repository_dispatch),忽略开关,允许部署"
exit 0
fi
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.from_panel }}" != "true" ]]; then
echo "should_deploy=true" >> $GITHUB_OUTPUT
echo "✅ 手动触发(workflow_dispatch),忽略开关,允许部署"
exit 0
fi
SPA_ENABLED="${{ vars.SPA_DEPLOY }}"
[ -z "$SPA_ENABLED" ] && SPA_ENABLED="true"
if [ "$SPA_ENABLED" = "true" ]; then
echo "should_deploy=true" >> $GITHUB_OUTPUT
echo "✅ SPA 自动部署已开启,允许部署"
else
echo "should_deploy=false" >> $GITHUB_OUTPUT
echo "⏸️ SPA 自动部署已关闭,跳过部署"
fi
- name: 🔎 检测变更范围
id: changes
run: |
echo "ui_changed=false" >> $GITHUB_OUTPUT
echo "worker_changed=false" >> $GITHUB_OUTPUT
echo "migrations_changed=false" >> $GITHUB_OUTPUT
if [[ "${{ github.event_name }}" == "push" ]]; then
before="${{ github.event.before }}"
if [[ -z "$before" || "$before" == "0000000000000000000000000000000000000000" ]]; then
echo "ui_changed=true" >> $GITHUB_OUTPUT
echo "worker_changed=true" >> $GITHUB_OUTPUT
echo "migrations_changed=true" >> $GITHUB_OUTPUT
exit 0
fi
if ! git cat-file -e "$before^{commit}" 2>/dev/null; then
echo "⚠️ before 提交不存在,默认视为全量变更"
echo "ui_changed=true" >> $GITHUB_OUTPUT
echo "worker_changed=true" >> $GITHUB_OUTPUT
echo "migrations_changed=true" >> $GITHUB_OUTPUT
exit 0
fi
CHANGED=$(git diff --name-only "$before" "${{ github.sha }}")
if echo "$CHANGED" | grep -q "^apps/ui/"; then
echo "ui_changed=true" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED" | grep -Eq "^apps/(worker|attempt-worker)/"; then
echo "worker_changed=true" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED" | grep -q "^apps/worker/migrations/"; then
echo "migrations_changed=true" >> $GITHUB_OUTPUT
fi
fi
- name: 🧭 生成部署计划
id: plan
run: |
event="${{ github.event_name }}"
deploy_action="${{ github.event.inputs.deploy_action }}"
deploy_target="${{ github.event.inputs.deploy_target }}"
apply_migrations="${{ github.event.inputs.apply_migrations }}"
if [[ "$event" == "repository_dispatch" ]]; then
if [[ -n "${{ github.event.client_payload.deploy_target }}" ]]; then
deploy_target="${{ github.event.client_payload.deploy_target }}"
fi
if [[ -n "${{ github.event.client_payload.apply_migrations }}" ]]; then
apply_migrations="${{ github.event.client_payload.apply_migrations }}"
fi
if [[ -n "${{ github.event.client_payload.deploy_action }}" ]]; then
deploy_action="${{ github.event.client_payload.deploy_action }}"
fi
fi
if [[ -z "$deploy_action" ]]; then
deploy_action="update"
fi
if [[ -z "$deploy_target" ]]; then
deploy_target="auto"
fi
if [[ -z "$apply_migrations" ]]; then
apply_migrations="auto"
fi
if [[ "$deploy_action" == "init" ]]; then
deploy_target="both"
apply_migrations="true"
fi
ui_changed="${{ steps.changes.outputs.ui_changed }}"
worker_changed="${{ steps.changes.outputs.worker_changed }}"
migrations_changed="${{ steps.changes.outputs.migrations_changed }}"
deploy_ui="false"
deploy_worker="false"
case "$deploy_target" in
frontend)
deploy_ui="true"
deploy_worker="true"
;;
backend)
deploy_ui="false"
deploy_worker="true"
;;
both)
deploy_ui="true"
deploy_worker="true"
;;
auto|*)
if [[ "$event" == "push" ]]; then
if [[ "$ui_changed" == "true" ]]; then
deploy_ui="true"
fi
if [[ "$worker_changed" == "true" ]]; then
deploy_worker="true"
fi
else
deploy_ui="true"
deploy_worker="true"
fi
;;
esac
should_migrate="false"
if [[ "$apply_migrations" == "true" ]]; then
should_migrate="true"
elif [[ "$apply_migrations" == "auto" ]]; then
if [[ "$event" == "push" && "$migrations_changed" == "true" ]]; then
should_migrate="true"
fi
fi
echo "deploy_action=$deploy_action" >> $GITHUB_OUTPUT
echo "deploy_target=$deploy_target" >> $GITHUB_OUTPUT
echo "apply_migrations=$apply_migrations" >> $GITHUB_OUTPUT
echo "deploy_ui=$deploy_ui" >> $GITHUB_OUTPUT
echo "deploy_worker=$deploy_worker" >> $GITHUB_OUTPUT
echo "should_migrate=$should_migrate" >> $GITHUB_OUTPUT
init-spa:
needs: check-config
if: needs.check-config.outputs.should_deploy == 'true' && needs.check-config.outputs.deploy_action == 'init'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: "1.3.9"
no-cache: true
- name: Show init plan
run: |
echo "deploy_action=${{ needs.check-config.outputs.deploy_action }}"
echo "deploy_target=${{ needs.check-config.outputs.deploy_target }}"
echo "apply_migrations=${{ needs.check-config.outputs.apply_migrations }}"
echo "deploy_ui=${{ needs.check-config.outputs.deploy_ui }}"
echo "deploy_worker=${{ needs.check-config.outputs.deploy_worker }}"
echo "should_migrate=${{ needs.check-config.outputs.should_migrate }}"
- name: Check if D1 database exists
id: check-db
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "检查 D1 数据库是否已存在..."
DATABASE_LIST=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
EXISTING_DB=$(echo "$DATABASE_LIST" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -n "$EXISTING_DB" ]; then
echo "db_exists=true" >> $GITHUB_OUTPUT
echo "✅ D1 已存在,跳过初始化"
else
echo "db_exists=false" >> $GITHUB_OUTPUT
fi
- name: Install dependencies
if: steps.check-db.outputs.db_exists != 'true'
run: bun install
- name: Build UI
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true')
run: bun run --filter api-worker-ui build
- name: Verify UI build
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true')
run: |
if [ ! -d "apps/ui/dist" ]; then
echo "❌ UI 构建失败:dist 目录不存在"
exit 1
fi
if [ ! -f "apps/ui/dist/index.html" ]; then
echo "❌ UI 构建失败:index.html 不存在"
exit 1
fi
echo "✅ UI 构建产物已就绪"
- name: Disable wrangler telemetry
if: steps.check-db.outputs.db_exists != 'true'
run: bunx wrangler telemetry disable
- name: Create D1 database (init only)
if: steps.check-db.outputs.db_exists != 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "创建 D1 数据库..."
CREATE_OUTPUT=$(bunx wrangler d1 create api-worker --config apps/worker/wrangler.toml 2>&1)
if echo "$CREATE_OUTPUT" | grep -q "database_id"; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "database_id = \"\K[^\"]+")
if [ -z "$DATABASE_ID" ]; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})")
fi
else
LIST_OUTPUT=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
DATABASE_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.name=="api-worker") | .uuid')
fi
if [ -z "$DATABASE_ID" ]; then
echo "❌ 无法创建或找到 D1 数据库"
exit 1
fi
sed -i -E "s/(database_id = \")([^\"]+)(\")/\\1$DATABASE_ID\\3/" apps/worker/wrangler.toml
sed -i -E "s/(database_id = \")([^\"]+)(\")/\\1$DATABASE_ID\\3/" apps/attempt-worker/wrangler.toml
echo "database_id=$DATABASE_ID" >> $GITHUB_ENV
- name: Create or verify KV namespace
if: steps.check-db.outputs.db_exists != 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "检查并创建 KV Namespace..."
KV_NAMESPACE_TITLE="api-worker-hot"
KV_LIST_RAW=$(bunx wrangler kv namespace list --config apps/worker/wrangler.toml 2>/dev/null || true)
KV_ID=$(KV_LIST_RAW="$KV_LIST_RAW" KV_NAMESPACE_TITLE="$KV_NAMESPACE_TITLE" node -e 'const raw=process.env.KV_LIST_RAW||"";const title=(process.env.KV_NAMESPACE_TITLE||"").toLowerCase();const idPattern=/[0-9a-f]{32}/ig;const lines=raw.split(/\r?\n/);let found="";for(const line of lines){if(!line.toLowerCase().includes(title))continue;const match=line.match(idPattern);if(match&&match[0]){found=match[0].toLowerCase();break;}}if(!found&&title){const escapedTitle=title.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");const nearby=new RegExp(`${escapedTitle}[\\s\\S]{0,120}([0-9a-f]{32})`,"i");const m=raw.toLowerCase().match(nearby);if(m&&m[1])found=m[1].toLowerCase();}process.stdout.write(found);')
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
CREATE_OUTPUT=$(bunx wrangler kv namespace create "$KV_NAMESPACE_TITLE" --config apps/worker/wrangler.toml 2>&1)
KV_ID=$(CREATE_OUTPUT="$CREATE_OUTPUT" node -e 'const raw=process.env.CREATE_OUTPUT||"";const exact=raw.match(/^\s*id\s*=\s*"([0-9a-f]{32})"/im);if(exact&&exact[1]){process.stdout.write(exact[1].toLowerCase());process.exit(0);}const any=raw.match(/\b([0-9a-f]{32})\b/i);if(any&&any[1])process.stdout.write(any[1].toLowerCase());')
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
KV_LIST_RAW=$(bunx wrangler kv namespace list --config apps/worker/wrangler.toml 2>/dev/null || true)
KV_ID=$(KV_LIST_RAW="$KV_LIST_RAW" KV_NAMESPACE_TITLE="$KV_NAMESPACE_TITLE" node -e 'const raw=process.env.KV_LIST_RAW||"";const title=(process.env.KV_NAMESPACE_TITLE||"").toLowerCase();const idPattern=/[0-9a-f]{32}/ig;const lines=raw.split(/\r?\n/);let found="";for(const line of lines){if(!line.toLowerCase().includes(title))continue;const match=line.match(idPattern);if(match&&match[0]){found=match[0].toLowerCase();break;}}if(!found&&title){const escapedTitle=title.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");const nearby=new RegExp(`${escapedTitle}[\\s\\S]{0,120}([0-9a-f]{32})`,"i");const m=raw.toLowerCase().match(nearby);if(m&&m[1])found=m[1].toLowerCase();}process.stdout.write(found);')
fi
fi
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
echo "❌ 无法创建或找到 KV Namespace: $KV_NAMESPACE_TITLE(list/create 输出未解析到有效 id)"
exit 1
fi
sed -i -E '/binding = "KV_HOT"/,/^\[\[/{s/(id = ")[^"]+(")/\1'"$KV_ID"'\2/;}' apps/worker/wrangler.toml
sed -i -E '/binding = "KV_HOT"/,/^\[\[/{s/(id = ")[^"]+(")/\1'"$KV_ID"'\2/;}' apps/attempt-worker/wrangler.toml
KV_CONFIG_ID=$(grep -A4 'binding = "KV_HOT"' apps/worker/wrangler.toml | grep -m1 -oP '(?<=id = ")[^"]+')
if [ "$KV_CONFIG_ID" != "$KV_ID" ]; then
echo "❌ KV_HOT 写回失败:期望 $KV_ID,实际 ${KV_CONFIG_ID:-<empty>}"
echo "当前 KV_HOT 配置片段:"
grep -A4 'binding = "KV_HOT"' apps/worker/wrangler.toml || true
exit 1
fi
ATTEMPT_KV_CONFIG_ID=$(grep -A4 'binding = "KV_HOT"' apps/attempt-worker/wrangler.toml | grep -m1 -oP '(?<=id = ")[^"]+')
if [ "$ATTEMPT_KV_CONFIG_ID" != "$KV_ID" ]; then
echo "❌ attempt-worker KV_HOT 写回失败:期望 $KV_ID,实际 ${ATTEMPT_KV_CONFIG_ID:-<empty>}"
echo "当前 attempt-worker KV_HOT 配置片段:"
grep -A4 'binding = "KV_HOT"' apps/attempt-worker/wrangler.toml || true
exit 1
fi
echo "kv_hot_id=$KV_ID" >> $GITHUB_ENV
echo "✅ KV Namespace 就绪: $KV_ID"
- name: Apply D1 migrations (remote)
if: steps.check-db.outputs.db_exists != 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler d1 migrations apply DB --remote --config apps/worker/wrangler.toml
- name: Deploy to Cloudflare Workers (SPA Mode)
if: steps.check-db.outputs.db_exists != 'true' && needs.check-config.outputs.deploy_worker == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler deploy --config apps/attempt-worker/wrangler.toml
- name: Deploy to Cloudflare Workers (API Worker)
if: steps.check-db.outputs.db_exists != 'true' && (needs.check-config.outputs.deploy_worker == 'true' || needs.check-config.outputs.deploy_ui == 'true')
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler deploy --config apps/worker/wrangler.toml
deploy-spa:
needs: check-config
if: needs.check-config.outputs.should_deploy == 'true' && needs.check-config.outputs.deploy_action != 'init'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: "1.3.9"
no-cache: true
- name: Show deploy plan
run: |
echo "deploy_action=${{ needs.check-config.outputs.deploy_action }}"
echo "deploy_target=${{ needs.check-config.outputs.deploy_target }}"
echo "apply_migrations=${{ needs.check-config.outputs.apply_migrations }}"
echo "ui_changed=${{ needs.check-config.outputs.ui_changed }}"
echo "worker_changed=${{ needs.check-config.outputs.worker_changed }}"
echo "migrations_changed=${{ needs.check-config.outputs.migrations_changed }}"
echo "deploy_ui=${{ needs.check-config.outputs.deploy_ui }}"
echo "deploy_worker=${{ needs.check-config.outputs.deploy_worker }}"
echo "should_migrate=${{ needs.check-config.outputs.should_migrate }}"
- name: Check if deploy button trigger
id: check-deploy-button
run: |
if [[ "${{ github.event_name }}" == "repository_dispatch" && "${{ github.event.action }}" == "deploy-spa-button" ]]; then
echo "is_deploy_button=true" >> $GITHUB_OUTPUT
else
echo "is_deploy_button=false" >> $GITHUB_OUTPUT
fi
- name: Install dependencies
run: bun install
- name: Build UI
if: needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true'
run: bun run --filter api-worker-ui build
- name: Verify UI build
if: needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.deploy_worker == 'true'
run: |
if [ ! -d "apps/ui/dist" ]; then
echo "❌ UI 构建失败:dist 目录不存在"
exit 1
fi
if [ ! -f "apps/ui/dist/index.html" ]; then
echo "❌ UI 构建失败:index.html 不存在"
exit 1
fi
echo "✅ UI 构建产物已就绪"
- name: Disable wrangler telemetry
run: bunx wrangler telemetry disable
- name: Create or verify D1 Database
if: needs.check-config.outputs.deploy_worker == 'true' || needs.check-config.outputs.deploy_ui == 'true' || needs.check-config.outputs.should_migrate == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "检查并创建 D1 数据库..."
DATABASE_LIST=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
EXISTING_DB=$(echo "$DATABASE_LIST" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -n "$EXISTING_DB" ]; then
echo "✅ 找到现有 D1 数据库: $EXISTING_DB"
DATABASE_ID=$EXISTING_DB
else
CREATE_OUTPUT=$(bunx wrangler d1 create api-worker --config apps/worker/wrangler.toml 2>&1)
if echo "$CREATE_OUTPUT" | grep -q "database_id"; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "database_id = \"\K[^\"]+")
if [ -z "$DATABASE_ID" ]; then
DATABASE_ID=$(echo "$CREATE_OUTPUT" | grep -oP "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})")
fi
else
LIST_OUTPUT=$(bunx wrangler d1 list --json --config apps/worker/wrangler.toml 2>/dev/null || echo "[]")
DATABASE_ID=$(echo "$LIST_OUTPUT" | jq -r '.[] | select(.name=="api-worker") | .uuid')
if [ -z "$DATABASE_ID" ]; then
echo "❌ 无法创建或找到 D1 数据库"
exit 1
fi
fi
fi
sed -i -E "s/(database_id = \")([^\"]+)(\")/\\1$DATABASE_ID\\3/" apps/worker/wrangler.toml
sed -i -E "s/(database_id = \")([^\"]+)(\")/\\1$DATABASE_ID\\3/" apps/attempt-worker/wrangler.toml
echo "database_id=$DATABASE_ID" >> $GITHUB_ENV
- name: Create or verify KV namespace
if: needs.check-config.outputs.deploy_worker == 'true' || needs.check-config.outputs.deploy_ui == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
echo "检查并创建 KV Namespace..."
KV_NAMESPACE_TITLE="api-worker-hot"
KV_LIST_RAW=$(bunx wrangler kv namespace list --config apps/worker/wrangler.toml 2>/dev/null || true)
KV_ID=$(KV_LIST_RAW="$KV_LIST_RAW" KV_NAMESPACE_TITLE="$KV_NAMESPACE_TITLE" node -e 'const raw=process.env.KV_LIST_RAW||"";const title=(process.env.KV_NAMESPACE_TITLE||"").toLowerCase();const idPattern=/[0-9a-f]{32}/ig;const lines=raw.split(/\r?\n/);let found="";for(const line of lines){if(!line.toLowerCase().includes(title))continue;const match=line.match(idPattern);if(match&&match[0]){found=match[0].toLowerCase();break;}}if(!found&&title){const escapedTitle=title.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");const nearby=new RegExp(`${escapedTitle}[\\s\\S]{0,120}([0-9a-f]{32})`,"i");const m=raw.toLowerCase().match(nearby);if(m&&m[1])found=m[1].toLowerCase();}process.stdout.write(found);')
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
CREATE_OUTPUT=$(bunx wrangler kv namespace create "$KV_NAMESPACE_TITLE" --config apps/worker/wrangler.toml 2>&1)
KV_ID=$(CREATE_OUTPUT="$CREATE_OUTPUT" node -e 'const raw=process.env.CREATE_OUTPUT||"";const exact=raw.match(/^\s*id\s*=\s*"([0-9a-f]{32})"/im);if(exact&&exact[1]){process.stdout.write(exact[1].toLowerCase());process.exit(0);}const any=raw.match(/\b([0-9a-f]{32})\b/i);if(any&&any[1])process.stdout.write(any[1].toLowerCase());')
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
KV_LIST_RAW=$(bunx wrangler kv namespace list --config apps/worker/wrangler.toml 2>/dev/null || true)
KV_ID=$(KV_LIST_RAW="$KV_LIST_RAW" KV_NAMESPACE_TITLE="$KV_NAMESPACE_TITLE" node -e 'const raw=process.env.KV_LIST_RAW||"";const title=(process.env.KV_NAMESPACE_TITLE||"").toLowerCase();const idPattern=/[0-9a-f]{32}/ig;const lines=raw.split(/\r?\n/);let found="";for(const line of lines){if(!line.toLowerCase().includes(title))continue;const match=line.match(idPattern);if(match&&match[0]){found=match[0].toLowerCase();break;}}if(!found&&title){const escapedTitle=title.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");const nearby=new RegExp(`${escapedTitle}[\\s\\S]{0,120}([0-9a-f]{32})`,"i");const m=raw.toLowerCase().match(nearby);if(m&&m[1])found=m[1].toLowerCase();}process.stdout.write(found);')
fi
fi
if [ -z "$KV_ID" ] || [ "$KV_ID" = "null" ]; then
echo "❌ 无法创建或找到 KV Namespace: $KV_NAMESPACE_TITLE(list/create 输出未解析到有效 id)"
exit 1
fi
sed -i -E '/binding = "KV_HOT"/,/^\[\[/{s/(id = ")[^"]+(")/\1'"$KV_ID"'\2/;}' apps/worker/wrangler.toml
sed -i -E '/binding = "KV_HOT"/,/^\[\[/{s/(id = ")[^"]+(")/\1'"$KV_ID"'\2/;}' apps/attempt-worker/wrangler.toml
KV_CONFIG_ID=$(grep -A4 'binding = "KV_HOT"' apps/worker/wrangler.toml | grep -m1 -oP '(?<=id = ")[^"]+')
if [ "$KV_CONFIG_ID" != "$KV_ID" ]; then
echo "❌ KV_HOT 写回失败:期望 $KV_ID,实际 ${KV_CONFIG_ID:-<empty>}"
echo "当前 KV_HOT 配置片段:"
grep -A4 'binding = "KV_HOT"' apps/worker/wrangler.toml || true
exit 1
fi
ATTEMPT_KV_CONFIG_ID=$(grep -A4 'binding = "KV_HOT"' apps/attempt-worker/wrangler.toml | grep -m1 -oP '(?<=id = ")[^"]+')
if [ "$ATTEMPT_KV_CONFIG_ID" != "$KV_ID" ]; then
echo "❌ attempt-worker KV_HOT 写回失败:期望 $KV_ID,实际 ${ATTEMPT_KV_CONFIG_ID:-<empty>}"
echo "当前 attempt-worker KV_HOT 配置片段:"
grep -A4 'binding = "KV_HOT"' apps/attempt-worker/wrangler.toml || true
exit 1
fi
echo "kv_hot_id=$KV_ID" >> $GITHUB_ENV
echo "✅ KV Namespace 就绪: $KV_ID"
- name: Apply D1 migrations (remote)
if: needs.check-config.outputs.should_migrate == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler d1 migrations apply DB --remote --config apps/worker/wrangler.toml
- name: Deploy to Cloudflare Workers (Attempt Worker)
if: needs.check-config.outputs.deploy_worker == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler deploy --config apps/attempt-worker/wrangler.toml
- name: Deploy to Cloudflare Workers (API Worker)
if: needs.check-config.outputs.deploy_worker == 'true' || needs.check-config.outputs.deploy_ui == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: bunx wrangler deploy --config apps/worker/wrangler.toml
- name: Display Success Information
if: steps.check-deploy-button.outputs.is_deploy_button == 'true' && success()
run: |
echo "===================================================="
echo "🎉 SPA Worker 已成功部署到 Cloudflare Workers!"
echo "===================================================="
echo "后续步骤:"
echo "1. 访问您的 Worker URL"
echo "2. 登录后立刻修改管理员密码"
echo "3. 配置相关系统参数"
echo "===================================================="
- name: Notify deployment status
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "✅ SPA Worker 部署成功!"
else
echo "❌ SPA Worker 部署失败!"
fi