Hexo 自动化部署

GitHub Action

首先是 Hexo 提供的官方 GitHub Pages 配置

pages.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
49
name: Pages

on:
push:
branches:
- main # default branch

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node@v2 # 需要调整到 v3 版本
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v2 # 需要调整到 v3 版本
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v2
with:
path: ./public
deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2

限制

以上把代码与发布 target 限定在同一个仓库, 即完全暴露了配置与代码.

分离源代码仓库与页面文件仓库

非官方插件

第一种方式是使用非官方插件 github-action-push-to-another-repository.

pages.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
49
50
51
52
53
54
55
56
57
58
59
60
61
name: Pages

on:
push:
branches:
- master # default branch

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
# - name: Upload Pages artifact
# uses: actions/upload-pages-artifact@v2
# with:
# path: ./public
# deploy:
# needs: build
# permissions:
# pages: write
# id-token: write
# environment:
# name: github-pages
# url: ${{ steps.deployment.outputs.page_url }}
# runs-on: ubuntu-latest
# steps:
# - name: Deploy to GitHub Pages
# id: deployment
# uses: actions/deploy-pages@v2

- name: Pushes to public repository
uses: cpina/github-action-push-to-another-repository@main
env:
API_TOKEN_GITHUB: <% 能访问目标仓库授权 token %>
with:
source-directory: 'public'
destination-github-username: <% 目标仓库用户 %>
destination-repository-name: <% 目标仓库 %>
user-email: <% 提交的用户邮件 %>
target-branch: master


别忘了手动设置 GitHub Pages 到源代码模式: image

官方插件

第二种方式是使用官方插件 one-command-deployment.

pages.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

name: Pages

on:
push:
branches:
- master # default branch

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
# - name: Upload Pages artifact
# uses: actions/upload-pages-artifact@v2
# with:
# path: ./public
# deploy:
# needs: build
# permissions:
# pages: write
# id-token: write
# environment:
# name: github-pages
# url: ${{ steps.deployment.outputs.page_url }}
# runs-on: ubuntu-latest
# steps:
# - name: Deploy to GitHub Pages
# id: deployment
# uses: actions/deploy-pages@v2

# - name: Pushes to public repository
# uses: cpina/github-action-push-to-another-repository@main
# env:
# API_TOKEN_GITHUB: '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'
# with:
# source-directory: 'public'
# destination-github-username: 'siweipancc'
# destination-repository-name: 'blog-hexo-public'
# user-email: [email protected]
# target-branch: master

- name: Pushes to public repository
run: npm exec hexo deploy

_config.yml

补充如下信息:

1
2
3
4
5
6
7
8
9
10

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: https://oauth2:<% 能访问目标仓库授权 token %>@github.com/<% 目标仓库用户 %>/<% 目标仓库 %>.git
message: 'auto update'
name: '<% 提交的用户 %>'
email: '<% 提交的用户邮件 %>'
branch: master

: