使用 GitHub Actions 部署 Hexo

首先需要在 GitHub 上建立两个仓库,一个 公有,一个 私有

公有 仓库用于存放 Hexo 生成的静态文件以部署 GitHub Pages

私有 仓库用于存放未经编译的 Hexo 文件。

示例:starudream/blog-page 为我的 公有 仓库,starudream/blog 是我的 私有 仓库。

然后在 https://github.com/settings/tokens 申请 PAT,并将其加入私有仓库的 Secrets

最后在 私有 仓库内创建文件 .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
name: Deploy
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 1
- name: Checkout Page
uses: actions/checkout@v2
with:
fetch-depth: 1
repository: starudream/blog-page
path: .deploy_git
token: ${{ secrets.PAGE_PAT }}
- name: Cache
uses: actions/cache@v1
with:
path: node_modules
key: ${{ runner.os }}-${{ hashFiles('**/package.json') }}
- name: Node
uses: actions/setup-node@v1
with:
node-version: 12
- name: Build
run: |
npm install hexo-cli -g && npm install && npm run build
- name: Deploy
run: |
rm -rf .deploy_git/* && cp -rf public/* .deploy_git/
git config --global user.name starudream
git config --global user.email [email protected]
message=$(git log -1 --pretty=format:%s)
cd .deploy_git
git add -A
git commit -m "$message"
git push