Repository navigation
340 lines (304 loc) · 12.9 KB
/
Copy pathdeploy.yml
File metadata and controls
340 lines (304 loc) · 12.9 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
name: 双线部署
# 双线部署(SPEC §0 / §8.1 / §9.2 / §9.3):
# 同一提交 → 同一份静态产物(dist/)→ 两条独立发布通道
# ① GitHub Pages 主站(来源 = GitHub Actions,自定义域名栏必须留空)
# ② Cloudflare Pages 副线 hub.oneboy.app(deploy hook 触发同版重建)
# 一侧失败不阻塞另一侧:两个发布 job 互相不依赖,各自独立报错(§0 / §9.2)。
#
# 触发:
# - main 有新提交时(与 CF Pages Git 集成的生产分支一致)
# - 每日定时重建(数据刷新 + 链接健康巡检 + 构建摘要,§9.2 / §9.3)
# - 手动(workflow_dispatch):dry_run 演练模式 / 注入单侧失败演练
#
# 密钥红线:Cloudflare Pages deploy hook URL 只从 GitHub Secrets 读取
# (名称 CLOUDFLARE_PAGES_DEPLOY_HOOK),绝不写进仓库文件、README 或日志。
on:
push:
branches: [main]
schedule:
# UTC 19:17 = 北京时间 03:17(每日一次,§9.2)
- cron: '17 19 * * *'
workflow_dispatch:
inputs:
dry_run:
description: '演练模式:只构建 + 出摘要,不实际发布(不调用 Pages 发布动作、不调用 deploy hook)'
type: boolean
default: false
simulate_failure:
description: '演练:故意让某一侧发布失败,验证另一侧不受影响(仅 dry_run 模式可用)'
type: choice
options: [none, pages, cloudflare]
default: none
permissions:
contents: read
concurrency:
group: deploy-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
jobs:
build:
name: 构建(同一份静态产物)
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v7
with:
# 页脚版本号来自 git describe(§0 / §12 第 8 条),需要完整历史
fetch-depth: 0
persist-credentials: false
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: pnpm
- name: 解析运行模式(演练 / 注入失败侧)
id: mode
env:
INPUT_DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
INPUT_SIMULATE_FAILURE: ${{ github.event.inputs.simulate_failure || 'none' }}
run: |
set -euo pipefail
dry_run="${INPUT_DRY_RUN}"
simulate="${INPUT_SIMULATE_FAILURE}"
case "$dry_run" in
true|false) ;;
*) echo "::error::dry_run 取值非法:$dry_run"; exit 1 ;;
esac
case "$simulate" in
none|pages|cloudflare) ;;
*) echo "::error::simulate_failure 取值非法:$simulate"; exit 1 ;;
esac
if [ "$simulate" != "none" ] && [ "$dry_run" != "true" ]; then
echo "::error::simulate_failure 只允许在 dry_run=true 的演练模式下使用(正式发布不许注入失败)"
exit 1
fi
echo "运行模式:dry_run=$dry_run,simulate_failure=$simulate(事件 $GITHUB_EVENT_NAME)"
- name: 安装依赖
run: pnpm install --frozen-lockfile
- name: 密钥红线自检(deploy hook URL 不得出现在仓库任何文件里)
run: |
set -euo pipefail
# 排除工作流目录自身(本文件的正则出现在注释里),只扫会被提交的文件
if hits=$(grep -rIl --exclude-dir=.git --exclude-dir=node_modules --exclude-dir=dist \
--exclude-dir=.github -e 'deploy_hooks' . || true); then :; fi
if [ -n "${hits:-}" ]; then
echo "::error::检测到疑似 Cloudflare deploy hook URL 被写进仓库文件(只允许放 GitHub Secrets):"
echo "$hits"
exit 1
fi
echo "密钥红线自检通过:仓库文件里没有 deploy hook URL"
- name: 构建(§9.1:一条命令产出 dist/)
run: pnpm build
env:
# 只读公开数据足够;不要求、也不允许任何 PAT 入仓(§9.2)
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 记录版本号(git describe,与页脚同源;取不到就中断)
id: version
run: |
set -euo pipefail
# 与 src/lib/build-info.ts 同一命令(不带 --dirty:构建本身会改写自动字段缓存)
if ! version=$(git describe --tags --always 2>&1); then
echo "::error::git describe 失败($version)——版本号是 §12 第 8 条硬性项,请确认 checkout 带了 fetch-depth: 0"
exit 1
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "版本号:$version"
- name: 产物自检(双线共用同一份 dist/)
run: |
set -euo pipefail
test -f dist/index.html
test -f dist/sitemap.xml
test -f dist/robots.txt
if [ -f dist/CNAME ]; then
echo "::error::dist/ 里出现了 CNAME:GitHub Pages 自定义域名栏必须留空(§0)"
exit 1
fi
version=$(git describe --tags --always)
if ! grep -rqF -- "$version" dist/index.html; then
echo "::error::页脚版本号未注入:dist/index.html 里找不到 $version(§12 第 8 条)"
exit 1
fi
echo "静态页 $(find dist -name 'index.html' | wc -l) 个,产物目录 dist/"
echo "页脚版本号已注入:$version"
- name: 上传站点产物(两侧发布共用)
uses: actions/upload-artifact@v7
with:
name: site
path: dist
retention-days: 7
- name: 上传构建报告(抓取结果 + 缓存文件,供摘要与维护者取用)
uses: actions/upload-artifact@v7
with:
name: reports
path: |
github-meta-report.json
src/data/cache/github-meta.json
retention-days: 7
if-no-files-found: warn
link-health:
name: 链接健康巡检(§9.3)
runs-on: ubuntu-latest
needs: build
# §9.3:只在每日重建(定时)与手动演练时巡检,日常推送不打扰第三方站点
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: pnpm
- name: 安装依赖
run: pnpm install --frozen-lockfile
- name: 恢复上次的连续失败计数(不写入仓库)
uses: actions/cache@v6
with:
path: link-health-state.json
key: link-health-${{ github.run_id }}
restore-keys: |
link-health-
- name: 巡检全部外链(只出摘要,不硬拦、不改数据)
run: pnpm health:links
- name: 上传链接健康报告
uses: actions/upload-artifact@v7
with:
name: link-health
path: link-health-report.json
retention-days: 7
if-no-files-found: warn
publish-github-pages:
name: 发布 GitHub Pages 主站
runs-on: ubuntu-latest
needs: build
# 与 CF 侧完全独立:只依赖 build,一侧失败不影响另一侧(§0 / §9.2)
if: always() && needs.build.result == 'success'
permissions:
contents: read
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
env:
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
SIMULATE_FAILURE: ${{ github.event.inputs.simulate_failure || 'none' }}
steps:
- name: 下载站点产物
uses: actions/download-artifact@v8
with:
name: site
path: dist
- name: 演练|模拟发布 GitHub Pages(不调用官方发布动作)
if: env.DRY_RUN == 'true'
run: |
set -euo pipefail
test -f dist/index.html
echo "演练:本应把 dist/($(find dist -name 'index.html' | wc -l) 个页面)作为 Pages 产物发布,来源 = GitHub Actions"
echo "演练:未调用 actions/configure-pages / upload-pages-artifact / deploy-pages"
if [ "$SIMULATE_FAILURE" = "pages" ]; then
echo "::error::演练注入:GitHub Pages 侧发布失败(另一侧按独立 job 继续)"
exit 1
fi
echo "演练:GitHub Pages 侧发布成功(模拟)"
- name: 配置 Pages(来源 = GitHub Actions)
if: env.DRY_RUN != 'true'
uses: actions/configure-pages@v6
- name: 上传 Pages 产物
if: env.DRY_RUN != 'true'
uses: actions/upload-pages-artifact@v5
with:
path: dist
- name: 发布(官方 Pages 发布动作)
id: deployment
if: env.DRY_RUN != 'true'
uses: actions/deploy-pages@v5
publish-cloudflare-pages:
name: 触发 Cloudflare Pages 副线重建
runs-on: ubuntu-latest
needs: build
# 与 Pages 侧完全独立:只依赖 build,一侧失败不影响另一侧(§0 / §9.2)
if: always() && needs.build.result == 'success'
env:
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
SIMULATE_FAILURE: ${{ github.event.inputs.simulate_failure || 'none' }}
steps:
- name: 演练|模拟触发副线重建(不调用 deploy hook)
if: env.DRY_RUN == 'true'
run: |
set -euo pipefail
echo "演练:本应用同一提交 POST Cloudflare Pages deploy hook(URL 只来自 GitHub Secrets)"
echo "演练:未读取、未打印 CLOUDFLARE_PAGES_DEPLOY_HOOK"
if [ "$SIMULATE_FAILURE" = "cloudflare" ]; then
echo "::error::演练注入:Cloudflare Pages 侧触发失败(另一侧按独立 job 继续)"
exit 1
fi
echo "演练:Cloudflare Pages 侧触发成功(模拟)"
- name: 触发副线同版重建(deploy hook,URL 只从 GitHub Secrets 读取)
if: env.DRY_RUN != 'true'
env:
CLOUDFLARE_PAGES_DEPLOY_HOOK: ${{ secrets.CLOUDFLARE_PAGES_DEPLOY_HOOK }}
run: |
set -euo pipefail
if [ -z "${CLOUDFLARE_PAGES_DEPLOY_HOOK:-}" ]; then
echo "::error::缺少 GitHub Secrets:CLOUDFLARE_PAGES_DEPLOY_HOOK(Cloudflare Pages 项目 → Settings → Builds & deployments → Deploy hooks 生成后填进 Secrets;绝不写进仓库文件)"
exit 1
fi
# 不回显 URL(密钥红线):只打印状态码
status=$(curl -sS -o /dev/null -w '%{http_code}' \
--retry 2 --retry-delay 5 --max-time 30 \
-X POST "$CLOUDFLARE_PAGES_DEPLOY_HOOK" || echo '000')
echo "deploy hook 响应:HTTP $status"
case "$status" in
2??) echo "已触发 Cloudflare Pages 副线重建(同提交、同版本号)" ;;
*)
echo "::error::deploy hook 触发失败(HTTP $status)——本侧独立报错,不影响 GitHub Pages 侧"
exit 1
;;
esac
summary:
name: 构建摘要(§9.2 / §9.3 / §12 第 7 条)
runs-on: ubuntu-latest
needs: [build, link-health, publish-github-pages, publish-cloudflare-pages]
if: always()
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: pnpm
- name: 安装依赖
run: pnpm install --frozen-lockfile
- name: 下载构建报告
uses: actions/download-artifact@v8
with:
name: reports
path: .
- name: 下载链接健康报告(推送构建不跑巡检,故允许缺失)
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: link-health
path: .
- name: 生成构建摘要(自动字段刷新结果 + 异常项目清单 + 链接健康 + 两侧发布结果)
env:
HUB_META_REPORT: github-meta-report.json
HUB_LINK_REPORT: link-health-report.json
HUB_LINK_EXPECTED: ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && '1' || '0' }}
HUB_VERSION: ${{ needs.build.outputs.version }}
PUBLISH_DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
PUBLISH_SIMULATE_FAILURE: ${{ github.event.inputs.simulate_failure || 'none' }}
PUBLISH_GITHUB_PAGES_RESULT: ${{ needs.publish-github-pages.result }}
PUBLISH_CLOUDFLARE_RESULT: ${{ needs.publish-cloudflare-pages.result }}
run: pnpm summary:build
- name: 上传构建摘要
uses: actions/upload-artifact@v7
with:
name: build-summary
path: build-summary.md
retention-days: 7
if-no-files-found: warn