使用github action发布hugo到github page

2021-06-25, updated 2021-09-12

可以使用github action来进行发布hugo

hugo发布到新的仓库作为github page

在github仓库上选择action,选择set up a workflow yourself ,将下面的内容填入打开的编辑框。

external_repository配置为对应的github page的仓库.

示例代码

 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: github pages

on:
  push:
    branches:
      - main  # Set a branch to deploy
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/[email protected]
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/[email protected]
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/[email protected]
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: xxxx # 发布到的repository. 例如: https://github.com/torvalds/linux.git填入torvalds/linux
          publish_branch: main  # default: gh-pages
          publish_dir: ./public

问题一: 将对应的key填入两个github仓库

  1. 生成key

    1
    
    ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/action_deploy_key`
    

    会生成两个文件action_deploy_key.pubaction_deploy_key

  2. 选择external_repository仓库的Settings-->Deploy keys-->Add deploy key

    • Title中填入ACTIONS_DEPLOY_KEY
    • Key中填入action_deploy_key.pub文件内容
  3. 选择当前仓库的Settings-->Secrets-->New repository secret

    • Name中填入ACTIONS_DEPLOY_KEY
    • Value中填入action_deploy_key文件内容

问题二: 自定义域名

添加CNAME到hugo的staic目录,内容为custom-domain.com

详细内容查看 https://docs.github.com/cn/actions/learn-github-actions

words: 652 tags: github-action hugo site