使用 github action 自动发布 hexo blog

背景

用 Hexo 来生成 Github Pages 有一段时间了,之前一直是在本地写完 Blog 然后执行下述指令来发博客

script
1
2
rm -rf public
npx hexo deploy

今天想想,不是有 github acion 嘛,能不能使用自动化 CI 流程来发布,当我的 Hexo Repo 发布时,自动进行 deply 动作,进而部署到 rovast.github.io。
当然是可以的!本文就其中的步骤进行记录,也算是 github action 的初体验了。

步骤

首先我们要确认一个前提,就是 github pages 和 hexo 源码是两个东西,那我的来说,我有两个仓

整体的步骤如下

  1. 正确配置 hexo-config 的配置文件 _config.yaml
  2. 正确 https://github.com/rovast/hexo-config 仓库配置,尤其是 git 的 ssh 私钥和 github action
  3. 正确配置 https://github.com/rovast/rovast.github.io 的 deploy key

接下来依次配置

1、配置 hexo-config 项目的 _config.yaml

1
2
3
4
deploy:
type: git
repo: [email protected]:rovast/rovast.github.io.git
branch: master

注意其中的 repo 一定要是用 ssh 的协议,一因为后面我们要借助 ssh-key。

2、配置 https://github.com/rovast/hexo-config 的 github 配置

1. 配置 github action 的 yaml

参考的 https://github.com/sma11black/hexo-action,我们新建文件 .github/workflows/deploy.yaml

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
name: Deploy

on: [push]

jobs:
build:
runs-on: ubuntu-latest
name: A job to deploy blog.
steps:
- name: Checkout
uses: actions/checkout@v1
with:
submodules: true # Checkout private submodules(themes or something else).

# Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.)
- name: Cache node modules
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

# Deploy hexo blog website.
- name: Deploy
id: deploy
uses: sma11black/[email protected]
with:
deploy_key: ${{ secrets.DEPLOY_KEY }}
commit_msg: ${{ github.event.head_commit.message }} # (or delete this input setting to use hexo default settings)
# Use the output from the `deploy` step(use for test action)
- name: Get the output
run: |
echo "${{ steps.deploy.outputs.notify }}"

2. 配置 secrect.DEPLOY_KEY

我们看到 github action 里读取了 $,这个需要在 github 里正确配置。

注意这里应该填写的是私钥(cat ~/.ssh/id_rsa),而不是 pub 公钥

image-20210320222831807

3、在 https://github.com/rovast/rovast.github.io 配置 deploy key

cat ~/.ssh/id_rsa.pub,配置到仓库的 deploy key 里面

image-20210320223441248

4、结束

正常保存 https://github.com/rovast/hexo-config 仓库,push 后自动触发 github action 进行发布。

image-20210320223657343

image-20210320223734616

使用感受

有以下几点在使用过程中不得不提

  1. 丰富的 github action 模板,你甚至可以在编写的工程中在线搜索他的 Market Place!对新手而言可以快速入门,对于老手而言可以快速找到适合你的速成方案。

    image-20210320224530765

  2. 自定义的 secrect.VAR,并且这个 secrect 编写后,居然是不可以查看的,只能更改,简直太赞!对于一些敏感内容而言,简直完美!

    image-20210320224658673

  3. 执行 action 过程中的 UI 提示,很丝滑,执行到哪步,就显示哪个步骤。

    image-20210320224737049

  4. 另外,通过查看 https://github.com/sma11black/hexo-action 源码,我们知道,是可以对 Dockerfile 进行操作的,这个想象的空间就无限大了。

让你感觉这个流程本该如此的操作,一定是好操作!

参考文章

  1. 《GitHub Actions 入门教程》http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html
  2. https://github.com/sma11black/hexo-action