前言
网上虽然有很多通过GitHub Actions自动部署Hexo的教程,但都有各种各样的问题。
主要问题还是Workflow脚本没有写正确,比如插件部分。
步骤
1、生成密钥对(这个也不会的话,方法自行Google)
1
| ssh-keygen -t rsa -b 4096 -f ~/.ssh/GitHub-actions-deploy
|
然后会获得一个公钥和私钥.
2、在GitHub Pages所在的仓库中添加 “公钥”
找到仓库的Settings
-Deploye keys
- Add deploy key
Title
填入:ACTION_DEPLOY_KEY
Key
填入:# 步骤 1 生成的密钥对中的公钥 即ssh-rsa开头的pub文件
- 必须勾上
Allow write access
3、在存放Hexo源代码文件的仓库中添加 “私钥”
PS:跟步骤2中的仓库可能是同一个,也可能不是同一个。根据自己的选型设置。
找到仓库的Settings
-Secrets
- Add a new secret
Name
填入:ACTION_DEPLOY_KEY
Value
填入:# 步骤 1 生成的密钥对中的私钥 没有后缀为pub的是
4、脚本配置
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| name: 自动部署 Hexo
on: push: branches: - main pull_request: branches: - main
jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [22.x] # 更新为 Node.js 22 steps: - name: 开始运行 uses: actions/checkout@v3
- name: 设置 Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }}
- name: 缓存 node_modules uses: actions/cache@v3 with: path: node_modules key: node-modules-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }} restore-keys: | node-modules-${{ runner.os }}-
- name: 配置 Git 环境 env: ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.name "FTDRTD" git config --global user.email "angomd@outlook.com"
- name: 安装 Hexo CI run: | export TZ='Asia/Shanghai' npm i -g hexo-cli npm i
- name: 安装插件 run: | npm install hexo-auto-issue@1.0.4 hexo-deployer-git@4.0.0 hexo-generator-archive@2.0.0 hexo-generator-category@2.0.0 hexo-generator-index@3.0.0 hexo-generator-search@2.4.3 hexo-generator-tag@2.0.0 hexo-renderer-ejs@2.0.0 hexo-renderer-marked@6.3.0 hexo-renderer-pug@3.0.0 hexo-renderer-stylus@3.0.1 hexo-server@3.0.0 hexo-theme-landscape@1.0.0 hexo-wordcount@6.0.1 --save
- name: 部署博客 run: | rm -rf .deploy_git hexo g && hexo douban && hexo deploy
- name: 清理 run: | rm ~/.ssh/id_rsa
- name: 检查构建状态 if: ${{ failure() }} run: | echo "构建失败,请检查日志以获取更多信息。"
- name: 设置 Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }}
- name: 缓存 node_modules uses: actions/cache@v3 with: path: node_modules key: node-modules-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }} restore-keys: | node-modules-${{ runner.os }}-
- name: 配置 Git 环境 env: ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.name "" git config --global user.email ""
- name: 安装 Hexo CI run: | export TZ='Asia/Shanghai' npm i -g hexo-cli npm i
- name: 安装插件 run: | npm install hexo-auto-issue@1.0.4 hexo-deployer-git@4.0.0 hexo-generator-archive@2.0.0 hexo-generator-category@2.0.0 hexo-generator-index@3.0.0 hexo-generator-search@2.4.3 hexo-generator-tag@2.0.0 hexo-renderer-ejs@2.0.0 hexo-renderer-marked@6.3.0 hexo-renderer-pug@3.0.0 hexo-renderer-stylus@3.0.1 hexo-server@3.0.0 hexo-theme-landscape@1.0.0 hexo-wordcount@6.0.1 --save
- name: 部署博客 run: | rm -rf .deploy_git hexo g && hexo douban && hexo deploy
- name: 清理 run: | rm ~/.ssh/id_rsa
- name: 检查构建状态 if: ${{ failure() }} run: | echo "构建失败,请检查日志以获取更多信息。"
|
5、修改一下Actions脚本
1 2 3
| git config --global user.name "" git config --global user.email "" git clone https://github.com/*/*.GitHub.io .deploy_git
|
修改为自己的数值
6、其他注意事项
- 脚本中插件部分可按自己实际用的插件删改,
npm ls --depth 0
可查看自己安装了哪些插件
- 如果没有用到
hexo douban
插件,部署命令:
hexo g && hexo douban && hexo deploy
可改为 hexo g -d
另外一个建议是,所使用的 theme
中的 主题目录
用 git subtree
添加为子项目去维护。
这样在多环境多终端发布文章时,不会 clone
或 pull
一个空 theme
。
7、git push
文章写好后,不需要在本地构建,只需要 git push
到GitHub仓库即可自动部署。
其中部署方式在Hexo根目录的 _config.yml
中配置。