Skip to content

Commit 5beb826

Browse files
committed
配置 GitHub Actions 自动化 CI/CD 流程
- 添加自动部署工作流 (.github/workflows/deploy.yml) - 添加 PR 预览工作流 (.github/workflows/preview.yml) - 配置 Hugo 构建环境和部署到 gh-pages 分支 - 添加 CNAME 文件和自定义域名支持 - 更新 README 包含 CI/CD 徽章和说明 - 支持手动触发和测试模式 - 添加构建统计和错误处理 - 支持 PR 预览和自动评论 触发条件: - 推送到 master 分支自动部署 - PR 提交自动构建测试 - 支持手动触发部署
1 parent e0e9f20 commit 5beb826

File tree

5 files changed

+367
-0
lines changed

5 files changed

+367
-0
lines changed

.github/workflows/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# NoSQL-CN 社区官网 CI/CD 徽章
2+
3+
## 工作流状态
4+
5+
[![Deploy Hugo Site to GitHub Pages](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml)
6+
[![PR Preview](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml)
7+
8+
## 徽章用法
9+
10+
可以在 README.md 中添加以下徽章:
11+
12+
```markdown
13+
[![Deploy Status](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml)
14+
[![PR Preview](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml)
15+
```
16+
17+
## 触发条件
18+
19+
### 自动部署
20+
- **推送**`master` 分支(包含相关文件变更)
21+
- **PR 合并**`master` 分支
22+
- **手动触发**(GitHub Actions 页面)
23+
24+
### PR 预览
25+
- **PR 创建**
26+
- **PR 更新**
27+
- **PR 重新打开**
28+
29+
## 监控
30+
31+
- 工作流运行状态: https://github.com/nosql-cn/nosql-cn.org/actions
32+
- 部署状态: https://github.com/nosql-cn/nosql-cn.org/deployments

.github/workflows/deploy.yml

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
name: Deploy Hugo Site to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- 'content/**'
9+
- 'layouts/**'
10+
- 'static/**'
11+
- 'themes/**'
12+
- 'config/**'
13+
- '*.toml'
14+
- '*.yaml'
15+
- 'package.json'
16+
- 'postcss.config.js'
17+
pull_request:
18+
branches:
19+
- master
20+
paths:
21+
- 'content/**'
22+
- 'layouts/**'
23+
- 'static/**'
24+
- 'themes/**'
25+
- 'config/**'
26+
- '*.toml'
27+
- '*.yaml'
28+
- 'package.json'
29+
- 'postcss.config.js'
30+
workflow_dispatch:
31+
inputs:
32+
test_mode:
33+
description: '测试模式(不部署)'
34+
required: false
35+
default: 'false'
36+
type: boolean
37+
38+
jobs:
39+
build-and-deploy:
40+
runs-on: ubuntu-latest
41+
if: (github.ref == 'refs/heads/master' && github.event_name == 'push') || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_mode != 'true')
42+
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v4
46+
with:
47+
submodules: recursive
48+
fetch-depth: 0
49+
50+
- name: Setup Hugo
51+
uses: peaceiris/actions-hugo@v2
52+
with:
53+
hugo-version: '0.148.2'
54+
extended: true
55+
56+
- name: Setup Node.js
57+
uses: actions/setup-node@v4
58+
with:
59+
node-version: '18'
60+
cache: 'npm'
61+
62+
- name: Install dependencies
63+
run: npm ci
64+
65+
- name: Build Hugo site
66+
run: hugo --environment production --minify
67+
env:
68+
HUGO_ENVIRONMENT: production
69+
70+
- name: Test build
71+
id: build-test
72+
run: |
73+
if [ ! -f "public/index.html" ]; then
74+
echo "❌ Build failed: index.html not found"
75+
exit 1
76+
fi
77+
78+
PAGE_COUNT=$(find public -name "*.html" | wc -l)
79+
FILE_COUNT=$(find public -type f | wc -l)
80+
TOTAL_SIZE=$(du -sh public | cut -f1)
81+
82+
echo "✅ Build successful"
83+
echo "📊 Build statistics:"
84+
echo " - HTML files: $PAGE_COUNT"
85+
echo " - Total files: $FILE_COUNT"
86+
echo " - Total size: $TOTAL_SIZE"
87+
88+
# 设置输出变量供后续步骤使用
89+
echo "page_count=$PAGE_COUNT" >> $GITHUB_OUTPUT
90+
echo "file_count=$FILE_COUNT" >> $GITHUB_OUTPUT
91+
echo "total_size=$TOTAL_SIZE" >> $GITHUB_OUTPUT
92+
93+
- name: Deploy to GitHub Pages
94+
uses: peaceiris/actions-gh-pages@v3
95+
with:
96+
github_token: ${{ secrets.GITHUB_TOKEN }}
97+
publish_dir: ./public
98+
destination_dir: ./
99+
keep_files: true
100+
commit_message: '🤖 自动部署网站更新
101+
102+
构建时间: ${{ github.event.head_commit.timestamp }}
103+
触发者: ${{ github.actor }}
104+
提交信息: ${{ github.event.head_commit.message }}
105+
106+
构建统计:
107+
- 页面数: ${{ steps.build-test.outputs.page_count }}
108+
- 文件数: ${{ steps.build-test.outputs.file_count }}
109+
- 总大小: ${{ steps.build-test.outputs.total_size }}
110+
111+
触发方式: ${{ github.event_name }}'
112+
113+
test-build:
114+
runs-on: ubuntu-latest
115+
if: github.event_name == 'pull_request'
116+
117+
steps:
118+
- name: Checkout repository
119+
uses: actions/checkout@v4
120+
with:
121+
submodules: recursive
122+
123+
- name: Setup Hugo
124+
uses: peaceiris/actions-hugo@v2
125+
with:
126+
hugo-version: '0.148.2'
127+
extended: true
128+
129+
- name: Setup Node.js
130+
uses: actions/setup-node@v4
131+
with:
132+
node-version: '18'
133+
cache: 'npm'
134+
135+
- name: Install dependencies
136+
run: npm ci
137+
138+
- name: Build Hugo site (test)
139+
run: hugo --environment production --minify
140+
env:
141+
HUGO_ENVIRONMENT: production
142+
143+
- name: Test build
144+
run: |
145+
if [ ! -f "public/index.html" ]; then
146+
echo "❌ Build failed: index.html not found"
147+
exit 1
148+
fi
149+
echo "✅ PR build test successful"
150+
echo "📊 Build statistics:"
151+
echo " - HTML files: $(find public -name "*.html" | wc -l)"
152+
echo " - Total files: $(find public -type f | wc -l)"
153+
echo " - Total size: $(du -sh public | cut -f1)"
154+
155+
- name: Comment PR with build status
156+
uses: actions/github-script@v7
157+
with:
158+
script: |
159+
const { context } = require('@actions/github');
160+
const { execSync } = require('child_process');
161+
162+
const pageCount = parseInt(execSync('find public -name "*.html" | wc -l').toString());
163+
const fileCount = parseInt(execSync('find public -type f | wc -l').toString());
164+
const size = execSync('du -sh public | cut -f1').toString().trim();
165+
166+
const comment = `
167+
🤖 **Hugo 构建测试结果**
168+
169+
✅ **构建成功**
170+
171+
📊 **构建统计**:
172+
- 页面数: ${pageCount}
173+
- 文件数: ${fileCount}
174+
- 总大小: ${size}
175+
176+
🎯 **下一步**:
177+
- 如果 PR 被合并到 master 分支,网站将自动部署
178+
- 部署地址: https://nosql-cn.org
179+
`;
180+
181+
github.rest.issues.createComment({
182+
issue_number: context.issue.number,
183+
owner: context.repo.owner,
184+
repo: context.repo.repo,
185+
body: comment
186+
});

.github/workflows/preview.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: PR Preview
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
types: [opened, synchronize, reopened]
8+
9+
jobs:
10+
preview:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
with:
17+
submodules: recursive
18+
19+
- name: Setup Hugo
20+
uses: peaceiris/actions-hugo@v2
21+
with:
22+
hugo-version: '0.148.2'
23+
extended: true
24+
25+
- name: Setup Node.js
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: '18'
29+
cache: 'npm'
30+
31+
- name: Install dependencies
32+
run: npm ci
33+
34+
- name: Build Hugo site
35+
run: hugo --environment production --minify
36+
env:
37+
HUGO_ENVIRONMENT: production
38+
39+
- name: Upload preview artifacts
40+
uses: actions/upload-artifact@v4
41+
with:
42+
name: preview-site
43+
path: public/
44+
retention-days: 7
45+
46+
- name: Add PR comment with build info
47+
uses: actions/github-script@v7
48+
with:
49+
script: |
50+
const { context } = require('@actions/github');
51+
const { execSync } = require('child_process');
52+
53+
const pageCount = parseInt(execSync('find public -name "*.html" | wc -l').toString());
54+
const fileCount = parseInt(execSync('find public -type f | wc -l').toString());
55+
const size = execSync('du -sh public | cut -f1').toString().trim();
56+
57+
const changedFiles = context.payload.pull_request.changed_files;
58+
const additions = context.payload.pull_request.additions;
59+
const deletions = context.payload.pull_request.deletions;
60+
61+
let fileChanges = '';
62+
if (changedFiles > 0) {
63+
fileChanges = `
64+
📁 **文件变更**:
65+
- 修改文件数: ${changedFiles}
66+
- 新增行数: ${additions}
67+
- 删除行数: ${deletions}`;
68+
}
69+
70+
const comment = `
71+
🎯 **PR 构建预览**
72+
73+
✅ **构建成功** ${fileChanges}
74+
75+
📊 **构建统计**:
76+
- 页面数: ${pageCount}
77+
- 文件数: ${fileCount}
78+
- 总大小: ${size}
79+
80+
🚀 **预览方式**:
81+
1. 下载构建产物(Artifacts)
82+
2. 在本地运行 \`hugo server\` 预览
83+
84+
⚡ **自动部署**:
85+
- PR 合并到 master 分支后将自动部署到: https://nosql-cn.org
86+
`;
87+
88+
try {
89+
await github.rest.issues.createComment({
90+
issue_number: context.issue.number,
91+
owner: context.repo.owner,
92+
repo: context.repo.repo,
93+
body: comment
94+
});
95+
} catch (error) {
96+
console.log('Comment already exists or failed to create:', error.message);
97+
}

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
> 存储领域的从业者和爱好者自发组织的民间技术交流社区 - **无广告、零推广、不包装**
44
5+
[![Deploy Status](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/deploy.yml)
6+
[![PR Preview](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml/badge.svg)](https://github.com/nosql-cn/nosql-cn.org/actions/workflows/preview.yml)
7+
58
本项目是基于 [Hugo](https://gohugo.io/) 构建的 NoSQL-CN 社区官网,旨在为存储技术爱好者提供一个纯粹的技术交流平台。
69

710
## 目录结构简介
@@ -149,7 +152,55 @@ hugo --environment production
149152
- **多语言支持?**
150153
配置见 `config/_default/languages.yaml`,内容见 `content/zh/``content/en/` 等。
151154

155+
## 自动化 CI/CD
156+
157+
### 🚀 自动部署
158+
159+
本项目配置了 GitHub Actions 自动化部署:
160+
161+
**触发条件**
162+
- ✅ 推送到 `master` 分支(内容相关文件变更)
163+
- ✅ PR 合并到 `master` 分支
164+
- ✅ 手动触发工作流
165+
166+
**部署流程**
167+
1. **自动构建**:在云端使用 Hugo 构建静态网站
168+
2. **自动测试**:验证构建结果和关键页面
169+
3. **自动部署**:推送到 `gh-pages` 分支
170+
4. **自动生效**:GitHub Pages 自动发布
171+
172+
**监控地址**
173+
- 工作流状态:https://github.com/nosql-cn/nosql-cn.org/actions
174+
- 部署状态:https://github.com/nosql-cn/nosql-cn.org/deployments
175+
176+
### 📋 PR 预览
177+
178+
当用户提交 PR 时,会自动:
179+
180+
1. **构建测试**:验证 PR 的构建是否成功
181+
2. **统计信息**:显示构建统计(页面数、文件数、大小)
182+
3. **PR 评论**:自动在 PR 中评论构建结果
183+
4. **产物下载**:提供构建产物供预览
184+
185+
### 🛠️ 本地开发
186+
187+
```bash
188+
# 克隆仓库
189+
git clone [email protected]:nosql-cn/nosql-cn.org.git
190+
cd nosql-cn.org
191+
192+
# 安装依赖
193+
npm install
194+
195+
# 本地开发
196+
hugo server --disableFastRender
197+
198+
# 构建测试
199+
hugo --environment production
200+
```
201+
152202
## 参考文档
153203

154204
- [Hugo 官方文档](https://gohugo.io/documentation/)
155205
- [dot-org-hugo-theme 主题文档](themes/dot-org-hugo-theme/README.md)
206+
- [GitHub Actions 文档](https://docs.github.com/en/actions)

static/CNAME

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nosql-cn.org

0 commit comments

Comments
 (0)