Skip to content

Commit a42ab74

Browse files
committed
feat: 为文档部署工作流增加PR预览功能
1 parent e294822 commit a42ab74

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

.github/workflows/deploy-docs.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
name: Deploy Documentation to GitHub Pages
1+
name: Build and Deploy Documentation
22

33
on:
44
push:
55
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
68
workflow_dispatch:
79

810
permissions:
@@ -15,7 +17,7 @@ concurrency:
1517
cancel-in-progress: false
1618

1719
jobs:
18-
build-and-deploy:
20+
build:
1921
runs-on: ubuntu-latest
2022

2123
steps:
@@ -45,14 +47,34 @@ jobs:
4547
cd docs
4648
python build_multilang.py
4749
50+
- name: Upload build artifacts (for PR preview)
51+
uses: actions/upload-artifact@v4
52+
if: github.event_name == 'pull_request'
53+
with:
54+
name: documentation-preview
55+
path: docs/build/html
56+
retention-days: 7
57+
4858
- name: Setup Pages
4959
uses: actions/configure-pages@v4
60+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
5061

5162
- name: Upload Pages artifact
5263
uses: actions/upload-pages-artifact@v3
64+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
5365
with:
5466
path: docs/build/html
55-
67+
68+
deploy:
69+
needs: build
70+
runs-on: ubuntu-latest
71+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
72+
73+
environment:
74+
name: github-pages
75+
url: ${{ steps.deployment.outputs.page_url }}
76+
77+
steps:
5678
- name: Deploy to GitHub Pages
5779
id: deployment
5880
uses: actions/deploy-pages@v4

.github/workflows/pr-preview.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: PR Preview Comment
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Build and Deploy Documentation"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
pull-requests: write
11+
contents: read
12+
13+
jobs:
14+
comment:
15+
runs-on: ubuntu-latest
16+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
17+
18+
steps:
19+
- name: Comment on PR
20+
uses: actions/github-script@v7
21+
with:
22+
script: |
23+
// Get PR number from the workflow run
24+
const pullRequests = context.payload.workflow_run.pull_requests;
25+
if (!pullRequests || pullRequests.length === 0) {
26+
console.log('No pull requests found for this workflow run');
27+
return;
28+
}
29+
30+
const prNumber = pullRequests[0].number;
31+
const runId = context.payload.workflow_run.id;
32+
const repoOwner = context.repo.owner;
33+
const repoName = context.repo.repo;
34+
35+
const comment = `## 📚 Documentation Preview
36+
37+
✅ **Build Status**: Success
38+
🔗 **Artifacts**: [Download documentation preview](https://github.com/${repoOwner}/${repoName}/actions/runs/${runId})
39+
40+
### 📋 Build Summary
41+
- **Multi-language build**: Completed
42+
- **Chinese docs**: Generated
43+
- **English docs**: Generated
44+
- **Build artifacts**: Available for 7 days
45+
46+
### 🚀 Next Steps
47+
After merging this PR, the documentation will be automatically deployed to:
48+
- **Chinese**: https://${repoOwner}.github.io/${repoName}/
49+
- **English**: https://${repoOwner}.github.io/${repoName}/en/
50+
51+
---
52+
*This comment was automatically generated by GitHub Actions*`;
53+
54+
await github.rest.issues.createComment({
55+
issue_number: prNumber,
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
body: comment
59+
});

DEPLOYMENT.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
### 部署流程
1919

20-
当你推送代码到 `main``master` 分支时,GitHub Actions 会自动:
20+
#### PR 构建验证
21+
当你创建或更新 Pull Request 时,GitHub Actions 会自动:
2122

2223
1. **安装依赖**: 安装 Python 和 Sphinx 相关包
2324
2. **构建文档**: 运行 `build_multilang.py` 生成多语言文档
24-
3. **部署到 Pages**: 将构建结果发布到 GitHub Pages
25+
3. **上传预览**: 将构建结果作为 artifacts 上传(保留7天)
26+
4. **状态检查**: 在PR中显示构建状态和预览链接
27+
28+
#### 生产部署
29+
当你推送代码到 `main``master` 分支时,GitHub Actions 会自动:
30+
31+
1. **构建文档**: 执行完整的多语言构建
32+
2. **部署到 Pages**: 将构建结果发布到 GitHub Pages
33+
3. **更新网站**: 实时更新在线文档
2534

2635
### 访问地址
2736

0 commit comments

Comments
 (0)