用 GitHub Actions 自动部署 Hexo 及使用腾讯云 EdgeOne Pages 集成部署(CI)(一)

介绍下如何用 GitHub Actions 来自动部署基于 Hexo 的 Blog。

建议大家先看一下 GitHub Actions 官方的介绍,不然对于下面的内容可能没有办法很好的理解。

Hexo

首先我们先要在本地确保 Hexo 是可以正确运行的,比如:

1
2
hexo clean
hexo deploy

至于如何设置和使用 Hexo,请参考 [https://hexo.io/

至于如何使用 GitHub Pages 部署自己的网站,请参考:https://pages.github.com/(我使用的腾讯云 EdgeOne Pages,国内访问挺快的,国外可以用 Vercel 或者 cloudflare)

确认 _config.yml 文件中有类似如下的 GitHub Pages 配置:

1
2
3
4
deploy:
type: git
repository: git@github.com:sssstarrr/sssstarrr.github.io.git
branch: main

注意:请将 repository 修改为你自己的仓库地址。

生成秘钥

Windows 用户可以在市场中安装 git 以后在博客根目录右键点击 Open Git Bash here 执行:

1
ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""

这会在当前目录生成两个文件:

  • github-deploy-key —— 私钥
  • github-deploy-key.pub —— 公钥

GitHub 配置秘钥

我们把私钥放到我们存放 Hexo 原始文件的代码仓库里面,用于触发 Actions 时使用。

公钥放到 GitHub Pages 对应的代码仓库里面,用于 Hexo 部署时的写入工作。

配置私钥

首先在 GitHub 上打开保存 Hexo 的仓库,然后选择 New secret画面如下:

名字部分填写:HEXO_DEPLOY_KEY,注意大小写,这个后面的 GitHub Actions Workflow 要用到,一定不能写错。 在 Value 的部分填入 github-deploy-key 中的内容

new

添加了私钥以后的界面显示如下: 私钥

添加公钥

接下来我们需要访问存放网页的仓库,也就是 Hexo 部署以后的仓库,比如:yourname.github.io 这种,访问 Settings -> Deploy keys,按 Add deploy key 来添加一个新的公钥,在 Title 中输入:HEXO_DEPLOY_PUB 字样,当然也可以填写其它自定义的名字。

Key 中粘贴 github-deploy-key.pub 文件的内容。

注意:一定要勾选 Allow write access 来打开写权限,否则无法写入会导致部署失败。

最后添加好了公钥的界面如下:image-20251025204725461

创建 Workflow

首先在 Hexo 的仓库中创建一个新文件:.github/workflows/deploy.yml,文件名可以自己取,但是一定要放在 .github/workflows 目录中,文件的内容如下:

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:
- master

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

steps:
- name: Checkout source
uses: actions/checkout@v4
with:
ref: master

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

- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_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 "example@qq.com"
git config --global user.name "三水番"
npm install hexo-cli -g
npm install

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

简单解释一下,当我们推送内容到远程 master 分支的时候,就会触发这个 Workflow。

使用 Ubuntu latest 作为 hexo deploy 的系统。

首先 checkout 源代码,然后设置使用最新的 Node.js 20 作为 node 解释器。

接下来就是创建 SSH 相关的配置文件,注意 secrets.HEXO_DEPLOY_KEY 就是对应我们之前设置的私钥,所以名字一定不要搞错。

git config 相关的名字和邮件地址替换成大家自己使用的就好了。

最后就是安装 Hexo CLI,各个依赖模块和部署了。

验证

下面就是 GitHub Actions 页面显示的运行结果:image-20251025205015743

前面有绿色钩钩的,就表示部署成功,红色叉叉的表示失败。如果部署失败,还会收到 GitHub 的邮件提醒。

好了,以上就是利用 GitHub Actions 自动部署 Hexo 到 GitHub Pages 的方法,谢谢观赏。

注:可能会遇到

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
16:05:51.196: [C:\Users\Administrator\OneDrive\文档\hexo\blog] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:refs/heads/master --set-upstream
Enumerating objects: 447, done.
Counting objects: 0% (1/447)
Counting objects: 1% (5/447)
Counting objects: 2% (9/447)
Counting objects: 3% (14/447)
Counting objects: 4% (18/447)
Counting objects: 5% (23/447)
。。。。。。
remote: Resolving deltas: 100% (14/14), done.
remote: error: GH013: Repository rule violations found for refs/heads/master.
remote:
remote: - GITHUB PUSH PROTECTION
remote: —————————————————————————————————————————
remote: Resolve the following violations before pushing again
remote:
remote: - Push cannot contain secrets
remote:
remote:
remote: (?) Learn how to resolve a blocked push
remote: https://docs.github.com/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-from-the-command-line#resolving-a-blocked-push
remote:
remote:
remote: —— GitHub SSH Private Key ————————————————————————————
remote: locations:
remote: - commit: b39ac3c12c5f38396b1a6a5cf7b66d57c4ba231b
remote: path: github-deploy-key:1
remote:
remote: (?) To push, remove secret from commit(s) or follow this URL to allow the secret.
remote: https://github.com/sssstarrr/hexo-source-code/security/secret-scanning/unblock-secret/34Y93cAHIsx7bxezhpnO7ACpMDo
remote:
remote:
remote:
error: failed to push some refs to 'https://github.com/sssstarrr/hexo-source-code.git'
To https://github.com/sssstarrr/hexo-source-code.git
! refs/heads/master:refs/heads/master [remote rejected] (push declined due to repository rule violations)
Done

这是你的推送(git push)被 GitHub 的 Push Protection(推送保护机制) 拦截了。

GitHub 在 2023 年后对所有仓库默认启用了 “密钥扫描 + 推送保护(Secret Scanning & Push Protection)”。 它会在你执行 git push 时自动扫描提交内容中是否包含敏感信息,例如:

  • SSH 私钥
  • API Token
  • Access Key
  • 密码、令牌、机密配置等

解决办法:删除该文件、重新提交并推送**;如果不是机密文件,也可在 GitHub 链接上“手动解除阻止”。(仓库设置 private 是否有风险我也不知道,来个大佬可以解释下可不可以上传到私有仓库)

参考资料

https://zhuanlan.zhihu.com/p/170563000 https://github.com/features/actions https://pages.github.com/