解决 CircleCI 报错 git submodule update failed 权限缺失修复
排查 CircleCI 构建包含 Git Submodule 依赖的工程时,执行检出报错 Permission denied (publickey) 或 fatal: clone of 'git@github.com:...' into submodule path failed,分析 用户密钥(User Key)与只读部署密钥(Deploy Key)限制。
问题现象
CircleCI 检出包含子模块的项目时,在执行子模块更新阶段突然崩溃:
Cloning into 'libs/shared-core'...
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
触发根因
- CircleCI 默认自动生成的 Deploy Key 仅对当前主代码仓库具备只读权限;
- 如果子模块存放在另一个独立的私有代码仓库中,默认的 Deploy Key 无法跨仓库访问目标子模块;
- 缺少能够同时读取两个私有仓库的专用 User Key 或机器用户凭证(Machine User)。
解决步骤
在 CircleCI 项目设置中添加 User Key 赋予跨仓库只读权限
前往 Project Settings -> SSH Keys -> User Key 点击 Authorize with GitHub 创建一个拥有跨仓库读取权限的凭证。
在 .circleci/config.yml 的检出步骤中启用 submodules: true
标准声明:
steps:
- checkout
- run:
name: Update Submodules
command: |
git submodule sync
git submodule update --init --recursive
或者在子模块的 .gitmodules 中改用带有个人访问令牌(PAT)的 HTTPS 规范格式
在 CI 中注入 Token。
验证确认
重新运行 CircleCI 任务:
所有多层嵌套子模块在 2 秒内顺利递归检出,主工程完整无缺,后续编译畅通无阻。


