Hexo 博客使用 GitHub Actions 自动部署到 GitHub Pages 完整教程

本教程基于:Hexo 7.x + Fluid 主题(npm 安装方式)+ GitHub Actions + 跨仓库部署到 GitHub Pages


一、整体架构说明

本方案使用两个 GitHub 仓库分离源码与静态文件:

1
2
3
4
5
6
7
my-blog-source(私有/公开)        iomelons.github.io(公开)
├── source/_posts/ → ├── index.html
├── themes/ → ├── archives/
├── _config.yml → ├── css/
└── package.json → └── js/
源码仓库 Pages 仓库
(你写文章、改配置) (存放编译后的静态文件)

工作流程:

  1. 你在本地写文章,git pushmy-blog-sourcemain 分支
  2. GitHub Actions 自动触发,安装依赖、运行 hexo generate
  3. public/ 目录的静态文件推送到 iomelons.github.iomain 分支
  4. GitHub Pages 自动发布,博客更新完成

二、前置准备

2.1 安装本地环境

1
2
3
4
5
6
7
8
# 安装 Node.js(推荐 v18 LTS)
# 下载地址:https://nodejs.org/

# 安装 Hexo CLI
npm install -g hexo-cli

# 验证安装
hexo version

2.2 初始化 Hexo 项目

1
2
3
4
5
6
7
8
9
10
11
# 新建博客项目
hexo init my-blog-source
cd my-blog-source
npm install

# 安装 Fluid 主题(推荐 npm 方式,无需手动 clone)
npm install hexo-theme-fluid

# 验证本地预览
hexo server
# 浏览器访问 http://localhost:4000

2.3 创建 GitHub 仓库

在 GitHub 上创建两个仓库:

仓库名 说明 建议可见性
my-blog-source 存放 Hexo 源码 私有或公开均可
your-username.github.io 存放编译后的静态文件 必须公开

your-username.github.io 命名格式固定,将作为 GitHub Pages 的默认域名。


三、配置文件详解

3.1 Hexo 根配置 _config.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 站点信息
title: 我的博客
subtitle: ''
description: '个人技术博客'
keywords:
author: 你的名字
language: zh-CN
timezone: 'Asia/Shanghai'

# 访问地址(必须填写,影响 RSS、sitemap 等生成)
url: https://your-username.github.io
root: /

# 文章目录
source_dir: source
public_dir: public

# 主题(使用 npm 安装的 Fluid)
theme: fluid

# 不需要 deploy 配置,部署完全由 GitHub Actions 负责
# 删除或注释掉原来的 deploy 配置段落

3.2 Fluid 主题配置 _config.fluid.yml

在项目根目录新建 _config.fluid.yml,**不要直接修改 themes/fluid/_config.yml**,这样升级主题时不会丢失自定义配置。

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
#---------------------------
# 导航栏
#---------------------------
navbar:
blog_title: "你的博客名" # 左上角显示的名称
menu:
- { key: "home", link: "/", icon: "iconfont icon-home-fill" }
- { key: "archive", link: "/archives/", icon: "iconfont icon-archive-fill" }
- { key: "about", link: "/about/", icon: "iconfont icon-user-fill" }

#---------------------------
# 首页 Banner
#---------------------------
index:
banner_img: https://fastly.jsdelivr.net/gh/fluid-dev/static@master/hexo-theme-fluid/wallpaper.jpg
banner_img_height: 80
slogan:
enable: true
text: "你的个性签名"

#---------------------------
# 文章页
#---------------------------
post:
banner_img_height: 60
math:
enable: false # 不写数学公式则关闭,减少加载资源
mermaid:
enable: false # 不用流程图则关闭

#---------------------------
# 页脚
#---------------------------
footer:
content: '由 <a href="https://hexo.io" target="_blank">Hexo</a> 驱动,主题使用 <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank">Fluid</a>'

#---------------------------
# 评论(可选,按需开启)
#---------------------------
# comment:
# enable: true
# type: giscus # 支持 giscus / utterances / waline 等

完整配置项参考 Fluid 官方文档:https://hexo.fluid-dev.com/docs/guide/

3.3 确认 package.json

