博客手札:利用Github Action自动化部署Hexo

蓝色大肥鱼:这篇文章记录了用 GitHub Action 自动构建和部署 Hexo 博客的完整流程。作者给出了工作流 YAML、Node 与 Yarn 环境、node_modules 和 public 缓存、SSH 部署密钥与仓库 Secrets 的配置方法,并提醒部署目标需要单独的 <username>.github.io 仓库;文末也吐槽了 Action 部署 commit 不连续、不如本地 deploy 顺手。

AI 摘要 DeepSeek v4.1 Flash

本文最后更新于 2026年11月10日 下午

编写工作流

由于众所周知的稳定性缘故,所以需要使用同步代理站 kkgithub.com

创建完仓库,push 好之后。直接点进 Action 栏目,选择 Node.js 项目,把代码复制下来。

然后本地创建文件:./.github/workflows/hexo.yml

我这就直接放我测试通过的了。[1][2][3]

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# For more information see: https://docs.kkgithub.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Hexo Blog CI

on:
push:
branches: [ "master" ]

jobs:
build:
name: 构建 Hexo 静态页
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [ 22.x ]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- name: 1.检出 master
uses: actions/checkout@v4

# 设置 NodeJS 环境
- name: 2.设置 NodeJS ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'


- name: 3.安装 yarn
run: npm install yarn -g

- name: 4.安装 Hexo 命令行工具
run: yarn global add hexo-cli

- name: 5.缓存 node_modules
uses: actions/cache@v4
id: cache-yarn
with:
path: ./node_modules
key: ${{runner.OS}}-${{hashFiles('**/yarn.lock')}}
restore-keys: |
${{runner.OS}}-${{hashFiles('**/yarn.lock')}}
${{runner.OS}}-

- name: 5.1.缓存 public
uses: actions/cache@v4
id: cache-public
with:
path: ./public
key: ${{runner.OS}}-public
restore-keys: |
${{runner.OS}}-public

- name: 6.配置 gitssh
env:
PRIVATE: ${{ secrets.GIT_SSH_PRIVATE }}
run: |
mkdir -p ~/.ssh/
echo "$PRIVATE" > ~/.ssh/github
chmod 600 ~/.ssh/github
printf "Host github.com\nHostName github.com\nIdentityFile ~/.ssh/github" > ~/.ssh/config
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name '${{ secrets.GIT_USER }}'
git config --global user.email '${{ secrets.GIT_MAIL }}'

- if: ${{ steps.cache-yarn.outputs.cache-hit != 'true' }}
name: 7.初始化Hexo
run: yarn

- name: 8.Hexo构建静态页
run: |
hexo g

- name: 9.Git Push
run: |
git config --global core.autocrlf false
hexo deploy

敲黑板,划重点。github库设置(存放 Hexo 源码的库,不是 public 库)

变量名含义
GIT_SSH_PRIVATEgit ssh的私钥,直接复制即可
GIT_USERgit 用于提交的用户名
GIT_MAILgit 用于提交的邮箱

跟着下面两张图去找到设置变量的位置,一个一个设置就完事了。

找到位置

设置变量

完事直接 push 就可以了。

后记

我是真服了啊,deploy 得另外准备一个库【<username>.github.io】才行啊。得强行覆盖了。

我本地的

Github上的

【<username>.github.io】这个库是关键,博客 pages 服务靠的就是这个库,然后去又拍云做加速。


体验一下流程就完事了,action 是真不好用,可能是我不太熟悉吧。public 库的 commit 不是连续的,就很难受。还是本地 deploy 来得舒服,关键是可以保留 commit 记录。

参考文献


博客手札:利用Github Action自动化部署Hexo
https://www.vlice.cn/po/6a33ba4e7ef0/
作者
Violet_ice紫冰
发布于
2025年1月13日
更新于
2026年11月10日
许可协议