还在用hexo d来推送你的Blog到Github上吗?本文教您如何使用Github Actions自动推送!

本文假定您已经有了Hexo blog的基础。如果没有,请左转用GitHub建立自己的博客

生成公私钥对

首先,你需要准备一个ssh的公私钥匙对。你可以使用本地的 ssh-keygen 也可以使用在线网站,例如 https://8gwifi.org/sshfunctions.jsp 这样的公私钥生成器。生成完后,保存下你的公私钥(分别就是 Public keyPrivate key),准备之后的步骤使用

生成 Deploy key

首先,进入你的 Github blog 仓库,也就是 yourname.github.io 然后点击 Settings. 在右边找到 Deploy keys

什么是 Deploy keys

Deploy keys 是为 git ssh 而生的。Deploy keys是一组公钥,只有拥有与这些公钥配对的私钥的人才能操作该仓库。

仓库的Deploy keys,是这个仓库的专有的key,用这个公私钥对,只能操作这个项目,其他项目都没有权限。

现在,点击右侧的 Add deploy key。将你生成的公钥粘贴到deploy key里面。勾选 Allow write access 以允许之后github actions push到你的仓库中。这里 title 可以随便填写,方便你自己记忆即可。

新建代码仓库

进入你的 hexo blog 文件夹。现在,你需要在这里建立一个代码仓库,用作之后的数据存放

1
2
3
git init
touch .gitignore
touch build.sh

build.sh 文件的作用是不用修改 .github/workflows 里的内容而修改你的自动化build操作。

gitignore

编辑你的 .gitignore 文件,写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
_multiconfig.yml
.deploy_git/
data/
themes

这将忽略大部分会有hexo帮你生成的文件夹。

注意,我们这里把 themes 文件夹忽略掉了!因此,你必须在之后的部署中把你的主题下载回来。这里我们以我用的主题 Anatolo 为例子。

首先,把你的主题所改动的文件复制出来,放到asserts文件夹里。例如我改动了 Anatolo 主题里面的 source/images 文件夹,于是我把这整个文件夹复制到了blog的根目录的 asserts/images 文件夹。

修改 build.sh

编辑 build.sh 文件,对于我来说,我需要写入以下内容:

1
2
3
4
5
6
7
8
9
mkdir themes
git clone https://github.com/Lhcfl/hexo-theme-anatolo themes/Anatolo
cp ./_config.theme.yml ./themes/Anatolo/_config.yml
cp -r ./asserts/images/* ./themes/Anatolo/source/images/

npm install hexo-renderer-pug --save
npm install hexo-renderer-stylus --save

npx hexo g
  • mkdir themes 的作用是创建被我们忽略掉的themes文件夹
  • git clone 把这个主题从github上clone到themes中间
  • 后面的两个 cp 是将我们需要对主题进行的改动复制进去
  • 两个 npm install 是需要Anatolo主题安装后需要进行的操作。(实际上它应该已经保存在了 node modules 里面,可以不写)
  • npx hexo g 将blog生成到 /public 文件夹中

现在,执行 build.sh 应该可以正常生成一个你的blog了。

上传到 github

现在,去github创建一个 Private 仓库。注意,如果你不希望别人只需要简单的 fork 一下就能创造一个和你一模一样的仓库,甚至暴露你的配置文件中可能涉及到的私有信息,那就选择 Private

现在, git remote add origin <你的仓库地址>。将你的hexo 仓库上传上去。

配置 Actions

去你的private仓库里,打开 Settings ,找到左侧的 Actions 里的 General,右边会有 Actions permissions。 把它切换成 Allow all actions and reusable workflows

配置 secret

去你的private仓库里,打开 Settings ,找到左侧的 Secrets and varibles 里的 Actions,点击 New repository secret.这里我们Name填写 DEPLOY_PRI (后面的代码会用到)。Secret就填写你刚刚生成的私钥

创建 Workflow

在你的hexo文件夹里新建 .github\workflows\build.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
name: Build

on:
push:
branches:
- main

jobs:
build_blog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: bash build.sh

- name: Configure Git # 配置Git
env:
DEPLOY_PRI: ${{secrets.DEPLOY_PRI}} # 这里就是刚刚配置的私钥了
GIT_USERNAME: ${{ github.repository_owner }} #Github用户名,这里用了Actions自带的变量,也可以写死。
GIT_EMAIL: ${{ github.repository_owner }}@user.github.com # 邮箱,可以写自己的邮箱。
run: |
sudo timedatectl set-timezone "Asia/Shanghai"
mkdir -p ~/.ssh/
echo "$DEPLOY_PRI" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name '$DEPLOY_PRI'
git config --global user.email '$DEPLOY_PRI'

- name: Commit Blog # 提交文档到Git仓库
env:
GIT_URL: 'git@github.com:yourname@yourname.github.io.git' # 把它换成项目的地址,注意要用SSH格式的。
run: |
cd public
git init
git remote add origin $GIT_URL
git add -A
git commit -m "Blog auto generated."

- name: Push blog # 推送
run: |
cd public
git push origin HEAD:master --force

yourname@yourname.github.io.git换成项目的地址,注意要用SSH格式的。

大功告成了!现在 git add .git commit 然后 git push origin main

你的Github blog应该可以自动推送了。