Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 340 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ og-report.json

# 测试临时目录
tests/.tmp/

# 部署工作流产出的报告与状态(不入库:报告每次变化,状态由 actions/cache 持久化)
link-health-report.json
link-health-state.json
build-summary.md
Loading
Loading