Repository navigation
223 lines (223 loc) · 10.8 KB
/
Copy pathdeploy-worker.yml
File metadata and controls
223 lines (223 loc) · 10.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
# 数字前缀随 Fork 继承,让部署入口在默认名称排序中靠前,无需逐仓设置置顶。
name: 00 Deploy Worker
on:
push:
branches:
- master
- main
paths:
- "worker/**"
- "frontend/**"
- "shared/**"
- "test/**"
- ".github/scripts/**"
- ".github/workflows/deploy-worker.yml"
- "package.json"
- "package-lock.json"
- "biome.json"
- "wrangler.example.toml"
workflow_dispatch:
inputs:
apply_encryption_keyring:
description: Apply EDGECHAT_ENCRYPTION_KEYRING, retaining every old key ID during rotation
required: false
default: false
type: boolean
rotate_encryption_key:
description: Add a new automatic key and make it active while retaining every old key
required: false
default: false
type: boolean
concurrency:
group: deploy-worker-${{ github.ref }}
cancel-in-progress: true
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Setup Node.js
uses: actions/setup-node@v5
with:
node-version: "24"
cache: npm
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Install security test browser
run: npx playwright install --with-deps chromium
- name: Check editor HTML boundaries
run: npm run test:editor-security
- name: Build frontend assets
run: npm run build
- name: Validate message retention
env:
MESSAGE_RETENTION_DAYS: ${{ vars.MESSAGE_RETENTION_DAYS || '30' }}
shell: bash
run: |
if [[ ! "$MESSAGE_RETENTION_DAYS" =~ ^[1-9][0-9]*$ ]]; then
echo "MESSAGE_RETENTION_DAYS must be a positive integer (days)."
exit 1
fi
- name: Ensure Cloudflare resources
id: ensure_resources
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_KV_NAMESPACE_TITLE: ${{ vars.EDGECHAT_KV_NAMESPACE_TITLE || 'cfchat-sessions' }}
EDGECHAT_S3: ${{ secrets.EDGECHAT_S3 }}
run: node .github/scripts/ensure-cloudflare-resources.mjs
- name: Generate wrangler config for CI
shell: bash
env:
R2_AVAILABLE: ${{ steps.ensure_resources.outputs.r2_available }}
R2_BUCKET_NAME: ${{ steps.ensure_resources.outputs.r2_bucket_name }}
MESSAGE_RETENTION_DAYS: ${{ vars.MESSAGE_RETENTION_DAYS || '30' }}
run: |
cp wrangler.example.toml wrangler.ci.toml
sed -i "s|^MESSAGE_RETENTION_DAYS = .*|MESSAGE_RETENTION_DAYS = \"${MESSAGE_RETENTION_DAYS}\"|" wrangler.ci.toml
sed -i "s|YOUR_D1_DATABASE_ID_HERE|${{ steps.ensure_resources.outputs.d1_database_id }}|g" wrangler.ci.toml
sed -i "s|YOUR_KV_NAMESPACE_ID_HERE|${{ steps.ensure_resources.outputs.kv_namespace_id }}|g" wrangler.ci.toml
if [[ "$R2_AVAILABLE" == "true" ]]; then
sed -i "/^\[\[r2_buckets\]\]$/,/^$/ s|^bucket_name = \".*\"$|bucket_name = \"${R2_BUCKET_NAME}\"|" wrangler.ci.toml
else
sed -i '/^\[\[r2_buckets\]\]$/,/^$/d' wrangler.ci.toml
fi
- name: Initialize D1 schema (first creation only)
if: steps.ensure_resources.outputs.d1_created == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: npx wrangler d1 execute ${{ steps.ensure_resources.outputs.d1_database_name }} --remote --file worker/schema.sql --config wrangler.ci.toml
- name: Prepare D1 migrations
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_D1_DATABASE_ID: ${{ steps.ensure_resources.outputs.d1_database_id }}
run: node .github/scripts/prepare-d1-migrations.mjs
- name: Apply D1 migrations
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: npx wrangler d1 execute ${{ steps.ensure_resources.outputs.d1_database_name }} --remote --file .tmp/edgechat-d1-migrations.sql --config wrangler.ci.toml
- name: Verify D1 schema contract
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_D1_DATABASE_ID: ${{ steps.ensure_resources.outputs.d1_database_id }}
run: node .github/scripts/prepare-d1-migrations.mjs --verify
- name: Sync administrator credentials (optional)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_D1_DATABASE_ID: ${{ steps.ensure_resources.outputs.d1_database_id }}
EDGECHAT_ADMIN_USERNAME: ${{ secrets.EDGECHAT_ADMIN_USERNAME || secrets.CFCHAT_ADMIN_USERNAME }}
EDGECHAT_ADMIN_PASSWORD: ${{ secrets.EDGECHAT_ADMIN_PASSWORD || secrets.CFCHAT_ADMIN_PASSWORD }}
EDGECHAT_ADMIN_DISPLAY_NAME: ${{ secrets.EDGECHAT_ADMIN_DISPLAY_NAME || secrets.CFCHAT_ADMIN_DISPLAY_NAME }}
shell: bash
run: |
if [[ -n "$EDGECHAT_ADMIN_USERNAME" && -n "$EDGECHAT_ADMIN_PASSWORD" ]]; then
node .github/scripts/sync-admin-credentials.ts
elif [[ -n "$EDGECHAT_ADMIN_USERNAME" || -n "$EDGECHAT_ADMIN_PASSWORD" ]]; then
echo "Configure both EDGECHAT_ADMIN_USERNAME and EDGECHAT_ADMIN_PASSWORD Repository Secrets."
exit 1
else
echo "Skipping administrator credential sync; admin credentials were not provided."
fi
- name: Verify login administrator
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_D1_DATABASE_ID: ${{ steps.ensure_resources.outputs.d1_database_id }}
run: node .github/scripts/prepare-d1-migrations.mjs --verify-admin
# 先注入 S3,再发布无 R2 binding 的版本;缺省分支不执行任何额外云操作。
- name: Configure custom S3 secret (optional)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_S3: ${{ secrets.EDGECHAT_S3 }}
shell: bash
run: |
if [[ -n "${EDGECHAT_S3//[[:space:]]/}" && "${EDGECHAT_S3//[[:space:]]/}" != "r2" ]]; then
printf '%s' "$EDGECHAT_S3" | npx wrangler secret put EDGECHAT_S3 --config wrangler.ci.toml
fi
- name: Prepare Worker encryption secret
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_WORKER_NAME: cfchat
EDGECHAT_SECRETS_FILE: .tmp/worker-secrets.json
EDGECHAT_ENCRYPTION_KEYRING: ${{ secrets.EDGECHAT_ENCRYPTION_KEYRING }}
EDGECHAT_APPLY_ENCRYPTION_KEYRING: ${{ github.event_name == 'workflow_dispatch' && inputs.apply_encryption_keyring || false }}
EDGECHAT_ROTATE_ENCRYPTION_KEY: ${{ github.event_name == 'workflow_dispatch' && inputs.rotate_encryption_key || false }}
run: node .github/scripts/prepare-worker-encryption-secret.mjs
- name: Validate optional TURN configuration
env:
EDGECHAT_TURN_KEY_ID: ${{ secrets.EDGECHAT_TURN_KEY_ID }}
EDGECHAT_TURN_API_TOKEN: ${{ secrets.EDGECHAT_TURN_API_TOKEN }}
shell: bash
run: |
if [[ -n "$EDGECHAT_TURN_KEY_ID" || -n "$EDGECHAT_TURN_API_TOKEN" ]]; then
test -n "$EDGECHAT_TURN_KEY_ID" && test -n "$EDGECHAT_TURN_API_TOKEN"
fi
- name: Deploy worker
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
shell: bash
run: |
if [[ -f .tmp/worker-secrets.json ]]; then
npx wrangler deploy --config wrangler.ci.toml --secrets-file .tmp/worker-secrets.json
else
npx wrangler deploy --config wrangler.ci.toml
fi
# 切回 R2 时先发布 FILES binding,避免旧 S3 版本先失去可用后端。
- name: Restore native R2 selection (optional)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_S3: ${{ secrets.EDGECHAT_S3 }}
shell: bash
run: |
if [[ "${EDGECHAT_S3//[[:space:]]/}" == "r2" ]]; then
printf '%s' 'r2' | npx wrangler secret put EDGECHAT_S3 --config wrangler.ci.toml
fi
- name: Configure direct transfers for the deployed R2 bucket
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
R2_BUCKET_NAME: ${{ steps.ensure_resources.outputs.r2_bucket_name }}
EDGECHAT_R2_ORIGINS: ${{ vars.EDGECHAT_R2_ORIGINS }}
if: steps.ensure_resources.outputs.r2_available == 'true'
run: node .github/scripts/configure-r2-direct.mjs wrangler.ci.toml
- name: Configure TURN secrets (optional)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_TURN_KEY_ID: ${{ secrets.EDGECHAT_TURN_KEY_ID }}
EDGECHAT_TURN_API_TOKEN: ${{ secrets.EDGECHAT_TURN_API_TOKEN }}
shell: bash
run: |
if [[ -n "$EDGECHAT_TURN_KEY_ID" || -n "$EDGECHAT_TURN_API_TOKEN" ]]; then
test -n "$EDGECHAT_TURN_KEY_ID" && test -n "$EDGECHAT_TURN_API_TOKEN"
printf '%s' "$EDGECHAT_TURN_KEY_ID" | npx wrangler secret put EDGECHAT_TURN_KEY_ID --config wrangler.ci.toml
printf '%s' "$EDGECHAT_TURN_API_TOKEN" | npx wrangler secret put EDGECHAT_TURN_API_TOKEN --config wrangler.ci.toml
fi
- name: Configure Turnstile secret (optional)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
EDGECHAT_TURNSTILE_SECRET_KEY: ${{ secrets.EDGECHAT_TURNSTILE_SECRET_KEY }}
shell: bash
run: |
if [[ -n "$EDGECHAT_TURNSTILE_SECRET_KEY" ]]; then
printf '%s' "$EDGECHAT_TURNSTILE_SECRET_KEY" | npx wrangler secret put EDGECHAT_TURNSTILE_SECRET_KEY --config wrangler.ci.toml
fi
- name: Remove temporary secret files
if: always()
shell: bash
run: rm -f .tmp/worker-secrets.json