GitHub Actions 如何检出递归子模块?

解读

在国内 CI/CD 面试中,这道题常被用来区分“只会用 actions/checkout”与“真正落地过多仓库协同”的候选人。
面试官真正想听的是:

  1. 你知道子模块与递归子模块的区别;
  2. 你清楚 GitHub Actions 默认不会拉取子模块,更不会递归;
  3. 你能给出至少两种官方或社区认可的写法,并指出各自的适用场景与隐患(性能、权限、安全)。
    如果只答“加 recursive: true”只能拿到 60 分;能把 token 透传、深度克隆、部分克隆、缓存、私有子模块权限、国内网络加速都串起来,才能拿到 90+。

知识点

  • 子模块(submodule)与嵌套子模块(nested submodule,即递归子模块)
  • Git 两次克隆机制:先 clone 父库,再 git submodule update --init --recursive
  • actions/checkout 的三个关键参数:submodule、token、persist-credentials
  • GitHub 自动生成的 GITHUB_TOKEN 权限范围:仅对当前仓库有效,子模块若为私有需 PAT 或 GitHub App 安装令牌
  • 国内镜像加速:url.<base>.insteadOf 与 git config --global
  • 部分克隆与 blobless/filtter 选项,减少大仓传输时间
  • 缓存策略:actions/cache 对 .git/modules 做二次命中,避免每次重新拉子模块
  • 安全红线:PAT 不能硬编码在 yaml,应使用 repository secret 或 OIDC+Cloud Vault

答案

给出三种在国内生产环境验证过的写法,按推荐顺序排列。

方案一:官方 actions/checkout 一步到位(公有子模块、网络良好场景)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo and recursive submodules
        uses: actions/checkout@v4
        with:
          # 关键字段
          submodules: recursive
          # 若子模块里还有私有仓,需要把 token 透传下去
          token: ${{ secrets.GITHUB_TOKEN }}   # 仅当前仓库为私有且子模块同属一个 owner 时可复用
          # 国内加速,提前配置 insteadOf
          persist-credentials: true
      - name: Just in case, double check
        run: git submodule status --recursive

方案二:私有子模块或跨组织场景,使用 PAT 并显式 update

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.SUBMODULE_PAT }}   # 在 Settings/Secrets 里配置
          submodules: false                      # 先不拉,避免 token 作用域问题
      - name: Configure insteadOf for国内镜像
        run: |
          git config --global url."https://github.com.cnpmjs.org/".insteadOf "https://github.com/"
      - name: Init and update recursive
        run: |
          git submodule update --init --recursive --depth=1

方案三:大仓+嵌套子模块,开启部分克隆与缓存,兼顾速度与费用

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Restore submodule cache
        uses: actions/cache@v3
        with:
          path: .git/modules
          key: ${{ runner.os }}-submodules-${{ hashFiles('.gitmodules') }}
          restore-keys: |
            ${{ runner.os }}-submodules-
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          token: ${{ secrets.SUBMODULE_PAT }}
          # 部分克隆,减少 blob 传输
          fetch-depth: 1
          filter: blob:none
      - name: Ensure nested submodules are up to date
        run: git submodule foreach --recursive 'git sparse-checkout init --cone || :'

小结:

  1. 公有子模块直接 submodules: recursive 即可;
  2. 只要出现私有仓,就必须换 PAT 或 GitHub App 令牌,且不能硬编码;
  3. 国内网络差或仓库大,用 insteadOf + 部分克隆 + 缓存三板斧;
  4. 最后一定加 git submodule status --recursive 做二次校验,避免“以为拉全了其实没拉到嵌套仓”的坑。

拓展思考

  1. 如果子模块使用 SSH 地址,而 GitHub Actions 默认没有私钥,你会如何无密码拉取?
    答:在 job 里注入 SSH 私钥(secret),写 ~/.ssh/id_ed25519,并在 known_hosts 预置 github.com 的 RSA/ECDSA 指纹;或者统一把 .gitmodules 里的 url 改成 HTTPS,再配 insteadOf。
  2. 当子模块体积超过 5 GB,checkout 步骤超时,你有哪些优化手段?
    答:
    • 启用 blobless clone(--filter=blob:none)或 treeless clone;
    • 把子模块拆成独立 job,用 actions/cache 跨 job 共享;
    • 使用 Git LFS 且仅拉取当前分支的 LFS 对象(lfs: true + fetch-depth: 1);
    • 终极方案:把只读大文件抽成独立产物仓,CI 里用 http 下载代替 submodule。
  3. 安全角度,PAT 泄露风险最高,如何用 GitHub App 替代?
    答:创建 GitHub App,赋予其“Contents”只读权限,安装在父仓库和子模块仓库;在 workflow 里使用 actions/create-github-app-token 换取临时安装令牌,生命周期仅当前 job,避免 PAT 长期有效的风险。