Hugo自动化构建静态网站-GithubPages

通过github workflow

搭建自己的博客中已经知道了如何使用github pages来部署hugo生成的静态网站.

但是由于我们日常编辑的文件都是hugo项目的content目录下,因此必须得先本地build生成了最新版的public文件后,再push到github上,这个过程还是有些复杂。

作为一个懒人,希望可以写完文章push就可以自动构建最新内容,本文就来介绍一下如何通过github actions来自动化构建静态网站。

出于个人原因,我这边的存储博客文件的hugo项目是单独一个git项目(一个私有项目),编译生成的public下的内容是另一个git项目(公开项目),但实际上如果你不介意文章、配置等完全公开的话,只使用hugo项目也可以做到,下面分别介绍一下这两种方案。

区分项目

需要新建两个项目,其中一个private项目用来存储hugo本身的内容,另一个public项目用来存储编译后的内容.

比如,我这里用hugo-demo来用做private项目,内容就是hugo初始化后的项目,我这里将public目录添加到了.gitignore中,避免编译后的内容上传

deploy.yml

在当前项目新建.github/workflow/deploy.yml文件,内容如下:

name: GitHub Pages

on:
  push:
    branches:
      - main  # hugo-demo当前项目(即运行actions的项目的触发分支)
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - uses: actions/checkout@v3
        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.140.0'
          # 是否启用 hugo extend
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ github.ref == 'refs/heads/main' }} #这里要和上面hugo-demo触发的分支一样
        with:
          PERSONAL_TOKEN: ${{ secrets.ACTION_TOKEN }}
          EXTERNAL_REPOSITORY: qisiii/hugo-demo.github.io  # 你的GitHub Pages仓库
          PUBLISH_BRANCH: master  # hugo-demo.github.io项目对应的分支
          PUBLISH_DIR: ./public
          commit_message: ${{ github.event.head_commit.message }}
          # cname: #如果要

其中有三个核心的地方,一个是PERSONAL_TOKEN,一个是EXTERNAL_REPOSITORY,最后则是cname;

PERSONAL_TOKEN

我用的方式是,生成一个专门只是为workflow用的token,假设生成的token为ghp_xxxxxxxx;

在当前仓库-setting-Secrets And Variables种新增一个secrets,名字为ACTION_TOKEN,value为ghp_xxxxxxxx;

EXTERNAL_REPOSITORY

新建一个public项目专门用来存储编译后的内容,如代码中所示,我这里配置的是hugo-demo.github.io

CNAME

如果需要自定义域名的话,在deploy中最后要用cname填上对应的域名,不然不会生效

此时,改动hugo-demo任意内容并进行commit,就会发现项目的Actions这里会有一个执行中的workflow在触发分支,触发条件,实际部署项目,实际部署分支都正确的情况下,就会如下图一样,将编译后的内容输出到我们对应的项目

后续的将项目使用github pages功能暴露就和之前一样了

不区分项目

如果使用同一个项目,由于githubpages要求项目必须是public,所以整个项目都得是public。

除了之外,就只需要修改deploy.yml,其他都和上面一样,最核心的便是,EXTERNAL_REPOSITORY得填当前项目,或者没有这行都可以;PUBLISH_BRANCH 一般会另选一个分支来用作部署pages

name: GitHub Pages

on:
  push:
    branches:
      - master  # hugo-demo当前项目(即运行actions的项目的触发分支)
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - uses: actions/checkout@v3
        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.140.0'
          # 是否启用 hugo extend
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ github.ref == 'refs/heads/master' }} #这里要和上面hugo-demo触发的分支一样
        with:
          PERSONAL_TOKEN: ${{ secrets.ACTION_TOKEN }}
          EXTERNAL_REPOSITORY: qisiii/hugo-demo-page  #这行不写都行
          PUBLISH_BRANCH: pages  # 当前项目的另一个专用部署分支
          PUBLISH_DIR: ./public
          commit_message: ${{ github.event.head_commit.message }}
          # cname: 

参考:

基于 Github Action 自动构建 Hugo 博客 -

【Hugo网站搭建】GitHub Action自动化部署Hugo博客 | Eddy’s blog

使用 Github Actions 自动部署 Hugo 博客 | The Ship of Zenlian

Licensed under CC BY-NC-SA 4.0
最后更新于 2025-11-06