使用 github action 发布 hexo

本文最后更新于:2022年7月6日 上午

最近想着搞一下 github action,体验一下自动化的威力。我可真是完完全全的零基础,搞了半天才搞定,记录一下吧

准备工作

准备两个仓库,如下:

仓库说明
bloghexo 项目,存放博客源码的地方
floiges.gitub.iohexo 编译后的静态文件,即博客

配置密钥

ssh-keygen -f github-hexo-deploy-key

此命令会生成 github-hexo-deploy-key 私钥 和 github-hexo-deploy-key.pub 公钥。

配置 blog 项目

github 打开 blog 仓库 => settings => secrets => add new secrets

  • Name 名字随便起,但是要记住,后面有用,例如:HEXO_DEPLOY_SECRETS
  • Value 输入 github-hexo-deploy-key 私钥的内容

配置 flogies.gitub.io 项目

github 打开 floiges.gitub.io 仓库 => settings => keys => add deploy key

  • Title 名字随便起,但是要记住,例如: HEXO_DEPLOY_PUB
  • Key 输入 github-hexo-deploy-key.pub 公钥的内容

github action

打开 blog 仓库 => Actions => Set up this workflow,内容如下,可做参考:

name: HEXO CI

on: [push] # 有提交时触发

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

    steps:
      - uses: actions/checkout@v1

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

      - name: Configuration environment
        env:
          HEXO_DEPLOY_SECRETS: ${{secrets.HEXO_DEPLOY_SECRETS}} # blog 项目内新增的 secrets key
        run: |
          mkdir -p ~/.ssh/
          echo "$HEXO_DEPLOY_SECRETS" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name "floiges"
          git config --global user.email "floiges@163.com"
          git clone --branch yadong_custom --depth=10 git@github.com:floiges/hexo-theme-next.git themes/next
          git checkout -b yadong_custom
          git clone git@github.com:floiges/theme-next-three --depth=1 themes/next/source/lib/three
      - name: Install dependencies
        run: |
          npm i -g hexo-cli
          npm i

      - name: Deploy hexo
        run: |
          hexo g -d

End

现在 blog 项目里有提交时,就会触发自动构建了~


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

 目录