Hugo静态博客自动部署

两个Github Repos:原始Repo: PDCA-blog 和目标Repo: hustbill.github.io

所有的原始博客文件(post/2021-02-20-post*.md) 存放在原始Repo: PDCA-blog ,Hugo Build出来的public文件夹则存放在目标Repo: hustbill.github.io

以前都是手动运行脚本提交,并且部署到目标Repo。每次需要2-5秒时间,总归是有点不方便。

今天上午,参考了这篇博文:Automate your GitHub Pages Deployment using Hugo and Actions,有了peaceiris/actions-gh-pages, 可以自动调用Hugo build,把原始Repo: PDCA-blog 更新文件,重新build,并且部署到目标Repo: hustbill.github.io,完全不需要人工干预,省心省力。

剩下的只需要安心写字了。

详细步骤

Read Create SSH Deploy Key, create your SSH deploy key, and set the deploy_key option like the following

  1. Generate your deploy key with the following command.
1
2
3
4
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""
# You will get 2 files:
#   gh-pages.pub (public key)
#   gh-pages     (private key)
  1. Add your private key as a secret with name ACTIONS_DEPLOY_KEY in the Hugo Source Repository. Navigate to Your Hugo Source Repository > Settings > Secrets > Add new secrets and the name the secret as ACTIONS_DEPLOY_KEY and paste the contents of master file in the value.

  2. Add your public key as deployment key in the GitHub pages repository Navigate to Your GitHub pages Repository > Settings > Deploy Key> Add new deploy key and set title something you can relate to the private key (like Public Key for my site deploy) and paste the contents of master.pub file in the value.

  3. Add below file .github/workflows/pages.yml location of your Hugo source code and replace username/external-repository with your GitHub username and GitHub pages repository name.

 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
name: hugo publish

on:
  push:
    branches:
    - master  # Set a branch name to trigger deployment

jobs:
  build-deploy:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v2
      with:
        submodules: true  # Fetch Hugo themes (true OR recursive)
        fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: '0.80.0'

    - name: Build
      run: hugo --minify

    - name: Deploy
      uses: peaceiris/actions-gh-pages@v3
      with:
        deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
        PUBLISH_DIR: ./public
        EXTERNAL_REPOSITORY: hustbill/hustbill.github.io
        PUBLISH_BRANCH: source
        allow_empty_commit: false
        commit_message: ${{ github.event.head_commit.message }}

Hugo publish 截图

Check the hugo publish status: repo -> Actions

hugo publish-All workflows Showing runs from all workflows

参考链接

  1. peaceiris/actions-gh-pages

  2. Automate your GitHub Pages Deployment using Hugo and Actions