GitHub Actions部署Hugo站点

使用私有仓库存放博客源码, 将Hugo构建好的public目录推送到公有仓库来发布, 参考peaceiris/actions-gh-pagesREADME

  1. 创建一个私有仓库用来存放博客源码。
  2. 创建一个公有仓库用来发布博客。
  3. 创建一个个人token, 名字为了方便记住它的用途, 可以叫做blog
  4. 到期日期选择No expiration
  5. 作用域选择repoworkflow
  6. 生成token, 复制它的值。
  7. 到博客源码仓库的SettingsSecrets中新建一个Actions secrets, 名为blog, Value粘贴上一步的个人token的值。
  8. 在博客源码仓库的根目录下创建.github/workflows/gh-pages.yml文件, 自定义使用以下代码, 然后提交:
 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
name: GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3.0.0
        with:
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2.4.13
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --gc --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3.7.3
        with:
          github_token: ${{ secrets.blog }}
          external_repository: #用来发布博客的公有仓库名称
          publish_branch: master
          publish_dir: ./public
          cname: #填写自定义域名
  1. 创建一个公有仓库用来存放博客源码。
  2. 在博客源码仓库的根目录下创建.github/workflows/gh-pages.yml文件, 自定义使用以下代码, 然后提交:
 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
name: GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3.0.0
        with:
          submodules: false
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2.4.13
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --gc --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3.7.3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }} #GITHUB_TOKEN不需要手动设置, GitHub会分配临时token
          publish_dir: ./public
          cname: #填写自定义域名
警告

参考文章GitHub Pages使用问题, 如果CNAME设置为www域名, 那么需要如下设置DNS:

类型名称内容
CNAMEwwwbahuangshanren.github.io
CNAMEbahuangshanren.techwww.bahuangshanren.tech
  1. CircleCICloudflare PagesNetlifyTravis CIVercel等。
警告
Cloudflare Pages的.pages.dev域名似乎已被墙, 如果使用Cloudflare Pages, 建议自定义域名, 然后DNS设置为Cloudflare代理。
  1. 使用Nginx在自己服务器上部署, 参考Nginx部署Hugo站点