Hexo Deploy By Github Actions

Intro

作为个人的技术笔记,将 Hexo Blog 以 Github Pages 的方式对外发布。Hexo Cli 提供了较好的发布方式(hexo deploy),足以满足低频发布操作。

今天的实践是:通过 Github Actions 来“持续集成”发布 Hexo Blog Site 到 Github Pages

实践的过程中遇到各种问题,对环境做更新:
Nodejs:v12.19.0->v14.15.4
Hexo: 3.9.0-> 5.3.0

Github Actions

GitHub Actions让你很容易自动化所有的软件工作流程,现在拥有世界级的CI/CD。从GitHub上构建、测试和部署你的代码。让代码评审、分支管理和问题分类按照您想要的方式工作。

Github Actions 特性:https://github.com/features/actions

Github Actions 文档:https://docs.github.com/cn/actions

Github Actions 市场:https://github.com/marketplace?type=actions

Hexo Action

Github Actions 市场已经有了相关的 Action:Hexo Action。

https://github.com/marketplace/actions/hexo-action

https://github.com/sma11black/hexo-action

Operation

参考 Hexo Action 使用手册,

  1. 创建密钥对(Github Pages Deploy Keys 使用公钥,Github Source Repos Secrets DEPLOY_KEY使用私钥);
  2. 在 Github Source Repos 添加流程文件,如在 .github/workflows 下创建 deploy.yml

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
44
45
46
47
48
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]

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: Hexo Action
id: deploy
uses: sma11black/hexo-action@v1.0.4
with:
deploy_key: ${{ secrets.DEPLOY_KEY }}
user_name: shankai
user_email: shankai.kvn@gmail.com
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 }}"
  • name Actions 的名称
  • on 触发 Actions 的事件
  • jobs 执行的一系列任务,每个任务是单独的运行环境,runs-on指定运行环境
  • steps任务包含一系列操作步骤,每个步骤是一个 Action,如 Hexo Deploy 本例中使用 uses: sma11black/hexo-action@v1.0.4

编辑完成 deploy.yml 后提交变更,push 到 Hexo Source Repo。

FAQ

  1. 将主题 next 作为 git module(Github Actions: hexo-action 用法)

    因为 hexo 站点与 next 主题是完全独立的,此处在构建时 next 做为资源依赖参与构建。

1
2
3
4
rm -rf themes/next
git rm -r themes/next
rm -rf .git/modules/themes/next
git submodule add https://github.com/theme-next/hexo-theme-next themes/next
  1. 远程仓库使用 SSH 方式访问而非 Https

    出现的错误:

1
2
3
4
5
6
fatal: could not read Username for 'https://github.com': No such device or address
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/github/workspace/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (events.js:314:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

修改 hexo/_config.yml 部署相关配置,使用 SSH 方式访问 Github Pages Repo(https://github.com/sma11black/hexo-action/issues/5)

1
2
3
4
deploy:
type: git
repo: git@github.com:<username>/<username>.github.io.git
branch: master

(完)