GitHub Actions

GitHub Actions 是 GitHub 於 2019 年底推出的自動化流程工具,它的功能很強大。而我們只需要用到它的構建。由於是 GitHub 自家的工具,我們無需再額外的註冊賬號,GitHub Actions 也集成在了 GitHub 界面上,我們只需點擊 Actions 進行創建。

創建 Actions

在 Hexo 目錄的倉庫,點擊 Actions Tab, 然後點擊 Set up a workflow yourself

把以下代碼複製到代碼框去, 注意如果倉庫中使用到子模塊,則需要將submodules設置為true

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
49
50
51
52
name: 自動部署 Hexo

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x]

steps:
- name: 開始運行
uses: actions/checkout@v1
with:
submodules: true

- name: 設置 Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: 安裝 Hexo CI
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 緩存
uses: actions/cache@v1
id: cache-dependencies
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

- name: 安裝插件
if: steps.cache-dependencies.outputs.cache-hit != 'true'
run: |
npm install

- name: 部署博客
run: |
hexo clean && hexo g && hexo douban && gulp
cd ./public
git init
git config user.name "${{secrets.GIT_NAME}}"
git config user.email "${{secrets.GIT_EMAIL}}"
git add .
git commit -m "Update"
git push --force --quiet "https://${{secrets.GH_TOKEN}}@${{secrets.GH_REF}}" master:master
  • on 是要被監測的 branch, 等同 Travis-ci 的 branches
  • GIT_NAME, GIT_EMAIL, GH_TOKEN, GH_REF 均是爲了保密需要,需要在 secrets 進行額外設置的全局變量

設置隱私資料

Settings 裏的 Secrets 裏設置全局變量

  • GIT_NAME: 用戶名
  • GIT_EMAIL: 郵件
  • GH_REF: 倉庫地址,以.git結尾
  • GH_TOKEN: 在Setting->Developer Setting->Personal access tokens->Generate new token中申請一個public_repotoken即可

運行

現在你只要把 Hexo 的目錄推送到 GitHub 去,GitHub Actions 會自動監測到變化,然後進行編譯。

參考