Hexo Blog CI by GitHub Action

本地修改代码提交Github时自动触发Hexo代码更新

静态博客简单,但是发布博文时稍显麻烦,一般需要下面两步:

GitHub Action - Hexo CI/CD

步骤一:为仓库设置访问密钥

  1. 生成密钥

打开命令行并生成一组密钥

ssh-keygen -f github-deploy-key -C "HEXO CD"

记得跳过密码啥的,这里连按回车就好

您将获得 2 个文件:

github-deploy-key.pub    是公钥

github-deploy-key    是私钥
  1. 上传公钥:

博客源码存储库(https://github.com/zhenzhang20/zhenzhang20.github.io) 中进行设置,转到Deploy Keys并使用Allow write access添加您的公钥 github-deploy-key.pub,name写为HEXO_DEPLOY_PUB,指定用途,方便后面维护,内容为 github-deploy-key.pub文件内的所有内容。记得勾选允许写入权限

  1. 上传私钥:

转到源码存储库(https://github.com/zhenzhang20/blog) 中进行设置,github blog仓库地址 → Settings → Secrets and variables → action → New repository secret,name写为HEXO_DEPLOY_PRI,内容为 github-deploy-key文件内的所有内容

  1. 如果在github上删除了已经上传的密钥,则需要重新生产新的密钥进行上传。

不要轻易透露你的私钥

步骤二:配置Github Workflows

配置github action

在本地 blog 根目录下创建 .github/workflows/deploy.yml 文件,目录结构如下

blog (repository)  
└── .github  
└── workflows  
└── deploy.yml

deploy.yml 的内容如下

# workflow name
name: Hexo Blog CI

# main branch on push, auto run
on: 
  push:
    branches:
      - main

jobs:
  build: 
    runs-on: ubuntu-latest 

    steps:
    # check it to your workflow can access it
    # from: https://github.com/actions/checkout
    - name: Checkout Repository main branch
      uses: actions/checkout@main 

    # from: https://github.com/actions/setup-node  
    - name: Setup Node.js 12.x 
      uses: actions/setup-node@main
      with:
        node-version: "12.x"

    - name: Setup Hexo Dependencies
      run: |
        npm install hexo-cli -g
        npm install

    - name: Setup Deploy Private Key
      env:
        HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
      run: |
        mkdir -p ~/.ssh/
        echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa 
        chmod 600 ~/.ssh/id_rsa
        ssh-keyscan github.com >> ~/.ssh/known_hosts

    - name: Setup Git Infomation
      run: | 
        git config --global user.name 'zhen_zhang20' 
        git config --global user.email 'zhen_zhang20@163.com'
    - name: Deploy Hexo 
      run: |
        hexo clean
        hexo generate 
        hexo deploy

  • 可能遇到报错:
    INFO  Copying files from extend dirs...
    fatal: in unpopulated submodule '.deploy_git'
    FATAL {
    err: Error: Spawn failed
        at ChildProcess.<anonymous> (/home/runner/work/blog/blog/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
        at ChildProcess.emit (events.js:314:20)
        at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12) {
      code: 128
    }
    } Something's wrong. Maybe you can find the solution here: %s https://hexo.io/docs/troubleshooting.html
    Error: Process completed with exit code 2.
INFO  Copying files from extend dirs...
fatal: in unpopulated submodule '.deploy_git'
FATAL {
  err: Error: Spawn failed
      at ChildProcess.<anonymous> (/home/runner/work/blog/blog/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
      at ChildProcess.emit (events.js:314:20)
      at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12) {
    code: 128
  }
} Something's wrong. Maybe you can find the solution here: %s https://hexo.io/docs/troubleshooting.html
Error: Process completed with exit code 2.

解决方法:可能是没有权限进行 删除/更新 “.deploy_git”。增加“rm -rf .deploy_git” 来解决。

- name: Deploy Hexo 
  run: |
    rm -rf .deploy_git
    hexo clean
    hexo generate 
    hexo deploy
  • 可能遇到报错:
    To github.com:zhenzhang20/zhenzhang20.github.io.git
    + 78f5aeb...8311e98 HEAD -> main (forced update)
    branch 'master' set up to track 'git@github.com:zhenzhang20/zhenzhang20.github.io.git/main'.
    On branch master
    nothing to commit, working tree clean
    Host key verification failed.
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    FATAL {
    err: Error: Spawn failed
        at ChildProcess.<anonymous> (/home/runner/work/blog/blog/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
        at ChildProcess.emit (events.js:314:20)
        at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12) {
      code: 128
    }
    } Something's wrong. Maybe you can find the solution here: %s https://hexo.io/docs/troubleshooting.html
    Error: Process completed with exit code 2.

解决方法:先主动提交一次 博客源码,之后再用持续集成方式提交

步骤三:提交Blog代码

在Git Bash下,转到本地 blog 目录,运行下述命令提交代码

git add .
git commit -m 'initial'
git push origin main

提交后,检查github.com/zhenzhang20/blog仓库是否被更新,同时检查blog下的action 是否有正常运行,再检查博客仓库是否被自动更新


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