GitHub Actions 自动部署 Hexo 脚本

OpenAI ChatGPT3.5
Pre-generated by ChatGPT 3.5
AI 接口失联……

网上虽然有很多通过 GitHub Actions 自动部署 Hexo 的教程,但都有各种各样的问题。
主要问题还是 Workflow 脚本没有写正确,比如插件部分。

1
ssh-keygen -t rsa -b 4096 -f ~/.ssh/GitHub-actions-deploy

然后会获得一个公钥和私钥。

找到仓库的 Settings - Deploye keys - Add deploy key

  • Title 填入:ACTION_DEPLOY_KEY
  • Key 填入:# 步骤 1 生成的密钥对中的公钥
    勾上 Allow write access

PS: 跟步骤 2 中的仓库可能是同一个,也可能不是同一个。根据自己的选型设置。

找到仓库的 Settings - Secrets - Add a new secret

  • Name 填入:ACTION_DEPLOY_KEY
  • Value 填入:# 步骤 1 生成的密钥对中的私钥

下文贴出的 Actions 的 Wordflows 脚本中,其中 3 个地方要改为自己的信息:

1
2
3
git config --global user.name "eallion"
git config --global user.email "eallion@eallion.com"
git clone https://github.com/eallion/eallion.GitHub.io .deploy_git
  • 脚本中插件部分可按自己实际用的插件删改,npm ls --depth 0 可查看自己安装了哪些插件
  • 如果没有用到 hexo douban 插件,部署命令:
    hexo g && hexo douban && hexo deploy 可改为 hexo g -d

另外一个建议是,所使用的 theme 中的 主题目录git subtree 添加为子项目去维护。
这样在多环境多终端发布文章时,不会 clonepull 一个空 theme

文章写好后,不需要在本地构建,只需要 git push 到 GitHub 仓库即可自动部署。
其中部署方式在 Hexo 根目录的 _config.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
name: 自动部署 Hexo

#on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]

    steps:
      - name: 开始运行
        uses: actions/checkout@v1

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

      - 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 "eallion"
          git config --global user.email "eallion@eallion.com"

      - name: 安装 Hexo CI
        run: |
          export TZ='Asia/Shanghai'
          npm i -g hexo-cli
          npm i

      - name: 安装插件
        run: |
          npm install hexo-renderer-pug --save
          npm install hexo-renderer-stylus --save
          npm install hexo-deployer-git --save
          npm install hexo-generator-search --save
          npm install hexo-douban --save
          npm install hexo-generator-feed --save
          npm install hexo-abbrlink --save
          npm install hexo-wordcount --save

      - name: 部署博客
        run: |
          rm -rf .deploy_git
          hexo g && hexo douban && hexo deploy
          rm ~/.ssh/id_rsa

/assets/images/posts/2019/12/20191228232334.png