通过github私库部署hexo博客的优势

  在不影响公开访问的同时,通过github私库存储全部的博客文档,更方便的迁移工作环境

步骤

1.创建一个包含hexo源码的github私库

创建私库,注意不要勾选Add a README file,否则heo初始化会报错。
windows环境下需安装gitbash,npm环境配置不再赘述。把私库fork到本地后执行:

1
2
3
4
5
6
# install hexo framework
npm install hexo
# initialize Hexo in the target <folder>
hexo init <folder>
cd <folder>
npm install

再提交到仓库

2.创建github令牌并添加到action密钥变量

创建令牌,权限至少包括:workflowwrite:packages,在私库中添加action secret,暂且称之为MYPAT

3.添加action

.github/workflow/目录下创建文件pages.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
name: Hexo Build & Deploy - Private to Public

on:
workflow_dispatch:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4.2.2
with:
token: ${{ secrets.MYPAT }}
submodules: 'true'
persist-credentials: false

- name: Prepare Node env
uses: actions/setup-node@v4.3.0
with:
node-version: 22
cache: npm

- name: Install pandoc
run: |
cd /tmp
wget -c https://github.com/jgm/pandoc/releases/download/3.6.4/pandoc-3.6.4-1-amd64.deb
sudo dpkg -i pandoc-3.6.4-1-amd64.deb

- name: Hexo
run: |
pandoc --version
npm i -g hexo-cli
npm i
hexo clean && hexo g

- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4.7.3
with:
token: ${{ secrets.MYPAT }}
repository-name: <your-github-name>/<your-github-name>.github.io
branch: gh-pages # The branch the action should deploy to.
folder: public # The folder the action should deploy.
single-commit: true
commit-message: "Deploy by source"