前置配置 - _config.yml

1
2
3
4
5
6
7
8
# 运行 hexo generate 命令时,相关文件的输出目录
public_dir: build

# 运行 hexo deploy 命令时,文件部署的目标地址
deploy:
type: git
repo: git@github.com:fangqk1991/fangqk1991.github.io.git
branch: master

SSH Key 准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 生成用于 hexo 发布的密钥对
ssh-keygen -t rsa -f ~/.ssh/hexo

cat ~/.ssh/hexo
# https://github.com/fangqk1991/github-blog
# 网页访问 GitHub 相关项目 - Settings - Secrets - Actions - New repository secret
# 创建 Name 为 HEXO_DEPLOY_PRI 的私钥
# 将 cat ~/.ssh/hexo 返回的内容作为私钥的 Value

cat ~/.ssh/hexo.pub
# https://github.com/fangqk1991/fangqk1991.github.io
# 网页访问 GitHub 相关项目 - Settings - Deploy keys - Add deploy key
# 创建 Name 为 HEXO_DEPLOY_PUB 的公钥
# 将 cat ~/.ssh/hexo.pub 返回的内容作为私钥的 Value

##### 如需将博客文件发布到私有服务器
# 将 hexo.pub 公钥信息添加到目标服务器 ~/.ssh/authorized_keys 中
cat ~/.ssh/hexo.pub | ssh some_user@example.com "cat >> ~/.ssh/authorized_keys"

添加 .github/workflows/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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: CI

on:
push:
branches:
- master

env:
GIT_USER: fangqk1991
GIT_EMAIL: me@fangqk.com
THEME_REPO: fangqk1991/maupassant-hexo
THEME_BRANCH: master
DEPLOY_REPO: fangqk1991/fangqk1991.github.io
DEPLOY_BRANCH: master
# Using for "Deploy to a server"
SERVER_USER: some_user
SERVER_HOST: example.com
SERVER_PATH: /some/path

jobs:
build:
name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest]
node_version: [12.x]

steps:
- name: Checkout
uses: actions/checkout@v3

# Download Hexo theme
- name: Checkout theme repo
uses: actions/checkout@v3
with:
repository: ${{ env.THEME_REPO }}
ref: ${{ env.THEME_BRANCH }}
path: themes/maupassant-hexo

- name: Checkout deploy repo
uses: actions/checkout@v3
with:
repository: ${{ env.DEPLOY_REPO }}
ref: ${{ env.DEPLOY_BRANCH }}
path: .deploy_git
ssh-key: ${{secrets.HEXO_DEPLOY_PRI}}

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

# Prepare SSH Key and target hosts
- name: Configuration environment
env:
HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
run: |
sudo timedatectl set-timezone "Asia/Shanghai"
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name $GIT_USER
git config --global user.email $GIT_EMAIL

- name: Build Hexo
run: |
yarn install
npx hexo generate
echo fangqk.com > build/CNAME

- name: Deploy to GitHub
run: |
npx hexo deploy

- name: Deploy to a server
run: |
ssh-keyscan ${SERVER_HOST} >> ~/.ssh/known_hosts
rsync -avz --delete --progress ./build/ ${SERVER_USER}@${SERVER_HOST}:${SERVER_PATH}