迁移到 github pages 以后,还会面临一个问题,就是 pages 其实只需要 hugo 生成的 public 目录下的内容,但是我们也需要找一个地方存储 hugo 的程序,和原始的文章,这样我们就需要另一个仓库,这个仓库最好是私有的,当我们提交的时候,可以发布到 pages 的仓库去。这个时候 github action 就派上用场了。

当然要先做一些准备,

  1. 创建私有仓库存储原始代码
  2. 生成 Deploy Key https://github.com/marketplace/actions/github-pages-action#%EF%B8%8F-create-ssh-deploy-key
  3. 开始配置action
 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
# Workflow to build and deploy site to Github Pages using Hugo

# Name of Workflow
name: github pages

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: 
      - master

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "deploy"
  deploy:
    # The type of runner that the job will run on
    runs-on: ubuntu-18.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

    # Step 1 - Checks-out your repository under $GITHUB_WORKSPACE
    - name: Checkout
      uses: actions/checkout@v2
      with:
        submodules: true  # Fetch Hugo themes
        fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

    # Step 2 - Sets up the latest version of Hugo
    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: 'latest'

    # Step 3 - Clean and don't fail
    - name: Clean public directory
      run: rm -rf public

    # Step 4 - Builds the site using the latest version of Hugo
    - name: Build
      run: hugo --minify

    # Step 6 - Push our generated site to our gh-pages branch
    - name: Deploy
      uses: peaceiris/actions-gh-pages@v3
      with:
        deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
        external_repository: fyzzy1943/fyzzy1943.github.io
        publish_branch: master
        publish_dir: ./public
        cname: www.fordawn.com

暂时记录这么多,抽空增加详细步骤。