hexo搭建博客

按照官网教程先把 hexo 在本地安装好

官网教程

1
$ npm install -g hexo-cli

然后再执行: hexo init your-blog-directory,相当于这个 hexo-cli 初始化了一个博客目录。

博客美化: 使用 next 主题

参考这篇文档:Hexo + NexT主题美化GitHub博客

Github Page 自动化发布

在自己的 Github 下创建2个 Repo, 一个以 username/username.github.io 另一个可以是 private 的repo,比如 blog,用于存 hexo 的源码。
流程就是,在 blog 项目里写博文,然后通过 Github Action 推动到 username.github.io 的分支 gh-pages 用于发布。blog 项目可以是私有的,这样就可以隔离源码和发布后的 public 文件夹。

一、仓库准备

源码仓库(任选其一)
新建 blog-src(私有更好,防止草稿泄露)。
把本地 Hexo 根目录全部文件(含 package.json、_config.yml、主题目录、source/、scaffolds/ 等)推送到该仓库。
GitHub Pages 仓库
若尚未创建,新建 username.github.io(公开)。
进入该仓库 → Settings → Pages → Source 选 “GitHub Actions”,不要选 branch,否则后面 Actions 会冲突。

二、一次性密钥配置

Actions 需要有权把构建结果推送到 username.github.io 仓库,最简办法:
生成仅用于部署的 Personal Access Token
GitHub 头像 → Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token (classic)
勾选 repo 全部权限,过期时间选 “No expiration” 或 90 天,复制生成的 token。
把 token 添加到源码仓库的 Secret
进入 blog-src → Settings → Secrets and variables → Actions → New repository secret
Name 填 GH_TOKEN(下文 workflow 里引用),Value 粘贴刚才的 token。

三、一键 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
name: Deploy to External Repo
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source repo
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Cache dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install dependencies
run: npm install

- name: Build site
run: npm run build

- name: Deploy to external repo
uses: peaceiris/actions-gh-pages@v4
with:
personal_token: ${{ secrets.GH_TOKEN }} # 必须是 PAT,且对目标仓库有写权限
external_repository: lxyangfan/lxyangfan.github.io # 替换为你的目标仓库
publish_branch: gh-pages # 目标仓库的 GitHub Pages 分支
publish_dir: ./public # 构建输出目录
cname: frankk.top # 可选:自定义域名