Deploy Hexo blog automatically by Github Action

在博客的站点配置文件_config.yml中将博客仓库地址从https地址和改为ssh地址,已经是ssh地址的跳过。

生成 SSH Key,不是第一次生成的话用一下命令:

1
ssh-keygen -t rsa -f ~/.ssh/id_rsa_x -C "yourmail@xxx.com"

将生成的 private key 作为 Hexo 源文件仓库 Settings > Secrets > Actions 的 一个名叫 DEPLOY_KEY 的 Secret。注意:需要复制包括 ——-BEGIN OPENSSH PRIVATE KEY——- 和 ——-END OPENSSH PRIVATE KEY——- 的整个内容。

将生成的 public key 作为网站文件仓库 Settings > Deploy Keys 的 Deploy Key。随便取个名字。

在博客源文件根目录.github/workflows 下,创建一个名为action,后缀为.yml的文件。如果没有workflows文件夹就自己创建一个。

下面是action.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
name: Hexo Deploy

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id

steps:
- name: Checkout source
uses: actions/checkout@v2
with:
ref: main

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '12'

- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "your email"
git config --global user.name "your username"
npm install hexo-cli -g
npm install

- name: Deploy
run: |
hexo clean
hexo generate
hexo deploy

对应修改自己的用户名和邮箱即可。

我个人每次修改完博客的习惯:

1
2
3
git add .
git commit -m "back up"
git push

References

https://www.cnblogs.com/deppwang/p/12326906.html
https://github.com/DeppWang/hexo-action
https://cyfeng.science/2020/12/02/use-github-action-to-automatically-deploy-hexo/