根目录的 package.json 应包含所有必要依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"name": "my-blog-source",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "hexo generate",
"clean": "hexo clean",
"server": "hexo server"
},
"dependencies": {
"hexo": "^7.0.0",
"hexo-generator-archive": "^2.0.0",
"hexo-generator-category": "^2.0.0",
"hexo-generator-index": "^3.0.0",
"hexo-generator-tag": "^2.0.0",
"hexo-renderer-ejs": "^2.0.0",
"hexo-renderer-marked": "^6.0.0",
"hexo-renderer-stylus": "^3.0.0",
"hexo-server": "^3.0.0",
"hexo-theme-fluid": "^1.9.8"
}
}

依赖安装完后,务必生成 lock 文件并提交

1
2
3
npm install
git add package.json package-lock.json
git commit -m "chore: add package-lock.json"

四、配置 GitHub Personal Access Token

GitHub Actions 需要有权限向另一个仓库(your-username.github.io)推送代码,需要配置 Personal Access Token(PAT)。

4.1 生成 Token

  1. 登录 GitHub → 右上角头像 → Settings
  2. 左侧菜单最底部 → Developer settings
  3. Personal access tokensTokens (classic)Generate new token
  4. 填写说明,例如 hexo-deploy
  5. 勾选权限:repo(完整勾选,包含子选项)
  6. 点击 Generate token,复制生成的 Token(只显示一次,务必保存

4.2 添加到仓库 Secrets

  1. 打开 my-blog-source 仓库
  2. SettingsSecrets and variablesActions
  3. 点击 New repository secret
  4. Name 填写:GH_TOKEN
  5. Value 粘贴刚才复制的 Token
  6. 点击 Add secret

五、GitHub Actions Workflow 配置

在项目根目录创建 .github/workflows/deploy.yml

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
name: Hexo Deploy

on:
push:
branches:
- main # 仅在 main 分支有推送时触发

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
# 步骤 1:检出源码
- name: Checkout 源码
uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整 git 历史,hexo-generator-index 等插件可能需要

# 步骤 2:配置 Node.js 环境
- name: 安装 Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm' # 缓存 node_modules,加速后续构建

# 步骤 3:安装 Hexo CLI
- name: 安装 Hexo CLI
run: npm install -g hexo-cli

# 步骤 4:安装项目依赖(含 Fluid 主题)
- name: 安装依赖
run: npm ci # 使用 package-lock.json 严格安装,保证版本一致性

# 步骤 5:清理旧的缓存和生成文件
- name: 清理缓存
run: hexo clean

# 步骤 6:生成静态文件
- name: 生成静态文件
run: hexo generate --verbose

# 步骤 7:验证生成结果(防止空文件被部署)
- name: 验证生成结果
run: |
HTML_COUNT=$(find ./public -name "*.html" | wc -l)
INDEX_SIZE=$(wc -c < ./public/index.html)
echo "HTML 文件数量: $HTML_COUNT"
echo "index.html 大小: $INDEX_SIZE bytes"
if [ "$INDEX_SIZE" -lt 100 ]; then
echo "❌ index.html 疑似空文件,终止部署"
exit 1
fi
echo "✅ 构建验证通过"

# 步骤 8:推送静态文件到 Pages 仓库
- name: 部署到 GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.GH_TOKEN }}
external_repository: your-username/your-username.github.io # 替换为你的用户名
publish_branch: main # Pages 仓库使用的分支
publish_dir: ./public # Hexo 生成的静态文件目录
commit_message: "ci: deploy from ${{ github.sha }}"
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'

注意将 external_repository 替换为你自己的用户名。


六、配置 GitHub Pages

6.1 在 Pages 仓库启用 GitHub Pages

  1. 打开 your-username.github.io 仓库
  2. SettingsPages
  3. Source 选择 Deploy from a branch
  4. Branch 选择 main,目录选择 / (root)
  5. 点击 Save

几分钟后访问 https://your-username.github.io 即可看到博客。


七、完整项目目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my-blog-source/
├── .github/
│ └── workflows/
│ └── deploy.yml ← GitHub Actions 配置
├── source/
│ ├── _posts/
│ │ └── hello-world.md ← 你的文章
│ └── about/
│ └── index.md
├── scaffolds/
├── themes/ ← 为空或不存在(主题通过 npm 安装)
├── _config.yml ← Hexo 全局配置
├── _config.fluid.yml ← Fluid 主题配置
├── package.json ← 依赖声明(含 hexo-theme-fluid)
└── package-lock.json ← 依赖锁定文件(必须提交)

