目录

GitHub Action 使用私有仓库 Submodule

GitHub Actions + 私有 Submodule(PAT)配置记录

仓库结构

  • A 源码仓库(主仓库)

    • 存放 Hugo 博客源码
    • 运行 GitHub Actions
    • 分支:main
  • B 部署仓库

    • 仓库名:hex2rgb.github.io
    • 存放构建后的静态文件(public/
    • 用于 GitHub Pages 访问
  • C 主题仓库(Submodule)

    • 仓库名:hex2rgb/LoveIt
    • 私有仓库
    • 位置:themes/LoveIt
    • 引入方式:Git Submodule

调用关系

A(源码仓库) ├── content/ ├── config/ ├── themes/ │ └── LoveIt(submodule → C) └── .github/workflows/

GitHub Actions 流程:

  1. 拉取主仓库代码
  2. 拉取私有主题子模块 C
  3. Hugo 构建生成 public
  4. 推送产物到部署仓库 B

认证方式

统一使用 PAT(Personal Access Token):

  • 全程走 HTTPS 方式访问仓库
  • 用于:
    • 拉取私有 submodule(C)
    • 推送部署仓库(B)

关键配置点

  • Submodule(C)为私有仓库,CI 默认无访问权限
  • 必须通过 PAT 配置 Git 鉴权
  • 所有 Git 操作统一使用 HTTPS + Token

一句话总结

源码仓构建,静态仓托管页面,私有主题用子模块管理,PAT 打通访问链路。


已跑通的 workflow 配置

name: Deploy Hugo site

on:
  push:
    branches: ["main"]
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      # 拉取主仓库代码,禁用自动 submodule
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: false
          fetch-depth: 0
          token: ${{ secrets.PAT_GITHUB_PAGES }}

      # 全局配置 Git 鉴权,强制所有请求携带 Token
      - name: Setup Git Auth
        run: |
          git config --global url."https://${{ secrets.PAT_GITHUB_PAGES }}@github.com/".insteadOf "https://github.com/"

      # 手动拉取私有子模块
      - name: Pull submodules
        run: |
          git submodule sync --recursive
          git submodule update --init --force --recursive

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

      - name: Build Hugo
        run: |
          hugo --minify --baseURL "https://hex2rgb.github.io/"

      # 推送到部署仓库
      - name: Deploy to hex2rgb.github.io
        uses: cpina/github-action-push-to-another-repository@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.PAT_GITHUB_PAGES }}
        with:
          source-directory: public
          destination-github-username: hex2rgb
          destination-repository-name: hex2rgb.github.io
          target-branch: main
          clean: true
          commit-message: "deploy: update site"

附件

  • 图 1 账号PAT 设置

    /images/Github-Action使用私有仓库/1.png

  • 图 2 源码仓库 Secret配置

    /images/Github-Action使用私有仓库/2.png