Hexo 使用 Github Actions 自动更新

准备

  • 需要两个 github 仓库:
    • 一个用于发布页面: XXXXXX.github.io
    • 一个用于放源码: hexo-source (可设为隐私仓库)

1. 创建 hexo-source 仓库

  • 在hexo的根目录下( ~/hexo)运行:
    1
    2
    3
    4
    5
    6
    7
    8
    cd hexo

    git init
    git add .
    git commit -m "first commit"
    git remote add origin https://github.com/XXXXXX/hexo-source.git

    git push -u origin master

2. Github Actions 设置

  • 运行 ssh-keygen 生成一对密钥。

    1
    ssh-keygen
  • 打开 XXXXXX.github.io 仓库设置,在 Deploye keys 选项中,添加公钥~/.ssh/id_rsa.pub的内容。

  • 打开 hexo-source 仓库设置,在 Secrets 选项中,新建 repo secret: 名称设为GITHUB_ACTION, 内容为~/.ssh/id_rsa的内容。

3. 添加 Github Actions 配置文件

内容如下:

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: Hexo Auto-Deploy
on: [push]

jobs:
build:
name: Hexo Auto-Deploy by GitHub Actions
runs-on: ubuntu-latest

steps:
- name: 1. git checkout...
uses: actions/checkout@v1

- name: 2. setup nodejs...
uses: actions/setup-node@v1

- name: 3. install hexo...
run: |
npm install hexo-cli -g
npm install

- name: 4. hexo generate public files...
run: |
hexo clean
hexo g

- name: 5. hexo deploy ...
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.GITHUB_ACTION }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts

git config --global user.name "XXXXXX"
git config --global user.email "[email protected]"
git config --global core.quotepath false

hexo d

以上。