八、日常写作流程

配置完成后,日常只需以下步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1. 新建文章
hexo new "我的新文章"

# 2. 编辑文章
# 打开 source/_posts/我的新文章.md,写你的内容

# 3. 本地预览(可选)
hexo server

# 4. 提交并推送
git add .
git commit -m "post: 我的新文章"
git push

# GitHub Actions 自动触发,约 1~2 分钟后博客更新

九、常见问题排查

Q1:Actions 报错 Dependencies lock file is not found

原因:package-lock.json 未提交到仓库,或被 .gitignore 排除。

解决:

1
2
3
4
5
6
7
# 检查 .gitignore 是否包含 package-lock.json
grep "package-lock" .gitignore

# 如果有,删除该行,然后强制添加
git add -f package-lock.json
git commit -m "fix: track package-lock.json"
git push

Q2:生成的 HTML 文件全是空文件

原因:Hexo 找不到主题模板(layout),通常是主题安装不完整。

解决:确认 package.json 中有 hexo-theme-fluid,并且 package-lock.json 已提交,让 CI 通过 npm ci 完整安装主题。

Q3:Actions 报错 Resource not accessible by integration

原因:GH_TOKEN 权限不足,或 Secret 名称填写有误。

解决:检查 Token 是否勾选了 repo 权限,Secret 名称是否与 workflow 中的 secrets.GH_TOKEN 一致。

Q4:推送后 Pages 没有更新

原因:your-username.github.io 仓库的 Pages 设置未正确配置。

解决:确认 Settings → Pages → Source 设置为 main 分支根目录,并等待 GitHub Pages 重新构建(通常需要 1~3 分钟)。

Q5:本地 hexo server 正常但 CI 构建失败

原因:本地 node_modules 状态与 package-lock.json 不一致,导致 CI 安装的依赖版本有差异。

解决:

1
2
3
4
5
6
# 重新生成 lock 文件
rm package-lock.json
npm install
git add package-lock.json
git commit -m "fix: regenerate package-lock.json"
git push

十、进阶优化

10.1 缓存加速

在 workflow 中 setup-node 加上 cache: 'npm' 后,GitHub Actions 会自动缓存 node_modules,后续构建速度可提升 50% 以上。

10.2 仅在文章变更时触发构建

1
2
3
4
5
6
7
8
9
on:
push:
branches:
- main
paths:
- 'source/**'
- '_config.yml'
- '_config.fluid.yml'
- 'package.json'

这样只有修改了文章或配置时才触发,避免修改 README 等文件时浪费 CI 时间。

10.3 绑定自定义域名

source/ 目录下创建 CNAME 文件:

1
www.yourdomain.com

peaceiris/actions-gh-pages 会自动将该文件复制到部署目录,防止每次部署后自定义域名被重置。


十一、完整 workflow 最终版本

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
name: Hexo Deploy

on:
push:
branches:
- main
paths:
- 'source/**'
- '_config.yml'
- '_config.fluid.yml'
- 'package.json'
- 'package-lock.json'

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout 源码
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: 安装 Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'

- name: 安装 Hexo CLI
run: npm install -g hexo-cli

- name: 安装依赖
run: npm ci

- name: 清理缓存
run: hexo clean

- name: 生成静态文件
run: hexo generate --verbose

- name: 验证生成结果
run: |
HTML_COUNT=$(find ./public -name "*.html" | wc -l)
INDEX_SIZE=$(wc -c < ./public/index.html)
echo "HTML 文件数量: $HTML_COUNT"
echo "index.html 大小: $INDEX_SIZE bytes"
if [ "$INDEX_SIZE" -lt 100 ]; then
echo "❌ index.html 疑似空文件,终止部署"
exit 1
fi
echo "✅ 构建验证通过"

- name: 部署到 GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.GH_TOKEN }}
external_repository: your-username/your-username.github.io
publish_branch: main
publish_dir: ./public
commit_message: "ci: deploy from ${{ github.sha }}"
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'

Hexo 博客使用 GitHub Actions 自动部署到 GitHub Pages 完整教程
https://iomelons.github.io/2026/03/23/cmn6ntccg000gsk214t0773ur/
作者
iomelons
发布于
2026年3月23日
许可协议