Hugo博客搭建流程

博客自动化部署到github pages流程

使用私有仓库A → 构建 Hugo → 部署到公开仓库B(xxxxx.github.io)

第一步:创建 Personal Access Token (PAT)

你需要一个有权限写入目标仓库的 token。

创建 PAT 前往:https://github.com/settings/tokens

填写

  1. Token name: deploy-to-xxxxxx.github.io
  2. Expiration: 选择 90 天或自定义
  3. Repository access: Only select repositories
  4. 勾选:公开仓库B
  5. Permissions
    Repository contents: Read and write

点击 Generate token,复制生成的 token(形如 ghp_xxx…)并保存!

关掉页面后就再也看不到了!

第二步:在私有仓库添加 Secret

  1. 进入私有仓库的 settings → secrets → actions
  2. 点击 New repository secret
    Fieldvalue
    NamePERSONAL_TOKEN
    value粘贴第一步生成的token

第三步:配置 CI 文件(.github/workflows/ci.yml)

在根目录中创建:

.github/workflows/ci.yml

文件内容如下:

name: Deploy Hugo Site

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout source
        uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0

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

      - name: Build
        run: hugo --minify --baseURL "域名"

      - name: Deploy to esperanzaee.github.io
        uses: peaceiris/actions-gh-pages@v3
        with:
          personal_token: ${{ secrets.PERSONAL_TOKEN }}
          publish_dir: ./public
          external_repository: 用户名/仓库名
          publish_branch: main
          allow_empty_commit: true
          keep_files: false
          enable_jekyll: false
          commit_message: ${{ github.event.head_commit.message }}

第四步:配置 Cloudflare DNS(如果自己有域名的话)

  • 登录 Cloudflare,选择或者添加自己的域名

  • 添加 CNAME 记录(指向 GitHub Pages)

    字段
    TypeCNAME
    Nameblog
    Contentxxxxx.github.io
    Proxy status❌ 灰色云(关闭代理)
    TTLAuto
  • 等待DNS生效(可能5min到48h)

第五步:在 GitHub 上绑定自定义域名

  • 进入私有仓库的settings → Pages
  • 设置Custom Domain,输入自己的域名,Save

第六步:提交并触发部署

git add .
git commit -m "feat: setup CI and deploy"
git push origin main

Hugo 子模块主题更新流程

适用场景:你使用 Git 子模块管理主题,且已 Fork 主题仓库,希望更新并提交变更。

📌 前提条件(仅需执行一次)

如果你是第一次添加该子模块,请在项目根目录执行:

# 添加子模块(仅首次执行)
git submodule add 目标仓库地址 themes/hugo-binder

# 提交子模块初始化
git add .gitmodules themes/hugo
git commit -m "feat: add hugo theme as submodule"
git push origin main

✅ 提示:.gitmodules 文件会记录子模块的 URL 和路径。

日常更新流程(当你在 themes/xxxx 中修改代码时)

  1. 进入子模块目录,提交并推送主题本身的更改
cd themes/hugo

确保你在正确的分支(通常是 main 或 master):
git checkout main

拉取最新代码(避免冲突):
git pull origin main

进行你的修改(如修复配置、样式等),然后提交:
git add .
git commit -m "Fix: remove theme = hugo-looseleaf to prevent nesting"
git push origin main

✅ 这一步会把主题的更新推送到你的 Fork 仓库

  1. 回到主项目,更新子模块指针
cd ../../  # 回到 Hugo 项目根目录

此时主项目会显示子模块有“新提交”:

git status
# 输出类似:modified:   themes/hugo (new commits)

将子模块的引用更新到最新提交:

git add themes/hugo
git commit -m "chore: update submodule hugo to latest commit"
git push origin main

✅ 这一步会触发 GitHub Actions 构建并部署你的博客。

补充建议:同步上游主题更新(可选)

如果你希望合并原主题作者的更新(比如修复 bug、新增功能):

git remote -v                  # 查看当前远程仓库
git remote add upstream 上游仓库地址  # 添加上游仓库(仅一次)
git fetch upstream             # 获取上游更新
git merge upstream/main        # 合并到本地
git push origin main           # 推送到你的 Fork
cd ../../
git add themes/hugo
git commit -m "chore: sync upstream changes for hugo theme"
git push origin main```
End...