Repository navigation
106 lines (90 loc) · 3.94 KB
/
Copy pathdeploy.yml
File metadata and controls
106 lines (90 loc) · 3.94 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
name: Deploy
# 部署归用户关口:只允许手动触发,且必须显式确认。
# 凭据未配齐时 preflight 明确报出缺哪把 key,并让本次 run 结论为 failure(红);
# deploy job 直接跳过(不生成 secrets.json、不绕过、不降级校验)。
on:
workflow_dispatch:
inputs:
confirm:
description: '输入 deploy 确认执行生产部署'
required: true
default: ''
type: string
permissions:
contents: read
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
preflight:
name: 凭据关口
runs-on: ubuntu-latest
outputs:
creds_ready: ${{ steps.check.outputs.creds_ready }}
steps:
- uses: actions/checkout@v7
- name: 逐键校验(SETUP_SECRET / JWT_SECRET / CLOUDFLARE_API_TOKEN / CLOUDFLARE_ACCOUNT_ID)
id: check
env:
SETUP_SECRET: ${{ secrets.SETUP_SECRET }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: |
# 通过 → creds_ready=true;
# 不通过 → creds_ready=false + 本步骤退出码 1(job/run 结论为 failure,缺凭据绝不能显示绿色)。
# 脚本只输出键名与状态,绝不输出任何密钥值。
if out="$(bash scripts/ci/gen-secrets-file.sh --check 2>&1)"; then
echo "$out"
echo "creds_ready=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "creds_ready=false" >> "$GITHUB_OUTPUT"
echo "$out"
missing="$(printf '%s\n' "$out" | sed -n 's/.*缺少:\([^(]*\)(失败关闭.*/\1/p' | head -1)"
echo "::error::凭据关口未通过(失败关闭):缺少 ${missing:-未知}。本次 run 标记为失败,不生成 secrets.json,deploy job 不执行。请先在仓库 Settings → Secrets and variables 配齐上述 key,再重跑本工作流。"
exit 1
# 放在凭据校验之后并带 always():缺凭据时也照样校验确认输入,两条关口都能报出来;两者任一不通过 run 都是红。
- name: 确认输入
if: always() && inputs.confirm != 'deploy'
run: |
echo "::error::未确认部署:请在手动触发时把 confirm 填成 deploy。"
exit 1
deploy:
name: wrangler deploy
needs: preflight
if: needs.preflight.outputs.creds_ready == 'true' && inputs.confirm == 'deploy'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v7
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v7
with:
node-version: '24'
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
# wrangler.toml 的 [assets] directory = "./web/dist" 是外置目录,
# 必须先构建前端,否则 wrangler deploy 会因目录不存在直接失败。
- name: 构建前端(web/dist)
run: pnpm build:web
- name: 校验前端构建产物存在
run: |
if [ ! -f web/dist/index.html ]; then
echo "::error::web/dist/index.html 不存在,前端构建未产出,拒绝部署(wrangler.toml 的 assets.directory 无法满足)。"
exit 1
fi
- name: 生成 $RUNNER_TEMP/secrets.json(0600,逐键非空,失败关闭)
env:
SETUP_SECRET: ${{ secrets.SETUP_SECRET }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: bash scripts/ci/gen-secrets-file.sh
- name: Deploy
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: pnpm exec wrangler deploy --secrets-file "$RUNNER_TEMP/secrets.json"
- name: 清理明文 secrets 文件
if: always()
run: rm -f "$RUNNER_TEMP/secrets.json"