Skip to content

Commit bf1bcb7

Browse files
fix github action deploy error issue
1 parent 14798e8 commit bf1bcb7

File tree

2 files changed

+109
-18
lines changed

2 files changed

+109
-18
lines changed

.github/workflows/deployToGithubPages.yml

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,53 @@ name: Deploy to GitHub Pages
33
on:
44
push:
55
branches: [ "master" ]
6-
# Allow manual trigger
76
workflow_dispatch:
87

8+
# Official Pages actions need these explicit permissions
99
permissions:
10-
contents: write
10+
contents: read # to checkout
11+
pages: write # to deploy
12+
id-token: write # required by deploy-pages for OIDC
13+
14+
concurrency:
15+
group: pages-deploy
16+
cancel-in-progress: false
1117

1218
jobs:
13-
build-and-deploy:
19+
build:
1420
runs-on: ubuntu-latest
1521
steps:
16-
- uses: actions/checkout@v4
17-
with:
18-
persist-credentials: true
19-
token: ${{ secrets.GITHUB_TOKEN }}
22+
- name: Checkout
23+
uses: actions/checkout@v4
2024

2125
- name: Setup Node.js
2226
uses: actions/setup-node@v4
2327
with:
24-
node-version: '20'
25-
cache: 'npm'
28+
node-version: 20
29+
cache: 'npm'
2630

2731
- name: Install dependencies
2832
run: npm ci
2933

30-
- name: Build & Deploy to gh-pages
31-
env:
32-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
- name: Build (Vite)
35+
run: npm run build
36+
37+
- name: Include CNAME (if exists)
3338
run: |
34-
npm run deploy
39+
if [ -f CNAME ]; then cp CNAME dist/CNAME; fi
3540
36-
- name: Upload artifact (dist) for reference
37-
if: success()
38-
uses: actions/upload-artifact@v4
41+
- name: Upload artifact to Pages
42+
uses: actions/upload-pages-artifact@v3
3943
with:
40-
name: dist
41-
path: dist
44+
path: dist
45+
46+
deploy:
47+
environment:
48+
name: github-pages
49+
url: ${{ steps.deployment.outputs.page_url }}
50+
needs: build
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: Deploy to GitHub Pages
54+
id: deployment
55+
uses: actions/deploy-pages@v4

AI_Document.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,80 @@ npx serve dist # 打开 http://localhost:3000 或 serve 输出的端口
147147
3. 如果需要保留旧链接(/awesome-blogs/)做兼容,可在根目录放一个 `awesome-blogs/index.html`,里边用 `<meta http-equiv="refresh">` 方式 301/模拟跳转到根。
148148

149149
---
150+
151+
# 已迁移到官方 GitHub Pages Actions 工作流 (2025-09-15)
152+
153+
## 迁移动机
154+
原方案使用 `gh-pages` NPM 包直接 push 到 `gh-pages` 分支,需要处理:
155+
1. Git 认证(可能出现 `could not read Username`)。
156+
2. 手动保证 CNAME 复制。
157+
3. 无 Pages 环境保护(Environment URL、审计、Rollbacks 功能有限)。
158+
159+
官方 Actions 组合(`upload-pages-artifact` + `deploy-pages`)提供:
160+
- 内置 OIDC 签发,不需要手动注入 token 到 git remote。
161+
- 自动生成 Environment(`github-pages`)并暴露 URL。
162+
- 更稳定的缓存与最小权限(`contents: read`, `pages: write`)。
163+
164+
## 新工作流文件
165+
路径:`.github/workflows/deployToGithubPages.yml`
166+
167+
核心步骤:
168+
1. build Job:checkout → setup-node → npm ci → vite build → 复制 CNAME → 上传构建产物。
169+
2. deploy Job:使用 `actions/deploy-pages@v4` 部署上传的 artifact。
170+
171+
并发控制:
172+
```
173+
concurrency:
174+
group: pages-deploy
175+
cancel-in-progress: false
176+
```
177+
避免快速多次 push 时出现中途构建产物被覆盖的不一致状态。
178+
179+
## 权限说明
180+
```
181+
permissions:
182+
contents: read
183+
pages: write
184+
id-token: write
185+
```
186+
`id-token: write``deploy-pages` 通过 OIDC 获取短期访问令牌的必要权限。
187+
188+
## 自定义域名处理
189+
工作流中保留:
190+
```
191+
if [ -f CNAME ]; then cp CNAME dist/CNAME; fi
192+
```
193+
确保根目录 `CNAME` 被复制进部署目录。保持仓库根的 `CNAME` 文件即可。
194+
195+
## 本地与线上一致性验证
196+
1. 本地执行:`npm ci && npm run build`,检查 `dist/` 包含 `index.html` 与资源。
197+
2. 手动确认 `dist/CNAME`(若本地复制脚本也需要,可运行:`cp CNAME dist/CNAME`)。
198+
3. 推送到 `master` 后,在 Actions 页面查看:
199+
- build 任务成功并显示 artifact 上传。
200+
- deploy 任务显示 `✅ Deployed`,Environment 面板出现 URL。
201+
202+
## 旧脚本及依赖清理(可选)
203+
现在发布不再需要 `gh-pages` 包,可选择:
204+
1.`package.json` 删除 `gh-pages` 依赖。
205+
2. 修改 `scripts.deploy`(保留本地手动预览可改成仅构建):
206+
```jsonc
207+
"deploy": "npm run build"
208+
```
209+
3. 若想安全过渡,可暂时保留,一段时间后再移除。
210+
211+
## 如果仍想保留手动推送方式
212+
可新增一个单独脚本如:`"legacy:publish": "vite build && gh-pages -d dist"`,仅在本地或特殊场景使用。
213+
214+
## 回滚方式
215+
若官方工作流异常(罕见):
216+
1. 在 Settings → Pages 将 Source 切回 Branch: `gh-pages`
217+
2. 恢复旧 workflow(保存在 git 历史中)。
218+
219+
## 后续可增量增强
220+
- 添加 `actions/cache` 缓存 `~/.npm`(需注意 vite 和 Tailwind 版本变动)。
221+
- 在 build 前跑 `npm run lint` 或轻量验证脚本(比如校验 `blogs.json` 结构)。
222+
- 添加一个 `preview` workflow 用于 PR 预览(`actions/deploy-pages` 支持 PR 部署预览)。
223+
224+
如需继续删除 `gh-pages` 依赖或添加缓存优化告诉我即可。
225+
226+
---

0 commit comments

Comments
 (0)