Git 第 4 小时:走向托管平台(AWS CodeCommit 与 GitLab)
- 说清 AWS CodeCommit 从 2024-07 停止接纳新客户到 2025-11-24 恢复全面可用(GA)的经过与现状,知道 Git LFS 等路线图项目的状态要去哪里查;能区分 GitLab.com(SaaS)、GitLab Dedicated 与 GitLab Self-Managed。
- 为 CodeCommit 选择正确的认证方式:联合身份/临时凭据 → git-remote-codecommit(AWS 推荐);IAM 用户最简单的是 HTTPS Git 凭据;SSH 密钥与 AWS CLI 凭据助手作为备选。能讲清 git-remote-codecommit 用 IAM 临时凭据做 SigV4 签名的过程。
- 在 CodeCommit 上用 IAM 条件策略(
codecommit:References+Null)拒绝推送到 main,用审批规则模板要求 PR 审批,用通知规则与触发器接入 SNS/Lambda。 - 用 AWS CodeConnections(原 CodeStar Connections)把 GitHub/GitLab 仓库接到 CodePipeline 等服务,理解
PENDING → AVAILABLE状态与UseConnection/PassConnection权限。 - 在 GitLab 上配置受保护分支(Allowed to merge / Allowed to push and merge)、Merge Request、审批规则与 CODEOWNERS、推送规则,并清楚哪些功能需要 Premium/Ultimate。
- 搭建 GitLab → CodeCommit 推送镜像,能诊断 403、凭据助手冲突、受保护分支拒绝、大文件与 LFS 差异等问题,并完成平台间迁移。
1 小时时间分配表
| # | 时间 | 模块 | 核心内容 | 动画 / 交互 | 实验 |
|---|---|---|---|---|---|
| 0 | 课前 | 热身与平台账号准备 | AWS 账号与 IAM Identity Center、GitLab.com 账号、AWS CLI v2、git-remote-codecommit、glab;复用 myapp 练习仓库 | 环境自检清单 | aws sts get-caller-identity、glab auth status |
| 1 | 00–05 | AWS 与 GitLab 的 Git 服务定位 | CodeCommit 2024 停新、2025-11 恢复 GA、路线图;GitLab SaaS / Dedicated / 自管;套餐差异 | CodeCommit 时间轴 + 平台选择器 | 查询可用区域与账号状态 |
| 2 | 05–13 | CodeCommit:认证与 git-remote-codecommit | 四种连接方式与推荐顺序、IAM Identity Center 配置文件、Git 凭据、SSH、aws codecommit CLI | 核心动画:GRC 用临时凭据做 SigV4 签名 | 用 SSO 配置文件克隆、推送 myapp |
| 3 | 13–21 | CodeCommit:分支保护、审批规则与通知 | IAM Deny + codecommit:References + Null;审批规则模板;通知规则;触发器 | IAM 策略求值器 | 拒绝推 main + 2 人审批 PR |
| 4 | 21–28 | CodeConnections:连接 GitHub/GitLab 到 AWS | 支持的提供商、PENDING 握手、主机资源、UseConnection/PassConnection、条件键 | 连接状态机 | 创建 GitLab 连接并完成握手 |
| 5 | 28–37 | GitLab:项目、受保护分支与 MR | 群组/项目/角色、受保护分支选项、MR 合并方式、glab CLI | 核心动画:受保护分支拒绝 push | 保护 main、走 MR 合并 |
| 6 | 37–44 | GitLab:审批规则、CODEOWNERS 与推送规则 | 审批规则与设置、CODEOWNERS 位置与语法、推送规则与套餐 | MR 审批计算器 | CODEOWNERS + 推送规则(Premium 试用) |
| 7 | 44–54 | 跨平台镜像与排错实战 | 推/拉镜像、CodeCommit 403、凭据助手冲突、受保护分支拒绝、大文件与 LFS | 镜像同步模拟器 + 交互排障树 | GitLab → CodeCommit 推送镜像 |
| 8 | 54–60 | 迁移、成本与自测 | GitHub/Azure Repos ↔ GitLab/CodeCommit 迁移、CodeCommit 计费、GitLab 套餐、自测 | 成本计算器 | git push --mirror 分批迁移 |
| — | 课后 | 速查表 · 综合实验 | CodeCommit 与 GitLab 双平台保护 + 推送镜像 + 验证 + 清理 | 结业测验 12 题 | 综合实验 |
知识覆盖矩阵 Knowledge Coverage Matrix
| 目标技能 | M1 | M2 | M3 | M4 | M5 | M6 | M7 | M8 | 关键命令 | 实验 |
|---|---|---|---|---|---|---|---|---|---|---|
| 选择托管平台与部署形态 | ✓ | · | · | ✓ | · | · | · | ✓ | aws codecommit list-repositories | M1 |
| CodeCommit 认证(GRC / Git 凭据 / SSH) | · | ✓ | · | · | · | · | ✓ | · | git clone codecommit::REGION://PROFILE@REPO | M2 |
| CodeCommit 分支保护与审批 | · | · | ✓ | · | · | · | ✓ | · | create-approval-rule-template | M3 / 综合实验 |
| 通知、触发器与事件 | · | · | ✓ | · | · | · | · | · | put-repository-triggers | M3 |
| 外部仓库接入 AWS(CodeConnections) | · | · | · | ✓ | · | · | · | ✓ | aws codeconnections create-connection | M4 |
| GitLab 受保护分支与 MR | · | · | · | · | ✓ | ✓ | ✓ | · | glab mr create、glab api protected_branches | M5 / 综合实验 |
| 审批规则、CODEOWNERS、推送规则 | · | · | ✓ | · | · | ✓ | · | · | glab api push_rule | M6 |
| 跨平台镜像 | · | · | · | · | · | · | ✓ | ✓ | glab api remote_mirrors | M7 / 综合实验 |
| 平台排错(403、凭据冲突、拒绝推送、大文件) | · | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | · | GIT_TRACE=1、GIT_CURL_VERBOSE=1 | M7 排障树 |
| 迁移与成本评估 | ✓ | · | · | · | · | · | · | ✓ | git push --mirror | M8 |
本课排除的内容(以及在哪里学)
| 主题 | 为什么不在本课 | 去哪里学 |
|---|---|---|
| Azure Repos、GitHub(规则集、GCM、合并队列) | 属于上一课 | 第 3 课 |
CodePipeline / CodeBuild 流水线、GitLab CI/CD(.gitlab-ci.yml)细节 | 本课只讲平台上与 Git 相关的能力,流水线点到为止 | 第 5 课(Git 在 CI/CD 中)及后续独立平台课 |
| 分支策略(GitFlow / 主干开发)、单体仓库架构 | 属于架构主题 | 第 6 课 |
历史改写(git filter-repo)、清除已泄露机密 | 属于深度排错 | 第 7 课(本课只说明平台侧如何阻止) |
| GitLab Runner、安全扫描(SAST/DAST)、GitLab Duo | 超出 Git 托管本身 | docs.gitlab.com |
如何使用本课件
原理 · 图解 · 动画 · 命令 · 实验 · 生产案例 · 排错 · 测验 · 语音 · 视频。先看“原理”和“动画”,再动手做“实验”。M2 与 M5 的动画是本课核心。
测验得分 ≥ 70% 或点击“标记本模块完成”即计入进度,保存在本浏览器中(禁用本地存储时刷新后会清空)。
“语音”分页用浏览器内置的 Web Speech API(zh-CN)朗读讲解稿;没有中文语音包时讲解稿仍可阅读或交给 TTS 工具。
模块 0 · 热身与平台账号准备(课前完成)
- 已完成第 1 课(本地
~/git-lab/myapp练习仓库)与第 3 课(理解 HTTPS 凭据助手、PR 与分支保护的通用概念)。 - 主机:Ubuntu 24.04(Azure VM 或 WSL)、macOS 或 Windows(Git for Windows + Git Bash);Git ≥ 2.28(建议 2.56)。
- AWS 账号:CodeCommit 已重新向新客户开放;本课使用区域
ca-central-1(与 Docker 课一致)。需要能创建 IAM 策略/角色(或由管理员在 IAM Identity Center 中分配权限集)。 - GitLab.com 账号:Free 套餐即可完成受保护分支、MR、推送镜像;审批规则、CODEOWNERS 审批、推送规则需要 Premium/Ultimate(可使用 GitLab 提供的试用,或只阅读不操作)。
费用、权限与安全提示(先读)
| 项目 | 本课用量 | 费用 | 所需权限 |
|---|---|---|---|
| CodeCommit | 2–3 个测试仓库、1–2 个活跃身份 | CodeCommit 免费层“对新老客户长期有效”,前 5 个活跃用户免费(每月 6 个活跃用户计费 $1,见 M8);本课预计 $0 | AWSCodeCommitPowerUser(练习)+ 创建 IAM 策略(管理员) |
| IAM / IAM Identity Center | 1 个权限集或角色、1 个镜像专用 IAM 用户 | 免费 | IAM 管理员或 Identity Center 管理员 |
| CodeConnections | 1 个连接 | 连接本身不单独收费(使用它的 CodePipeline 等服务按各自价格计费) | codeconnections:* 中的创建/握手权限 |
| SNS / Lambda(触发器) | 1 个 SNS 主题 | 免费层内 | sns:CreateTopic 等 |
| GitLab.com | 1 个群组、1–2 个项目 | Free 免费;Premium 功能需试用或付费 | 群组 Owner / 项目 Maintainer |
FAKE_SECRET=...(第 1 课生成),推送到公共平台前请确认仓库可见性为 Private。步骤 1:安装 / 检查工具
# Git(Ubuntu 用 git-core PPA 获得新版本) sudo add-apt-repository -y ppa:git-core/ppa && sudo apt-get update && sudo apt-get install -y git git --version # AWS CLI v2(Linux x86_64;macOS/Windows 见官方安装页) curl -sSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o awscliv2.zip unzip -q awscliv2.zip && sudo ./aws/install --update aws --version # git-remote-codecommit(Ubuntu 24.04 的系统 Python 受 PEP 668 保护,用 pipx 安装) sudo apt-get install -y pipx && pipx ensurepath pipx install git-remote-codecommit command -v git-remote-codecommit # glab(GitLab 官方 CLI):macOS: brew install glab ;Windows: winget install glab.glab # Linux: 从 https://gitlab.com/gitlab-org/cli/-/releases 下载 .deb,或 brew install glab glab --version
git version 2.56.0 aws-cli/2.x.x Python/3.x Linux/6.x exe/x86_64.ubuntu.24 /home/azureuser/.local/bin/git-remote-codecommit glab 1.x.x (…)
版本号为示例。git-remote-codecommit 必须在 PATH 中:Git 遇到 codecommit:// 开头的 URL 时,会去 PATH 里找名为 git-remote-codecommit 的可执行文件(Git 的 remote helper 机制,见 M2)。AWS 文档要求它配合 AWS CLI v2 才能使用 IAM Identity Center(SSO)与联合身份等临时凭据。
步骤 2:配置 AWS 身份(推荐 IAM Identity Center)
aws configure sso --profile cc-dev # SSO session name: lab # SSO start URL: https://d-xxxxxxxxxx.awsapps.com/start # SSO region: ca-central-1 # 选择账号与权限集(例如含 AWSCodeCommitPowerUser 的 CodeCommitDev) # CLI default client Region: ca-central-1 aws sso login --profile cc-dev aws sts get-caller-identity --profile cc-dev
{
"UserId": "AROAXXXXXXXXXXXXXXXXX:alice@example.com",
"Account": "111122223333",
"Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_CodeCommitDev_0123abcd/alice@example.com"
}
ARN 中的 assumed-role/AWSReservedSSO_… 说明你拿到的是角色的临时凭据——这正是 HTTPS Git 凭据无法使用、而 git-remote-codecommit 可以使用的场景。没有 Identity Center 的个人账号,可以创建一个 IAM 用户并 aws configure --profile cc-dev(访问密钥),M2 会对比两者。
步骤 3:登录 GitLab.com
glab auth login --hostname gitlab.com # 选择浏览器登录或粘贴个人访问令牌(scope: api, write_repository) glab auth status
gitlab.com ✓ Logged in to gitlab.com as alice (…/glab-cli/config.yml) ✓ Git operations for gitlab.com configured to use https protocol. ✓ Token: **************************
步骤 4:复用或重建 myapp 练习仓库
本课所有实验都推送第 1 课生成的 ~/git-lab/myapp。如果已删除,请回到第 1 课模块 0 重新运行 bootstrap-git-lab.sh。检查:
cd ~/git-lab/myapp git status -sb git branch -a git log --oneline --graph --all | head -15 git tag
origin/azure)。本课新增两个远程:gitlab 与 cc(CodeCommit),一个本地仓库可以同时拥有多个远程,这也是 M7/M8 镜像与迁移的基础。热身:凭记忆回答(30 秒)
Git 的 HTTPS 推送是谁在“输入密码”?
Git 调用 凭据助手(credential helper,git credential fill 协议),助手从钥匙串/GCM/环境中返回用户名和密码,再由 libcurl 放进 HTTP Basic 认证头。CodeCommit 的 AWS CLI 凭据助手和 git-remote-codecommit 都是在这一层“动态生成密码”。
分支保护是 Git 的功能吗?
不是。Git 本身没有权限概念;分支保护是服务端在 git-receive-pack 接收 ref 更新时做的检查(GitHub 规则集、GitLab 受保护分支、CodeCommit 的 IAM 条件策略)。
模块 1 · AWS 与 GitLab 的 Git 服务定位
- 复述 CodeCommit 的关键时间点:2024-07-25 停止新客户 → 2025-11-24 恢复 GA,以及当时公布的路线图(LFS、区域扩展)和核实方法。
- 区分 GitLab.com、GitLab Dedicated、GitLab Self-Managed(CE/EE),知道 Free / Premium / Ultimate 对本课功能的影响。
- 根据身份体系、合规、生态和成本为团队选出合适的平台组合(例如“GitLab 主仓库 + CodeCommit 镜像”)。
What:两个平台分别是什么
AWS CodeCommit 是 AWS 托管的私有 Git 仓库服务:仓库属于某个 AWS 账号的某个区域,身份与授权完全基于 IAM(用户、角色、Identity Center 权限集),数据用 KMS 加密(2023-12 起支持客户托管密钥),提供 PR、审批规则、通知与触发器。它没有 Issue、Wiki、内置 CI,流水线交给 CodeBuild/CodePipeline。
GitLab 是一体化 DevSecOps 平台:Git 仓库之外还有 Issue、MR、CI/CD、包与容器镜像仓库、安全扫描等。身份是 GitLab 自己的用户与角色(Guest、Planner、Reporter、Developer、Maintainer、Owner),按群组 → 子群组 → 项目继承。
Why:CodeCommit 的“停用—复活”经过
| 日期 | 事件 | 对你意味着什么 |
|---|---|---|
| 2015-07-09 | CodeCommit 用户指南首次发布 | — |
| 2020-03-04 | 推出 git-remote-codecommit,成为联合/临时凭据访问的推荐方式 | M2 的核心 |
| 2023-12-21 | 支持客户托管 KMS 密钥 | 合规场景可自管密钥 |
| 2024-07-25 | “不再向新客户开放”,现有客户可继续使用;AWS 发布迁移到其他 Git 提供商的博客 | 大量团队开始迁出到 GitHub/GitLab |
| 2025-11-24 | AWS DevOps 博客宣布 CodeCommit 恢复全面可用(GA),立即生效;新客户可再次通过控制台、CLI、API 创建仓库;29 个区域;价格与 99.9% SLA 不变;已迁出或迁移中的客户可以回来或暂停迁移 | 选型时 CodeCommit 重新成为可选项 |
| 2025-11-25 | 用户指南文档历史记录“now available to new customers” | 官方文档口径 |
| 2026 Q1(路线图) | Git LFS 支持(“最受期待的功能”) | 截至 2026-11-15 用户指南未见上线记录,按“不支持”设计 |
| 2026 Q3 起(路线图) | 扩展到 eu-south-2(西班牙)与 ca-west-1(卡尔加里) | 截至核实日配额页的区域列表中尚无这两个区域 |
docs.aws.amazon.com/codecommit/latest/userguide/history.html);② 配额页 Regions 列表;③ AWS What's New 搜索 “CodeCommit”;④ 定价页。博客写的是“计划”,文档历史写的是“已上线”。How:GitLab 的三种部署形态
| GitLab.com(SaaS 多租户) | GitLab Dedicated | GitLab Self-Managed | |
|---|---|---|---|
| 谁运维 | GitLab | GitLab(单租户,托管在 AWS 上) | 你自己(Omnibus、Helm、Operator) |
| 版本 | 持续部署,始终最新 | GitLab 管理升级窗口 | 你决定何时升级(每月发布,第三个星期四) |
| 套餐 | Free / Premium / Ultimate | Ultimate | CE(免费开源)或 EE(Free/Premium/Ultimate 许可证) |
| 管理员设置 | 无实例管理员;群组 Owner 是最高 | 部分可配置 | 完全控制(Admin 区域,如默认分支保护、文件大小上限) |
| 适合 | 大多数团队 | 数据驻留/隔离要求高 | 内网、离线、深度定制 |
本课用到的功能与套餐(以 docs.gitlab.com 页面 Tier 标注为准)
| 功能 | Free | Premium | Ultimate |
|---|---|---|---|
| 受保护分支(按角色允许合并/推送、允许强推) | ✓ | ✓ | ✓ |
| 受保护分支指定到具体用户/群组、群组级受保护分支 | — | ✓ | ✓ |
| 推送镜像(push mirror) | ✓ | ✓ | ✓ |
| 拉取镜像(pull mirror) | — | ✓ | ✓ |
| MR 必需审批、多条审批规则 | —(Free 只有可选审批) | ✓ | ✓ |
| CODEOWNERS 与代码所有者审批 | — | ✓ | ✓ |
| 推送规则(提交消息正则、禁止机密文件、文件大小) | — | ✓ | ✓ |
| 机密推送保护(按内容检测密钥)、安全审批 | — | — | ✓ |
When:怎么选
| 场景 | 推荐 | 理由 |
|---|---|---|
| 全部工作负载在 AWS,身份统一在 IAM Identity Center,要求代码不出 AWS 账号/区域 | CodeCommit | 无额外身份系统;VPC 接口终端节点;CloudTrail 审计;KMS |
| 需要 Issue、MR、CI、制品库一体化,多云 | GitLab | 平台完整;CodeConnections 仍可把它接到 AWS |
| 主仓库在 GitLab/GitHub,但 AWS 内的系统(如隔离网络里的构建机)需要读代码 | 主仓库 + CodeCommit 只读镜像 | M7 推送镜像;或用 CodeConnections 直接接入 |
| 大量二进制资产(游戏、ML 模型) | GitLab / GitHub(LFS) | CodeCommit 截至核实日不支持 LFS,单个 blob 上限 2 GB |
架构图:两种平台的身份与仓库模型
时间轴:CodeCommit 的状态变化
交互:时间轴播放 + 平台选择器
先播放 CodeCommit 时间轴,看清每个阶段“新客户能否创建仓库”;然后勾选你的团队约束,选择器会给出推荐组合与理由。
平台选择器
核心命令:确认平台状态
| 命令 | 作用 | 说明 |
|---|---|---|
aws codecommit list-repositories | 列出当前区域的仓库 | 能返回(即使为空列表)说明账号可以使用 CodeCommit |
aws codecommit create-repository | 创建仓库 | 2024-07 至 2025-11 期间新账号会被拒绝;现在应成功 |
aws ssm get-parameters-by-path --path /aws/service/global-infrastructure/services/codecommit/regions | 查询服务可用区域 | AWS 公共参数,结果以实际返回为准 |
glab api version | 查询 GitLab 实例版本 | Self-Managed 升级前后核对 |
glab api namespaces/:id / 群组设置页 | 查看群组套餐 | 套餐挂在顶级群组上 |
aws codecommit list-repositories --profile cc-dev --region ca-central-1 aws ssm get-parameters-by-path \ --path /aws/service/global-infrastructure/services/codecommit/regions \ --query 'Parameters[].Value' --output text --profile cc-dev | tr '\t' '\n' | sort | wc -l glab api version
{
"repositories": []
}
29
{"version":"19.5.x-ee","revision":"…","kas":{…},"enterprise":true}
如何验证:区域数量应与 AWS 公告的 29 一致(不同时间可能变化)。常见错误:忘了 --region,CodeCommit 仓库是区域资源,换区域就“看不到仓库”。生产用途:把这些检查放进平台巡检脚本,及时发现服务状态或版本变化。
实验 1:确认两个平台都可用(约 3 分钟)
aws codecommit create-repository --repository-name probe-$(date +%s) \ --repository-description "M1 probe" --profile cc-dev --region ca-central-1 \ --query 'repositoryMetadata.[repositoryName,cloneUrlHttp]' --output text
probe-1763190000 https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/probe-1763190000
aws codecommit delete-repository --repository-name probe-1763190000 --profile cc-dev --region ca-central-1
glab api user | python3 -c 'import sys,json; u=json.load(sys.stdin); print(u["username"], u["id"])' glab api "groups?min_access_level=50" | python3 -c 'import sys,json; [print(g["full_path"]) for g in json.load(sys.stdin)]'
没有群组时,在 gitlab.com 新建一个群组(例如 alice-git-lab),后续 M5 在其中创建项目。套餐在群组的“设置 → 使用量配额 / 账单”中查看。
docs/adr/0004-hosting.md 里写下你的平台选择与理由(ADR,架构决策记录),提交到本地分支 docs/adr-hosting,M5 会通过 MR 合并它。
cd ~/git-lab/myapp && git switch -c docs/adr-hosting mkdir -p docs/adr && printf '# ADR 0004: Hosting\n\nPrimary: GitLab.com\nMirror: AWS CodeCommit (ca-central-1), read-only for humans\n' > docs/adr/0004-hosting.md git add docs/adr/0004-hosting.md && git commit -m "docs: ADR 0004 hosting decision"
案例 1:2024 年迁出、2026 年要不要迁回?
问题:某金融团队 2024 年把 120 个 CodeCommit 仓库迁到 GitLab Self-Managed,2025-11 AWS 宣布恢复 GA 后有人提议迁回。做法:按“身份统一、合规、功能缺口、运维成本”四项评估:GitLab 已承担 CI 与安全扫描,迁回需要重建流水线;最终决定不迁回,只为 AWS 隔离网络中的构建机建 CodeCommit 只读镜像。限制:镜像只同步 Git 引用,MR 讨论、Issue 不会同步。
案例 2:合规要求代码不得离开 AWS 账号
问题:受监管的工作负载要求源代码仓库与 CloudTrail 审计在同一账号、同一区域。做法:选用 CodeCommit + VPC 接口终端节点(git-codecommit 与 codecommit 两个终端节点服务)+ 客户托管 KMS 密钥,开发者通过 Identity Center 权限集访问。限制:没有 LFS;大文件放 S3 并在仓库中只保存清单。
案例 3:GitLab.com Free 群组突然无法添加成员
问题:开源转私有后,Free 私有命名空间受成员数量上限影响,新成员被置为等待状态。做法:评估 Premium 或将公开项目转回公开;确认上限以 GitLab.com 当前政策为准。启示:平台选型时把套餐限制写进 ADR。
常见错误与解决
An error occurred (OperationNotAllowedException) when calling the CreateRepository operation …(2024-07 至 2025-11 期间的新账号)
原因:该时期 CodeCommit 不接纳新客户。现状:2025-11-24 起已恢复,新账号应能创建。若仍报错,检查 SCP(组织服务控制策略)是否禁止了 codecommit:*,以及所选区域是否在支持列表中。异常名称以实际返回为准。
aws codecommit create-repository --repository-name t1 --region ca-central-1 --debug 2>&1 | grep -i -E "error|denied" | head
Could not connect to the endpoint URL: "https://codecommit.eu-south-2.amazonaws.com/"
原因:该区域尚未提供 CodeCommit(路线图中的区域在上线前没有终端节点)。修复:改用支持的区域,或查询 SSM 公共参数确认。
GitLab:This feature is only available on Premium and Ultimate / 设置页看不到“Push rules”或“Approval rules”
原因:功能不在当前套餐中,或项目所在的顶级群组是 Free。修复:确认项目属于哪个顶级群组以及其套餐;个人命名空间下的项目沿用个人命名空间的套餐。
1. 用一条命令列出 ca-central-1 中所有 CodeCommit 仓库的名称。
aws codecommit list-repositories --region ca-central-1 --query 'repositories[].repositoryName' --output text
2. 查询当前 GitLab 实例的版本号。
glab api version | python3 -c 'import sys,json; print(json.load(sys.stdin)["version"])'
团队 80% 工作负载在 AWS,但游戏美术资产很多,且需要 Issue 跟踪。你会推荐什么组合?
主仓库放在支持 LFS 与 Issue 的 GitLab(或 GitHub),用 CodeConnections 把仓库接入 CodePipeline;如需要 AWS 内部只读副本,可以对不含 LFS 的仓库做 CodeCommit 推送镜像。CodeCommit 在核实日仍不支持 LFS,单个 blob 上限 2 GB,不适合作为美术资产的主仓库。
经核实的教学视频
未找到经核实的 AWS CodeCommit 专题视频(特别是 2025-11 恢复 GA 之后的),请以官方文档与下方分镜为准。
官方文档
- AWS CodeCommit returns to General Availability(2025-11-24)
- CodeCommit 用户指南文档历史
- CodeCommit 配额
- GitLab:选择订阅
视频分镜脚本(约 60 秒)
画面:时间轴,2024-07 处出现红色“停止新客户”。
旁白:二零二四年七月,CodeCommit 停止接纳新客户,很多团队开始迁出。
画面:2025-11-24 处变绿,显示“29 个区域、价格不变、SLA 99.9%”。
旁白:二零二五年十一月,AWS 宣布 CodeCommit 恢复全面可用,路线图包括 Git LFS 和新区域。
画面:左边 AWS 账号 + IAM,右边 GitLab 群组 + 角色,中间一条镜像箭头。
旁白:CodeCommit 用 IAM 管权限,GitLab 用角色和群组继承。很多团队两者结合:GitLab 做主仓库,CodeCommit 做只读镜像。
模块 2 · CodeCommit:认证与 git-remote-codecommit
- 按 AWS 官方建议选择连接方式:联合身份、IAM Identity Center、临时凭据、根账号 → git-remote-codecommit;IAM 用户 → HTTPS Git 凭据(最简单)或 git-remote-codecommit;SSH 与 AWS CLI 凭据助手为备选。
- 解释 git-remote-codecommit 的工作过程:remote helper → botocore 读取凭据 → SigV4 签名生成一次性密码 → 交给 Git 的 HTTPS 传输。
- 熟练使用
aws codecommit管理仓库、分支、PR。
What:四种连接方式
| 方式 | 克隆 URL | 身份类型 | 凭据形态 | AWS 文档定位 |
|---|---|---|---|---|
| git-remote-codecommit(GRC) | codecommit::ca-central-1://cc-dev@myapp | IAM Identity Center、联合身份、角色、根账号、IAM 用户 | 每次连接用 AWS 凭据现算 SigV4 签名 | 联合/临时凭据访问的推荐方式;也可用于 IAM 用户,且无需生成 Git 凭据 |
| HTTPS + Git 凭据 | https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp | 仅 IAM 用户 | IAM 生成的静态用户名/密码(每用户最多 2 组) | “设置 CodeCommit 最简单的方式”,兼容 IDE 与第三方工具 |
| SSH | ssh://git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp | 仅 IAM 用户 | 公钥上传到 IAM 用户,SSH 用户名为 SSH 密钥 ID(APKA…) | 备选;需要手动管理密钥文件 |
| HTTPS + AWS CLI 凭据助手 | 同 HTTPS | IAM 用户、EC2 实例角色等 | aws codecommit credential-helper 现算签名 | 备选;容易与系统凭据助手冲突(M7) |
codecommit:// URL 的 IDE)→ ③ SSH(IAM 用户、偏好 SSH 的环境)→ ④ CLI 凭据助手(EC2 实例角色的老脚本)。AWS 原文是:“Git 凭据是最简单的方式”“对于联合访问、临时凭据或 Web 身份提供商,无法使用 Git 凭据,推荐 git-remote-codecommit”。Why:为什么 HTTPS Git 凭据不能用于 SSO 用户
Git 凭据(service-specific credential)是 IAM 用户的子资源,由 iam:CreateServiceSpecificCredential 生成;Identity Center 用户在 IAM 里只是一个被代入的角色会话,没有 IAM 用户对象可以挂凭据。所以 SSO 用户只能用 GRC 或 CLI 凭据助手——两者都用当前会话的临时凭据现算签名。
How:git-remote-codecommit 的内部过程
- remote helper 分派:Git 看到 URL 方案
codecommit(形如codecommit::REGION://PROFILE@REPO或codecommit://PROFILE@REPO),在 PATH 中执行git-remote-codecommit <remote> <url>(与git-remote-https同一机制)。 - 解析 URL:得到区域(缺省取配置文件的区域)、配置文件名(缺省
default)、仓库名。 - 取凭据:通过 botocore 按配置文件解析凭据链——SSO 缓存(
~/.aws/sso/cache)、role_arn代入、环境变量等,得到AccessKeyId / SecretAccessKey / SessionToken。 - SigV4 签名:对规范请求(方法
GIT、路径/v1/repos/myapp、host 头)以服务名codecommit、区域、当前 UTC 时间派生签名密钥并计算 HMAC-SHA256 签名。 - 构造凭据:用户名 = 访问密钥 ID(有会话令牌时追加
%+ 会话令牌),密码 =时间戳Z + 签名;拼成https://用户名:密码@git-codecommit.REGION.amazonaws.com/v1/repos/myapp。 - 交给 Git HTTPS 传输:调用
git remote-https,后续info/refs、git-upload-pack/git-receive-pack都走普通 Smart HTTP。服务端用同样算法验证签名、检查时间偏差,然后用 IAM 评估codecommit:GitPull/GitPush权限(M3 的条件策略在这里生效)。
上面步骤 4–5 依据 git-remote-codecommit 开源实现(github.com/aws/git-remote-codecommit)整理,属于实现细节,AWS 可能调整;对使用者而言只需记住:密码是短时有效的签名,不会落盘,凭据过期就重新 aws sso login。
When:各方式的典型场景
| 场景 | 选择 |
|---|---|
| 开发者笔记本,公司用 Identity Center | GRC + SSO 配置文件 |
| 跨账号访问(仓库在共享服务账号) | GRC + 配置文件里的 role_arn/source_profile |
| GitLab 推送镜像、第三方 SaaS 只能填用户名密码 | 专用 IAM 用户 + HTTPS Git 凭据(最小权限) |
| CodeBuild / EC2 / Lambda 内部 | 使用服务角色:CodeBuild 原生集成;EC2 上可用 GRC(默认凭据链)或 CLI 凭据助手 |
IDE 不认 codecommit:// | 先在命令行用 GRC 克隆再用 IDE 打开,或使用 Git 凭据 |
与第 3 课对比
| CodeCommit | GitHub | Azure Repos | GitLab | |
|---|---|---|---|---|
| 推荐的人类认证 | GRC + Identity Center | GCM(OAuth 浏览器流) | GCM + Entra ID | GCM / glab(OAuth)或 PAT |
| 静态凭据 | Git 凭据(IAM 用户) | 细粒度 PAT | PAT | PAT / 项目访问令牌 / 部署令牌 |
| SSH | RSA 公钥挂 IAM 用户 | 账号 SSH 密钥 / 部署密钥 | SSH 公钥 | 用户 SSH 密钥 / 部署密钥 |
时序图:git push 经 git-remote-codecommit 到 CodeCommit
决策图:我该用哪种方式?
核心动画:git-remote-codecommit 用 IAM 临时凭据做 SigV4 签名
逐步观察一次 git push cc feature/login 背后发生的事。可以切换“凭据状态”和“IAM 策略”来看失败路径。签名值为示意。
核心命令与参数
| 命令 | 作用 | 关键参数 / 说明 |
|---|---|---|
git clone codecommit::REGION://PROFILE@REPO | GRC 克隆 | 省略区域用配置文件区域;省略 PROFILE@ 用 default |
git remote add cc codecommit::ca-central-1://cc-dev@myapp | 给现有仓库加 CodeCommit 远程 | 远程名任意 |
aws iam create-service-specific-credential | 为 IAM 用户生成 Git 凭据 | --service-name codecommit.amazonaws.com;密码只显示一次 |
aws iam upload-ssh-public-key | 上传 SSH 公钥 | 返回 SSHPublicKeyId(APKA…)作为 SSH 用户名 |
aws codecommit create-repository | 创建仓库 | --repository-name --repository-description --kms-key-id --tags |
aws codecommit get-repository | 查看克隆 URL、默认分支 | cloneUrlHttp、cloneUrlSsh、defaultBranch |
aws codecommit list-branches / get-branch | 查看分支 | --repository-name --branch-name |
aws codecommit update-default-branch | 修改默认分支 | --default-branch-name main |
aws codecommit create-pull-request | 创建 PR | --targets repositoryName=,sourceReference=,destinationReference= |
aws codecommit list-pull-requests | 列出 PR | --pull-request-status OPEN |
方式 ①:GRC + IAM Identity Center(推荐)
aws sso login --profile cc-dev cd ~/git-lab/myapp aws codecommit create-repository --repository-name myapp --repository-description "Git course lab" \ --profile cc-dev --region ca-central-1 --query repositoryMetadata.arn --output text git remote add cc codecommit::ca-central-1://cc-dev@myapp git push cc main git push cc --tags
arn:aws:codecommit:ca-central-1:111122223333:myapp Enumerating objects: 58, done. … To codecommit::ca-central-1://cc-dev@myapp * [new branch] main -> main * [new tag] v1.0.0 -> v1.0.0
跨账号:配置文件中代入角色
[profile cc-shared] source_profile = cc-dev role_arn = arn:aws:iam::444455556666:role/CodeCommitCrossAccount region = ca-central-1
git clone codecommit://cc-shared@platform-libs
方式 ②:IAM 用户 + HTTPS Git 凭据
aws iam create-service-specific-credential --user-name gitlab-mirror-bot \ --service-name codecommit.amazonaws.com \ --query 'ServiceSpecificCredential.[ServiceUserName,ServicePassword,ServiceSpecificCredentialId]' --output text
gitlab-mirror-bot-at-111122223333 AbCdEf…= ACCAXXXXXXXXXXXXXXXXX
用户名形如 用户名-at-账号ID。克隆时 Git 弹出用户名/密码提示,或由系统凭据助手(GCM、osxkeychain)保存。注意:不要同时配置 AWS CLI 凭据助手,否则会冲突(M7)。
方式 ③:SSH
ssh-keygen -t rsa -b 4096 -f ~/.ssh/codecommit_rsa -N '' aws iam upload-ssh-public-key --user-name alice-iam \ --ssh-public-key-body file://~/.ssh/codecommit_rsa.pub --query SSHPublicKey.SSHPublicKeyId --output text cat >> ~/.ssh/config <<'EOF' Host git-codecommit.*.amazonaws.com User APKAEIBAERJR2EXAMPLE IdentityFile ~/.ssh/codecommit_rsa EOF ssh git-codecommit.ca-central-1.amazonaws.com
You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. Interactive shells are not supported.Connection to git-codecommit.ca-central-1.amazonaws.com closed by remote host.
IAM 的 SSH 公钥要求 ssh-rsa(OpenSSH)或 PEM 格式、至少 2048 位;ed25519 等其他类型以 IAM 文档为准,本课使用 RSA。
方式 ④:AWS CLI 凭据助手(了解即可)
# 仅对 CodeCommit 主机生效,避免影响 GitHub/GitLab git config --global credential.https://git-codecommit.ca-central-1.amazonaws.com.helper '' git config --global --add credential.https://git-codecommit.ca-central-1.amazonaws.com.helper '!aws codecommit credential-helper $@' git config --global credential.https://git-codecommit.ca-central-1.amazonaws.com.UseHttpPath true
第一行设置空值,清空从全局继承的 helper 列表(例如 manager、osxkeychain),这是避免 M7 中“一小时后 403”的关键。
实验 2:用三种方式访问同一仓库(约 7 分钟)
cd ~/git-lab/myapp git push cc --all && git push cc --tags aws codecommit list-branches --repository-name myapp --profile cc-dev --region ca-central-1
{
"branches": ["main", "feature/login", "docs/adr-hosting", "release/1.0"]
}aws sso logout git fetch cc aws sso login --profile cc-dev && git fetch cc && echo "fetch ok"
… Error when retrieving token from sso: Token has expired and refresh failed fatal: … (git-remote-codecommit 退出) fetch ok
错误文字随 botocore 版本不同;关键是本地没有可用凭据时 GRC 直接失败,不会提示输入密码。
ACCOUNT=$(aws sts get-caller-identity --profile cc-dev --query Account --output text)
aws iam create-user --user-name gitlab-mirror-bot --profile cc-admin
cat > mirror-policy.json <<EOF
{ "Version": "2012-10-17",
"Statement": [{ "Effect": "Allow",
"Action": ["codecommit:GitPull","codecommit:GitPush"],
"Resource": "arn:aws:codecommit:ca-central-1:${ACCOUNT}:myapp-mirror" }] }
EOF
aws iam put-user-policy --user-name gitlab-mirror-bot --policy-name MirrorPushOnly \
--policy-document file://mirror-policy.json --profile cc-admin
cc-admin 是具备 IAM 管理权限的配置文件(Identity Center 管理员权限集)。仓库 myapp-mirror 将在 M7 创建;ARN 必须显式包含区域与账号(GitLab 文档同样强调这一点)。
aws codecommit create-pull-request --title "docs: hosting ADR" \ --targets repositoryName=myapp,sourceReference=docs/adr-hosting,destinationReference=main \ --profile cc-dev --region ca-central-1 --query 'pullRequest.[pullRequestId,pullRequestStatus]' --output text
1 OPEN
aws iam delete-ssh-public-key --user-name … --ssh-public-key-id APKA… 删除。案例 1:淘汰 IAM 用户与长期 Git 凭据
问题:安全审计发现 200 个 IAM 用户各有 Git 凭据,离职员工的凭据仍有效。做法:全面切换到 Identity Center + GRC:发布标准 ~/.aws/config 模板和 git remote set-url origin codecommit::ca-central-1://dev@REPO 脚本;IAM 用户只保留给镜像机器人,并用 IAM Access Analyzer 的“未使用访问”发现闲置凭据。限制:部分 IDE 插件不认 codecommit://,培训“先命令行克隆再用 IDE 打开”。
案例 2:CI 构建机在私有子网访问 CodeCommit
问题:EC2 构建机无公网出口,git clone 超时。做法:创建接口 VPC 终端节点 com.amazonaws.ca-central-1.git-codecommit(Git 流量)和 com.amazonaws.ca-central-1.codecommit(API),开启私有 DNS;构建机用实例角色 + GRC。限制:终端节点按小时与流量计费;终端节点策略也可以限制可访问的仓库。
案例 3:跨账号的共享库仓库
问题:多个业务账号要读取共享服务账号中的 platform-libs。做法:在共享账号创建只读角色(信任业务账号),业务开发者的配置文件写 role_arn + source_profile,GRC 自动代入。好处:没有任何跨账号的长期密钥。
常见错误与解决
git: 'remote-codecommit' is not a git command. See 'git --help'.
原因:git-remote-codecommit 不在 PATH 中(pipx/--user 安装目录未加入 PATH)。
command -v git-remote-codecommit || ls ~/.local/bin/ pipx ensurepath && exec $SHELL -l
error: externally-managed-environment(pip install git-remote-codecommit)
原因:Ubuntu 24.04 / Debian 12 等系统 Python 遵循 PEP 668。修复:用 pipx install git-remote-codecommit 或虚拟环境;不要用 --break-system-packages。AWS 故障排除文档也补充了这一情况。
fatal: unable to access 'https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp/': The requested URL returned error: 403
原因(按概率):① 凭据助手冲突,钥匙串缓存了过期的签名密码;② IAM 无 GitPull/GitPush 或被条件策略拒绝;③ 区域写错(仓库在另一区域);④ Git 凭据被停用。
git config --show-origin --get-all credential.helper GIT_TRACE=1 GIT_CURL_VERBOSE=1 git ls-remote cc 2>&1 | grep -E "helper|HTTP/|403" | head aws codecommit get-repository --repository-name myapp --region ca-central-1
修复:见 M7 排障树;先排除凭据助手,再查 IAM。
Permission denied (publickey).(SSH)
原因:~/.ssh/config 中 User 不是 SSH 密钥 ID、公钥状态为 Inactive、或使用了 IAM 不接受的密钥类型。
ssh -v git-codecommit.ca-central-1.amazonaws.com 2>&1 | grep -E "Offering|Authenticated|denied" aws iam list-ssh-public-keys --user-name alice-iam
Unable to locate credentials / The SSO session associated with this profile has expired
修复:aws sso login --profile cc-dev;确认 URL 中的配置文件名与 ~/.aws/config 一致(区分大小写)。
1. 把现有远程 origin 从 HTTPS Git 凭据方式改成 GRC 方式(配置文件 dev,区域 ca-central-1,仓库 api)。
git remote set-url origin codecommit::ca-central-1://dev@api git remote -v
2. 列出 IAM 用户 gitlab-mirror-bot 的所有 CodeCommit Git 凭据并停用其中一个。
aws iam list-service-specific-credentials --user-name gitlab-mirror-bot --service-name codecommit.amazonaws.com aws iam update-service-specific-credential --user-name gitlab-mirror-bot --service-specific-credential-id ACCAXXXX --status Inactive
新员工通过 Identity Center 登录控制台,在 IAM 里找不到“为 CodeCommit 生成 HTTPS Git 凭据”的入口,问是不是权限不够。你怎么解释并指导?
Identity Center 用户在 IAM 中是角色会话,没有 IAM 用户对象,因此不能生成 Git 凭据——不是权限问题。指导他安装 AWS CLI v2 与 git-remote-codecommit,执行 aws configure sso 与 aws sso login,使用控制台“Clone URL → HTTPS (GRC)”复制的 codecommit::区域://配置文件@仓库 地址克隆。
教学视频
未找到经核实的 git-remote-codecommit 专题视频。下列为经 oEmbed 核实的通用 Git 视频,可复习 HTTPS/SSH 远程基础:
官方文档
- Setting up for AWS CodeCommit
- Setup steps for HTTPS connections with git-remote-codecommit
- Connecting with rotating credentials
- aws/git-remote-codecommit(源码)
视频分镜脚本(约 90 秒)
画面:终端输入
git push cc main,URL 以 codecommit:: 开头高亮。旁白:Git 看到 codecommit 方案,就去找名为 git-remote-codecommit 的辅助程序。
画面:钥匙图标从 ~/.aws/sso/cache 飞出,变成三段:访问密钥 ID、私密密钥、会话令牌。
旁白:辅助程序读取 SSO 会话的临时凭据。私密密钥只在本机参与计算。
画面:HMAC 运算动画,输出“时间戳 Z 加签名”作为密码。
旁白:它用 SigV4 算出一次性密码,交给 Git 的 HTTPS 传输。
画面:服务器验签,IAM 天平判定 Allow 或 Deny。
旁白:服务端验签之后由 IAM 决定能否推送。会话过期,重新登录即可,没有长期密码需要保管。
模块 3 · CodeCommit:分支保护、审批规则与通知
- 写出官方推荐的“限制推送和合并到分支”的 IAM 策略:
Deny推送/合并/删除/文件 API,条件StringEqualsIfExists codecommit:References+Null codecommit:References = false,并说明Null为什么必需。 - 创建审批规则模板(
DestinationReferences、NumberOfApprovalsNeeded、ApprovalPoolMembers),关联到仓库,理解它只约束 PR 合并。 - 区分通知规则(CodeStar Notifications → SNS/Chatbot)、仓库触发器(SNS/Lambda,最多 10 个)与 EventBridge 事件。
What:CodeCommit 的三层“护栏”
| 层 | 机制 | 能阻止什么 | 不能阻止什么 |
|---|---|---|---|
| ① IAM 条件策略 | Deny + codecommit:References | 直接 git push 到 main、控制台改文件、合并 PR、删分支 | 拥有该仓库完整权限且未挂此策略的身份(例如管理员) |
| ② 审批规则(模板) | PR 上自动生成规则,满足前不能合并 | 未经审批的 PR 合并(控制台与 Merge API) | 直接 git push(不走 PR);规则可被有权限的人“覆盖”(override) |
| ③ 通知 / 触发器 / EventBridge | 事件驱动 | 不阻止任何事,只做告警、审计、自动化 | — |
所以第 3 课 GitHub 规则集“一键”做到的事,在 CodeCommit 上需要 ① + ② 组合:IAM 拒绝开发者直接推送 main,PR 的合并权限由审批规则把关;再给少数发布负责人或流水线角色保留合并权限。
Why:为什么需要 Null 条件
官方示例同时使用 StringEqualsIfExists 和 "Null": {"codecommit:References": "false"},并说明:凡是 Effect 为 Deny 且动作包含 GitPush 的策略都必须加 Null,这与 Git 和 git-receive-pack 的推送方式有关。可以这样理解:一次 git push 的授权检查中,有的请求不携带 References 上下文(例如协商阶段),有的携带被更新的 ref 列表:
- 只用
StringEqualsIfExists:键不存在时条件为“真”→ Deny 生效 → 推送任何分支都被拒绝。 - 加上
Null = false(要求键存在):键不存在的请求不匹配 Deny;键存在且等于refs/heads/main时才拒绝 → 只保护 main。
How:策略的完整动作列表(官方示例)
GitPush、DeleteBranch、DeleteFile、PutFile、MergeBranchesByFastForward、MergeBranchesBySquash、MergeBranchesByThreeWay、MergePullRequestByFastForward、MergePullRequestBySquash、MergePullRequestByThreeWay。只拒绝 GitPush 是常见漏洞——开发者仍可在控制台直接编辑 main 上的文件(PutFile)或自己合并 PR。
策略要挂到组或角色上(官方建议,不要逐个用户挂);把 Resource 改成 * 可对账号内所有仓库的 main 生效。
审批规则模板
- 内容 JSON:
Version "2018-11-08"、DestinationReferences(目标分支过滤,最多 100 个,可用通配符但不能以*开头)、Statements[].Type "Approvers"、NumberOfApprovalsNeeded、ApprovalPoolMembers(审批人池,最多 50)。 - 审批人池写法:
CodeCommitApprovers:账号:名称匹配同名 IAM 用户与联合用户;代入角色会话需要写arn:aws:sts::账号:assumed-role/角色名/*(每个会话名算一个审批人)。Identity Center 用户就属于这种情况。 - 模板按区域管理(每区域最多 1000 个),需要关联到仓库;关联之后新建的 PR 才会生成规则。PR 上最多 30 条规则(其中最多 25 条来自模板)。
- 不支持跨账号审批;PR 作者的审批是否计数取决于是否在池中——生产上把作者排除在池外需要靠流程或在池中精确列出角色会话。
- 有权限(
OverrideApprovalRules)的人可以覆盖规则,覆盖会产生事件codecommit-repository-approvals-rule-override,应当告警。
通知、触发器、EventBridge
| 通知规则 | 仓库触发器 | EventBridge | |
|---|---|---|---|
| API | AWS CodeStar Notifications(codestar-notifications) | codecommit put-repository-triggers | 默认事件总线 aws.codecommit |
| 事件 | 评论、审批状态/覆盖、PR 创建/更新/状态/合并、分支与标签创建/删除/更新 | all、updateReference、createReference、deleteReference | 仓库状态变化、PR 状态变化、评论等 |
| 目标 | SNS(标准)、Chatbot(Slack/Teams);每规则 ≤ 10 个目标、每资源 ≤ 10 条规则 | SNS 标准主题(不支持 FIFO)、Lambda;每仓库 ≤ 10 个 | 任意 EventBridge 目标 |
| 用途 | 给人看的消息 | 简单自动化(推送即调用 Lambda) | 复杂路由、跨账号、启动流水线 |
与 GitHub / Azure Repos / GitLab 对比
| 能力 | CodeCommit | GitHub | Azure Repos | GitLab |
|---|---|---|---|---|
| 禁止直接推送 main | IAM Deny + References | 规则集 “Require a pull request” | 分支策略(启用即禁止直接推送) | 受保护分支 Allowed to push = No one |
| 必需审批人数 | 审批规则模板 | 规则集审批数 | 最少审阅者策略 | 审批规则(Premium) |
| 按路径的所有者 | 无(只能按审批人池) | CODEOWNERS | 自动包含审阅者(路径过滤) | CODEOWNERS(Premium) |
| 构建验证 | 需 EventBridge + CodeBuild 自行回写 PR 审批/评论 | 必需状态检查 | 构建验证策略 | “流水线必须成功” |
git push origin main 完全绕过 PR,必须配合 IAM Deny。② “Deny 只写 GitPush 就够”——控制台 PutFile 与 Merge* API 仍可修改 main。③ “模板关联后老 PR 也会生效”——只对关联之后新建的 PR 自动生成规则。流程图:一次推送 / 一次合并分别被谁检查
结构图:审批规则模板 JSON
交互:IAM 条件策略求值器
选择“请求类型”和“是否携带 References”,切换策略中是否包含 Null 条件,观察显式 Deny 是否命中。这正是官方要求加 Null 的原因。
核心命令与参数
| 命令 | 作用 | 说明 |
|---|---|---|
aws iam create-policy / attach-role-policy / attach-group-policy | 创建并挂载分支保护策略 | Identity Center 中则把策略作为权限集的内联/客户托管策略 |
aws codecommit create-approval-rule-template | 创建模板 | --approval-rule-template-content JSON 字符串 |
aws codecommit associate-approval-rule-template-with-repository | 关联到仓库 | 批量用 batch-associate-… |
aws codecommit get-pull-request-approval-states | 查看审批状态 | 需要 --revision-id |
aws codecommit update-pull-request-approval-state | 批准 / 撤销批准 | --approval-state APPROVE|REVOKE |
aws codecommit evaluate-pull-request-approval-rules | 判断规则是否满足 | 返回 approved、overridden |
aws codecommit override-pull-request-approval-rules | 覆盖规则(应受限并告警) | --override-status OVERRIDE |
aws codecommit merge-pull-request-by-squash | 合并 PR | 也有 fast-forward / three-way |
aws codecommit put-repository-triggers / test-repository-triggers | 触发器 | events:all / updateReference / createReference / deleteReference |
aws codestar-notifications create-notification-rule | 通知规则 | --event-type-ids 如 codecommit-repository-pull-request-created |
1. 分支保护策略(官方示例,改成 ca-central-1 / myapp)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"codecommit:GitPush",
"codecommit:DeleteBranch",
"codecommit:DeleteFile",
"codecommit:PutFile",
"codecommit:MergeBranchesByFastForward",
"codecommit:MergeBranchesBySquash",
"codecommit:MergeBranchesByThreeWay",
"codecommit:MergePullRequestByFastForward",
"codecommit:MergePullRequestBySquash",
"codecommit:MergePullRequestByThreeWay"
],
"Resource": "arn:aws:codecommit:ca-central-1:111122223333:myapp",
"Condition": {
"StringEqualsIfExists": { "codecommit:References": ["refs/heads/main"] },
"Null": { "codecommit:References": "false" }
}
}
]
}
aws iam create-policy --policy-name DenyChangesToMain --policy-document file://deny-main.json --profile cc-admin # 方式 A:挂到 IAM 组 / 角色 aws iam attach-role-policy --role-name CodeCommitDevelopers \ --policy-arn arn:aws:iam::111122223333:policy/DenyChangesToMain --profile cc-admin # 方式 B:Identity Center 权限集附加客户托管策略 aws sso-admin attach-customer-managed-policy-reference-to-permission-set \ --instance-arn arn:aws:sso:::instance/ssoins-xxxx --permission-set-arn arn:aws:sso:::permissionSet/ssoins-xxxx/ps-xxxx \ --customer-managed-policy-reference Name=DenyChangesToMain --profile cc-admin aws sso-admin provision-permission-set --instance-arn … --permission-set-arn … --target-type ALL_PROVISIONED_ACCOUNTS
2. 审批规则模板(要求 2 名评审角色会话批准)
aws codecommit create-approval-rule-template \
--approval-rule-template-name 2-approver-rule-for-main \
--approval-rule-template-description "Requires two reviewers if the destination branch is main" \
--approval-rule-template-content '{"Version":"2018-11-08","DestinationReferences":["refs/heads/main"],"Statements":[{"Type":"Approvers","NumberOfApprovalsNeeded":2,"ApprovalPoolMembers":["arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_CodeCommitReviewer_*/*"]}]}' \
--profile cc-admin --region ca-central-1
aws codecommit associate-approval-rule-template-with-repository \
--approval-rule-template-name 2-approver-rule-for-main --repository-name myapp --profile cc-admin --region ca-central-1
Identity Center 生成的角色名带随机后缀(AWSReservedSSO_权限集名_xxxxxxxx),所以池成员用通配符。官方示例写法是 arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*。
3. 审批与合并
PR=1 REV=$(aws codecommit get-pull-request --pull-request-id $PR --query pullRequest.revisionId --output text) aws codecommit update-pull-request-approval-state --pull-request-id $PR --revision-id $REV --approval-state APPROVE --profile reviewer1 aws codecommit evaluate-pull-request-approval-rules --pull-request-id $PR --revision-id $REV aws codecommit merge-pull-request-by-squash --pull-request-id $PR --repository-name myapp --profile release
{
"evaluation": {
"approved": false,
"overridden": false,
"approvalRulesSatisfied": [],
"approvalRulesNotSatisfied": ["2-approver-rule-for-main"]
}
}
4. 通知规则与触发器
TOPIC=$(aws sns create-topic --name myapp-repo-events --query TopicArn --output text)
aws codestar-notifications create-notification-rule --name myapp-pr-events \
--resource arn:aws:codecommit:ca-central-1:111122223333:myapp --detail-type FULL \
--event-type-ids codecommit-repository-pull-request-created codecommit-repository-pull-request-merged codecommit-repository-approvals-rule-override \
--targets TargetType=SNS,TargetAddress=$TOPIC
cat > trigger.json <<EOF
{ "repositoryName": "myapp",
"triggers": [{ "name": "main-updated", "destinationArn": "$TOPIC", "customData": "git-course",
"branches": ["main"], "events": ["updateReference"] }] }
EOF
aws codecommit test-repository-triggers --cli-input-json file://trigger.json
aws codecommit put-repository-triggers --cli-input-json file://trigger.json
通过控制台创建通知规则时会自动为新建的 SNS 主题配置访问策略;用 CLI 使用现有主题时需要自己在主题策略中允许 codestar-notifications.amazonaws.com 发布(见官方 “Configure Amazon SNS topics for notifications”)。
实验 3:保护 myapp 的 main 并要求审批(约 7 分钟)
cc-dev 所用的权限集/角色(见命令分页),等待约 1 分钟让 IAM 变更传播。cd ~/git-lab/myapp && git switch main git commit --allow-empty -m "test: direct push to main" git push cc main git switch -c feature/m3-test && git commit --allow-empty -m "test: feature push" && git push cc feature/m3-test
fatal: unable to access 'https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp/': The requested URL returned error: 403 … * [new branch] feature/m3-test -> feature/m3-test
CodeCommit 对 IAM 拒绝返回 HTTP 403,不会像 GitLab 那样给出“受保护分支”的文字说明——这是排错时最容易误判成“凭据错误”的地方(M7)。撤销本地测试提交:git switch main && git reset --hard cc/main。
aws codecommit create-pull-request --title "test: m3" \ --targets repositoryName=myapp,sourceReference=feature/m3-test,destinationReference=main \ --query 'pullRequest.approvalRules[].approvalRuleName' --output text
2-approver-rule-for-main
M2 创建的 PR #1 是在关联模板之前建的,不会自动带规则——可以对比两者。
git push cc --delete feature/m3-test(如被拒绝说明你把 Resource 写成了所有分支)。案例 1:只拒绝了 GitPush,控制台被用来“热修复”main
问题:审计发现 main 上有一次没有 PR 的提交,作者在控制台直接编辑了配置文件。根因:Deny 策略只写了 codecommit:GitPush。修复:换成官方完整动作列表(含 PutFile、DeleteFile、所有 Merge*),并用 CloudTrail 查询 PutFile 事件做回溯。
案例 2:审批人池写成 IAM 用户名,SSO 后没人能批准
问题:迁移到 Identity Center 后,所有 PR 卡在“0/2 已批准”。根因:池成员是 CodeCommitApprovers:…:Mary,只匹配 IAM 用户与联合用户,不匹配代入角色会话。修复:改为 arn:aws:sts::账号:assumed-role/AWSReservedSSO_Reviewer_*/*,更新模板后对已存在的 PR 需手动更新规则或重新创建。
案例 3:用 EventBridge 把“审批规则被覆盖”推送到安全频道
做法:通知规则订阅 codecommit-repository-approvals-rule-override,目标为 Chatbot(Slack);同时用 SCP 限制 codecommit:OverrideApprovalRules 只允许 break-glass 角色。效果:紧急情况下仍可发布,但每次覆盖都留痕并被即时审查。
常见错误与解决
The requested URL returned error: 403(推送任何分支都被拒绝)
原因:Deny 策略缺少 Null 条件,或 References 写成了 refs/heads/*。
aws iam get-policy-version --policy-arn arn:aws:iam::111122223333:policy/DenyChangesToMain \ --version-id $(aws iam get-policy --policy-arn arn:aws:iam::111122223333:policy/DenyChangesToMain --query Policy.DefaultVersionId --output text)
修复:加回 "Null": {"codecommit:References": "false"},用 aws iam create-policy-version --set-as-default 发布新版本。
main 仍然可以直接推送
排查:① 策略挂到了正确的角色/权限集吗(aws sts get-caller-identity 看实际角色)?② 权限集更改后是否 provision?③ Resource ARN 的区域/账号/仓库名是否正确(区分大小写)?④ 是否写成了 refs/heads/master?
An error occurred (PullRequestApprovalRulesNotSatisfiedException) when calling the MergePullRequestBySquash operation
原因:审批规则未满足(这正是期望行为)。evaluate-pull-request-approval-rules 查看哪些规则未满足;确认审批者的身份 ARN 能匹配池成员。
InvalidApprovalRuleTemplateContentException
原因:JSON 转义错误、Version 不是 2018-11-08、分支名以 * 开头,或内容超过 3000 字符。建议把内容写进文件再用 "$(cat rule.json)" 传入。
test-repository-triggers 返回 failedExecutions
原因:SNS 是 FIFO 主题(不支持)、主题在其他区域,或 Lambda 资源策略未允许 codecommit.amazonaws.com 调用。
1. 查看仓库 myapp 关联了哪些审批规则模板。
aws codecommit list-associated-approval-rule-templates-for-repository --repository-name myapp
2. 用 CloudTrail 查最近谁对 myapp 执行了 PutFile。
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=PutFile \ --query 'Events[].[EventTime,Username]' --output table
你需要“所有仓库的 main 和 release/* 只能通过 PR 合并,且需 2 人批准;CI 角色可以推送 tag”。如何设计?
① 一份 Deny 策略,Resource: *,References 列出 refs/heads/main 和 refs/heads/release/*(用 StringLikeIfExists 以支持通配符)+ Null 条件,挂到开发者权限集,不挂到发布/CI 角色;tag 是 refs/tags/* 不在列表内,CI 可以推送。② 一个审批规则模板,DestinationReferences 为 main 与 refs/heads/release/*,2 人,批量关联所有仓库;新仓库创建时由自动化(EventBridge + Lambda)关联模板。③ 对 override 事件告警。
教学视频
未找到经核实的 CodeCommit 分支保护 / 审批规则专题视频,请以官方文档与分镜为准。
官方文档
- Limit pushes and merges to branches
- Working with approval rule templates
- Manage triggers for a repository
- Notification concepts(事件 ID 列表)
视频分镜脚本(约 75 秒)
画面:开发者 push main,IAM 天平亮出红色 Deny,终端显示 403。
旁白:CodeCommit 用 IAM 拒绝策略保护分支,条件键是 codecommit 冒号 References。
画面:两个请求,一个没有 References 键,一个有。去掉 Null 条件时两个都变红。
旁白:一定要加 Null 条件,否则所有分支的推送都会被拒绝。
画面:PR 卡片显示“已批准 1/2”,第二位评审点批准后合并按钮变亮。
旁白:审批规则模板只管 PR 合并,不管直接推送,所以它要和 IAM 策略一起用。
模块 4 · CodeConnections:把 GitHub/GitLab 仓库连接到 AWS
- 说出 CodeConnections 支持的提供商:Bitbucket、GitHub / GitHub Enterprise Cloud、GitHub Enterprise Server、GitLab.com、GitLab 自管、Azure DevOps;以及何时需要主机(host)资源。
- 理解连接状态
PENDING → AVAILABLE(失败为ERROR):CLI/CloudFormation 创建的连接必须在控制台完成 OAuth 握手。 - 区分
codeconnections:UseConnection(服务角色使用连接)与PassConnection(把连接 ARN 交给服务),会用FullRepositoryId等条件键收窄权限;知道新旧前缀codeconnections/codestar-connections的区别。
What:连接是什么
AWS CodeConnections(开发者工具控制台 → Settings → Connections)是一个 AWS 资源,保存了 AWS 对第三方 Git 提供商的授权(例如在 GitHub 安装的 “AWS Connector for GitHub” 应用,或在 GitLab 上授权的 OAuth 应用)。CodePipeline 源操作(CodeStarSourceConnection)、CodeBuild、CloudFormation Git 同步、Amazon Q Developer 等服务引用它的 ARN 去拉代码、回写状态。它在 2024 年由 AWS CodeStar Connections 更名而来:2024-07-01 起控制台创建的连接 ARN 使用 arn:aws:codeconnections:…,旧的 codestar-connections 前缀资源与动作仍然可用。
| 提供商 | 需要 host 资源? | 备注 |
|---|---|---|
| GitHub / GitHub Enterprise Cloud | 否 | 安装 AWS Connector for GitHub 应用 |
| GitHub Enterprise Server | 是 | 可放在 VPC 内(codeconnections:VpcId 条件键) |
| GitLab.com | 否 | OAuth 授权;创建者需在 GitLab 中有 Owner 角色(群组中的仓库不要求群组 Owner) |
| GitLab 自管(Self-Managed) | 是 | 需要个人访问令牌(api 范围)注册主机 |
| Bitbucket Cloud | 否 | 安装应用 |
| Azure DevOps | 否 | 较新支持的提供商,细节以文档为准 |
Why:为什么不直接用 PAT / Webhook
- 不在 AWS 里存长期令牌:令牌由连接服务托管和轮换,流水线定义只引用 ARN。
- IAM 可控:谁能使用哪个连接、访问哪个仓库、只读还是读写,都可以用 IAM 条件键表达。
- 事件驱动:CodePipeline V2 可以基于推送、PR、标签触发,无需自己维护 Webhook。
How:生命周期
CreateConnection(控制台、CLI、CloudFormation)→ 状态PENDING。- 在控制台“更新待处理连接”:跳转到提供商登录 → 授权/安装应用 → 返回 → 状态
AVAILABLE。这一步需要握手类权限:StartOAuthHandshake、GetInstallationUrl、ListInstallationTargets、UpdateConnectionInstallation、GetIndividualAccessToken(这些是“仅 IAM 权限”,不是 API 动作)。 - 创建/更新流水线时,操作者需要
PassConnection(条件键codeconnections:PassedToService,如codepipeline.amazonaws.com)。 - 流水线运行时,服务角色需要
UseConnection(可用FullRepositoryId、BranchName、ProviderPermissionsRequired、ProviderAction等条件键收窄)。 - 不再使用时
DeleteConnection,并在提供商侧卸载应用 / 撤销 OAuth 授权。
When:连接 vs 镜像
| 需求 | CodeConnections | 推送镜像到 CodeCommit(M7) |
|---|---|---|
| CodePipeline 从 GitHub/GitLab 构建 | ✓ 首选 | 可以,但多一跳延迟 |
| AWS 内部无公网的环境读代码 | ✗(服务侧拉取,不给你的 VPC 用) | ✓ 配合 VPC 终端节点 |
| 提供商宕机时 AWS 内仍有副本 | ✗ | ✓ |
| 不想在 AWS 保存代码副本 | ✓ | ✗ |
PENDING,必须到控制台完成握手。② “给流水线用户 UseConnection 就够了”——运行时是服务角色在用连接,创建流水线的人还需要 PassConnection。③ “codestar-connections 的策略能管新连接”——资源 ARN 前缀必须与策略中的服务前缀匹配。架构图:CodePipeline 通过连接读取 GitLab 仓库
交互:连接状态机
按顺序点击操作按钮,观察状态如何变化;尝试跳过握手直接运行流水线,或在 GitLab 撤销授权。
核心命令与参数
| 命令 | 作用 | 说明 |
|---|---|---|
aws codeconnections create-connection | 创建连接 | --provider-type GitHub|GitLab|Bitbucket|GitHubEnterpriseServer|GitLabSelfManaged|AzureDevOps、--connection-name、自管时 --host-arn |
aws codeconnections get-connection | 查看状态 | ConnectionStatus:PENDING / AVAILABLE / ERROR |
aws codeconnections list-connections | 列出连接 | --provider-type-filter |
aws codeconnections create-host | 为自管 GitLab / GHES 创建主机 | --provider-endpoint https://gitlab.example.com、可选 --vpc-configuration |
aws codeconnections delete-connection | 删除连接 | 记得在提供商侧撤销授权 |
aws codestar-connections … | 旧前缀命令 | 仍可用于旧资源 |
aws codeconnections create-connection --provider-type GitLab --connection-name gitlab-myapp \ --profile cc-admin --region ca-central-1 aws codeconnections get-connection --connection-arn arn:aws:codeconnections:ca-central-1:111122223333:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f \ --query Connection.ConnectionStatus --output text
{
"ConnectionArn": "arn:aws:codeconnections:ca-central-1:111122223333:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f"
}
PENDING
服务角色的最小权限(只允许读取 acme/myapp)
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "codeconnections:UseConnection",
"Resource": "arn:aws:codeconnections:ca-central-1:111122223333:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
"Condition": {
"StringEquals": {
"codeconnections:FullRepositoryId": "acme/myapp",
"codeconnections:ProviderPermissionsRequired": "read_only"
}
}
}]
}
创建流水线的操作者需要 PassConnection
{
"Effect": "Allow",
"Action": "codeconnections:PassConnection",
"Resource": "arn:aws:codeconnections:ca-central-1:111122223333:connection/*",
"Condition": { "StringEquals": { "codeconnections:PassedToService": "codepipeline.amazonaws.com" } }
}
CodePipeline 源操作片段(只示意,流水线细节见第 5 课)
{
"name": "Source",
"actionTypeId": { "category": "Source", "owner": "AWS", "provider": "CodeStarSourceConnection", "version": "1" },
"configuration": {
"ConnectionArn": "arn:aws:codeconnections:ca-central-1:111122223333:connection/aEXAMPLE-…",
"FullRepositoryId": "acme/myapp",
"BranchName": "main",
"OutputArtifactFormat": "CODE_ZIP"
},
"outputArtifacts": [{ "name": "SourceOutput" }]
}
OutputArtifactFormat 设为 CODEBUILD_CLONE_REF 时,CodeBuild 会做完整 git clone(可运行 git describe 等,需要服务角色有 UseConnection),这与第 5 课“浅克隆导致 git describe 失败”的话题直接相关。
实验 4:创建 GitLab 连接并完成握手(约 5 分钟)
glab repo create alice-git-lab/myapp --private)。PENDING。gitlab-myapp → Update pending connection → 登录 GitLab → Authorize → 返回。
aws codeconnections get-connection --connection-arn "$CONN" --query 'Connection.[ConnectionName,ProviderType,ConnectionStatus,OwnerAccountId]' --output text
gitlab-myapp GitLab AVAILABLE 111122223333
aws codeconnections delete-connection --connection-arn "$CONN",并在 GitLab → 用户设置 → 应用(Applications)→ 已授权的应用中撤销 AWS 的授权。案例 1:授权人离职,所有流水线源阶段失败
问题:周一早上 40 条流水线全部在 Source 阶段失败。根因:连接由一名已离职工程师的 GitLab 账号授权,账号被禁用后授权失效。修复:创建专用机器人用户(对相关群组为 Owner/Maintainer),新建连接并批量更新流水线 ARN;在 Runbook 中记录“连接授权人”。
案例 2:一个连接被多个团队共用,越权读取
问题:A 团队的流水线能拉到 B 团队的私有仓库。根因:连接授权人拥有整个群组权限,服务角色的 UseConnection 没有条件。修复:每个团队的服务角色加 codeconnections:FullRepositoryId 条件;或按团队拆分连接与授权人。
案例 3:自管 GitLab 在内网
做法:为 https://gitlab.corp.example.com 创建 host 并配置 VPC(子网、安全组、TLS 证书),在 GitLab 中用 api 范围的 PAT 完成主机注册;用 codeconnections:VpcId 条件限制谁能创建 host。限制:host 设置需要 GitLab 管理员配合,证书链必须完整。
常见错误与解决
Connection arn:aws:codeconnections:… is not available / The connection is in PENDING state
原因:连接仍为 PENDING(CLI/CloudFormation 创建后未握手)。修复:控制台 Update pending connection。
… is not authorized to perform: codeconnections:UseConnection on resource …
原因:服务角色缺少 UseConnection,或条件键(FullRepositoryId 大小写、群组路径)不匹配,或策略写的是 codestar-connections:UseConnection 而连接 ARN 是 codeconnections 前缀。
aws codeconnections get-connection --connection-arn "$CONN" --query Connection.ConnectionArn aws iam simulate-principal-policy --policy-source-arn arn:aws:iam::111122223333:role/PipelineRole \ --action-names codeconnections:UseConnection --resource-arns "$CONN"
… is not authorized to perform: codeconnections:PassConnection
原因:创建/更新流水线的 IAM 身份缺少 PassConnection,或 PassedToService 条件不匹配。
连接状态 ERROR
原因:提供商侧撤销了授权、应用被卸载、授权人失去访问权限或被删除。修复:在控制台重新更新连接(重新授权),或新建连接替换。
流水线找不到仓库 / Repository not found
原因:FullRepositoryId 写错(GitLab 要写完整路径,包括子群组 acme/platform/myapp),或授权人对该项目没有访问权限。
1. 列出账号中所有 GitLab 类型的连接及其状态。
aws codeconnections list-connections --provider-type-filter GitLab \ --query 'Connections[].[ConnectionName,ConnectionStatus]' --output table
2. 为自管 GitLab 创建 host。
aws codeconnections create-host --name corp-gitlab --provider-type GitLabSelfManaged \ --provider-endpoint https://gitlab.corp.example.com
安全团队要求:“AWS 中不得出现任何 GitLab 个人访问令牌,且流水线只能读取指定仓库。”你如何满足?
使用 CodeConnections(GitLab.com 走 OAuth 授权,不需要在 AWS 中保存 PAT;令牌由连接服务托管),授权人为专用机器人账号;服务角色的 UseConnection 加 FullRepositoryId 与 ProviderPermissionsRequired=read_only 条件;只给平台团队 CreateConnection 与握手权限;定期检查连接状态与授权人。自管 GitLab 的 host 注册需要 PAT,但该 PAT 只在注册时使用,可注册后撤销/轮换(以官方文档对 host 的要求为准)。
教学视频
未找到经核实的 CodeConnections 专题视频(更名后尚少专门讲解)。
官方文档
视频分镜脚本(约 60 秒)
画面:CLI 创建连接,状态灯黄色 PENDING。
旁白:命令行创建的连接一开始是待处理状态,需要在控制台完成授权握手。
画面:浏览器跳到 GitLab 授权页,点 Authorize,状态灯变绿 AVAILABLE。
旁白:授权完成后,连接可用。
画面:流水线角色拿着钥匙(UseConnection)打开门,门上写 acme/myapp 只读。
旁白:运行时是服务角色在使用连接,用条件键把它限制在指定仓库。
模块 5 · GitLab:项目、受保护分支与 Merge Request
- 理解 GitLab 的群组/子群组/项目层级与六种角色(Guest、Planner、Reporter、Developer、Maintainer、Owner),知道 Developer 与 Maintainer 在分支上的默认差异。
- 配置受保护分支:Allowed to merge、Allowed to push and merge(No one / Developers + Maintainers / Maintainers / 部署密钥)、Allowed to force push、通配符规则与“最宽松规则生效”。
- 讲清 push 被拒绝的完整链路:Gitaly 接收 → pre-receive → Rails
/internal/allowed访问检查 →remote: GitLab: You are not allowed to push code to protected branches on this project. - 用
glab完成建项目、开 MR、审批、合并,并会用glab api配置受保护分支。
What:项目、群组与角色
GitLab 的项目(project)包含一个 Git 仓库和 MR、Issue、CI 等功能,属于一个命名空间:个人命名空间或群组(group,可嵌套子群组)。成员在群组上获得的角色会继承到其下的子群组和项目;也可以在项目上单独邀请成员。
| 角色 | 与仓库相关的典型权限 |
|---|---|
| Guest | 私有项目中不能读代码(可看 Issue 等) |
| Planner | 规划类功能(Issue、Epic),不面向代码(GitLab 17.7 新增) |
| Reporter | 读代码、克隆、拉取;不能推送 |
| Developer | 推送到非受保护分支、创建 MR;受保护分支取决于规则 |
| Maintainer | 管理受保护分支、推送规则、镜像、成员(不含 Owner) |
| Owner | 群组/项目的最高权限(删除、转移、可见性、计费) |
Why:受保护分支解决什么
没有保护时,任何 Developer 都能 git push --force 到 main 或删除它。受保护分支在服务端拦截:谁能推送、谁能通过 MR 合并、能否强推、能否删除(受保护分支不能通过 Git 删除,只能先取消保护)。默认分支创建时会自动受保护,默认保护级别可在 Admin 区域(自管)或群组设置中调整。
How:规则的字段与语义
| 选项 | 可选值 | 含义 |
|---|---|---|
| Branch | 名称或通配符,如 main、release/*、*-stable | 区分大小写;多条规则匹配同一分支时最宽松的生效 |
| Allowed to merge | No one / Developers + Maintainers / Maintainers(Premium 可加具体用户/群组) | 谁能在 MR 中点“合并”使改动进入该分支 |
| Allowed to push and merge | No one / Developers + Maintainers / Maintainers / 部署密钥(Premium 可加用户/群组) | 谁能直接 git push(同时也能合并) |
| Allowed to force push | 开 / 关 | 开启后,所有有推送权限者可强推 |
| Require approval from code owners | 开 / 关(Premium) | MR 需代码所有者批准,同时拒绝直接推送匹配的分支(M6) |
API 中的访问级别数值:0 = No access(No one),30 = Developer,40 = Maintainer,60 = Admin(自管实例管理员)。
push 被拒绝的内部链路
- 客户端
git push经 HTTPS(Workhorse)或 SSH(gitlab-shell)到达 Gitaly,后者运行git receive-pack接收对象。 - 更新 ref 之前执行 pre-receive 钩子,Gitaly 携带“旧 SHA、新 SHA、ref 名、用户、协议”调用 GitLab Rails 的内部 API
POST /api/v4/internal/allowed。 - Rails 依次做访问检查:项目权限 → 受保护分支(推送/强推/删除)→ 推送规则(Premium)→ 文件大小/LFS 完整性 → 机密推送保护(Ultimate)等。
- 任一检查失败 → 返回
status: false与消息,Gitaly 让 pre-receive 退出非零,ref 不更新(对象可能已上传但会被丢弃)。 - 客户端打印
remote: GitLab: …与! [remote rejected] main -> main (pre-receive hook declined)。
Merge Request 的关键设置
| 设置(项目 → 设置 → 合并请求) | 选项 |
|---|---|
| 合并方式 | Merge commit / Merge commit with semi-linear history / Fast-forward merge |
| 压缩提交(squash) | Do not allow / Allow / Encourage / Require |
| 合并检查 | 流水线必须成功、所有讨论必须解决、(Premium)需审批 |
| 默认删除源分支 | 开 / 关 |
When:常见配置模板
| 分支 | Allowed to merge | Allowed to push and merge | Force push |
|---|---|---|---|
main | Developers + Maintainers(配合审批)或 Maintainers | No one | 关 |
release/* | Maintainers | No one | 关 |
env/production(被 CI 推) | No one | 部署密钥 / 机器人 | 关 |
与第 3 课对比
| GitLab 受保护分支 | GitHub 规则集 | Azure Repos 分支策略 | CodeCommit | |
|---|---|---|---|---|
| 拒绝消息 | “You are not allowed to push code to protected branches on this project.” | “GH013: Repository rule violations found” | “TF402455: Pushes to this branch are not permitted” | HTTP 403(无说明) |
| 绕过 | Allowed to push and merge 中的角色/用户 | Bypass list | “绕过策略”权限 | 未挂 Deny 的身份 |
* 规则设为 Maintainers 会更严格”——多条规则时最宽松的生效,可能反而放宽了 main。③ “受保护分支能阻止 MR 合并未审批代码”——Free 中不能,需要 Premium 的必需审批。时序图:push 到受保护分支被拒绝
结构图:规则匹配与“最宽松生效”
核心动画:GitLab 受保护分支拒绝 push 的流程
选择推送者角色、目标分支与推送方式,逐步观察请求经过 Workhorse → Gitaly → pre-receive → Rails 访问检查的过程。
核心命令与参数
| 命令 | 作用 | 关键参数 |
|---|---|---|
glab repo create | 创建项目 | GROUP/NAME --private --defaultBranch main --description |
glab mr create | 创建 MR | --source-branch --target-branch main --title --description --fill --squash-before-merge --remove-source-branch |
glab mr list / view / diff | 查看 MR | --state opened |
glab mr approve / revoke | 批准 / 撤销 | MR 编号 |
glab mr merge | 合并 | --squash --remove-source-branch --auto-merge(流水线成功后自动合并) |
glab api | 调用 REST API | -X POST projects/:id/protected_branches -f name=main -f push_access_level=0 -f merge_access_level=40 |
git push -o merge_request.create | 推送时创建 MR(push options) | -o merge_request.target=main -o merge_request.remove_source_branch |
cd ~/git-lab/myapp glab repo create alice-git-lab/myapp --private --defaultBranch main --description "Git course lab" git remote add gitlab https://gitlab.com/alice-git-lab/myapp.git git push gitlab main && git push gitlab --tags # 默认分支已自动受保护,先看当前规则(项目路径需要 URL 编码) glab api "projects/alice-git-lab%2Fmyapp/protected_branches"
[{"id":101,"name":"main",
"push_access_levels":[{"access_level":40,"access_level_description":"Maintainers"}],
"merge_access_levels":[{"access_level":40,"access_level_description":"Maintainers"}],
"allow_force_push":false,"code_owner_approval_required":false}]
改为“没人能直接推送,Developer + Maintainer 可合并”
P="alice-git-lab%2Fmyapp" glab api -X DELETE "projects/$P/protected_branches/main" glab api -X POST "projects/$P/protected_branches" \ -f name=main -f push_access_level=0 -f merge_access_level=30 -F allow_force_push=false glab api -X POST "projects/$P/protected_branches" \ -f name='release/*' -f push_access_level=0 -f merge_access_level=40
也可以用 PATCH projects/:id/protected_branches/main 修改 allow_force_push、code_owner_approval_required;修改访问级别列表时 PATCH 需要传 allowed_to_push[] 等数组参数,删除重建更直观。UI 路径:项目 → 设置 → 仓库 → 受保护分支。
MR 全流程
git switch docs/adr-hosting git push -u gitlab docs/adr-hosting glab mr create --target-branch main --fill --remove-source-branch --squash-before-merge --yes glab mr list glab mr merge 1 --squash --remove-source-branch --yes git switch main && git pull gitlab main
Creating merge request for docs/adr-hosting into main in alice-git-lab/myapp !1 docs: ADR 0004 hosting decision (docs/adr-hosting) https://gitlab.com/alice-git-lab/myapp/-/merge_requests/1 ✓ Squashed and merged! ✓ Deleted source branch docs/adr-hosting
实验 5:保护 main 并只能通过 MR 合并(约 8 分钟)
git switch main git commit --allow-empty -m "test: direct push" git push gitlab main
remote: GitLab: You are not allowed to push code to protected branches on this project. To https://gitlab.com/alice-git-lab/myapp.git ! [remote rejected] main -> main (pre-receive hook declined) error: failed to push some refs to 'https://gitlab.com/alice-git-lab/myapp.git'
git push gitlab +main # 强推 git push gitlab --delete main # 删除
两者都会被拒绝;删除受保护分支的消息与强推不同(受保护分支只能在网页上先取消保护再删除)。记录你看到的原文。
git switch -c test/m5 && git push -u gitlab test/m5 \ -o merge_request.create -o merge_request.target=main -o merge_request.remove_source_branch glab mr merge --squash --yes git switch main && git reset --hard gitlab/main && git pull gitlab main
案例 1:通配符规则意外放宽 main
问题:为了让开发者推 feature/*,有人加了一条 * 规则(push=Developers+Maintainers),结果 main 也能直接推送。根因:多条规则匹配时最宽松生效。修复:删除 * 规则(未保护的分支本来就允许 Developer 推送)。
案例 2:CI 需要推版本标签与 changelog
问题:发布作业要往 main 推一个“bump version”提交。做法:首选:改成通过 MR 自动合并(glab mr create + 自动合并);必须直接推送时,使用部署密钥或项目访问令牌机器人,并在受保护分支的 Allowed to push and merge 中只加入该部署密钥。限制:按用户/群组授权需要 Premium;部署密钥选项在 Free 可用(以界面为准)。
案例 3:大型组织统一保护规则
做法:Premium 的群组级受保护分支(仅顶级群组、需 Owner)为所有项目统一保护 main;Self-Managed 在 Admin 区域设置默认分支保护级别。效果:新项目创建即合规。
常见错误与解决
remote: GitLab: You are not allowed to push code to protected branches on this project.
原因:目标分支受保护,你的角色不在 Allowed to push and merge 中。修复:推到新分支并创建 MR;确实需要直接推送时请 Maintainer 调整规则(不建议对 main 调整)。
glab api "projects/GROUP%2FPROJECT/protected_branches/main" | python3 -m json.tool
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
原因:规则未开启 Allowed to force push。修复:用 git revert 代替改写历史;本地 rebase 过时请先 git pull --rebase。
remote: You are not allowed to push code to this project. / 403
原因:不是受保护分支问题,而是项目权限不足(Reporter/Guest),或令牌缺少 write_repository 范围、已过期。
glab auth status glab api "projects/GROUP%2FPROJECT/members/all/$(glab api user | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')"
MR 页面“合并”按钮灰色:You don't have permission to merge this merge request
原因:你的角色不在目标分支的 Allowed to merge 中,或合并检查未通过(流水线、讨论未解决、审批不足)。查看 MR 页面合并区块的具体提示。
1. 用 API 保护 release/*:没人能直接推送,只有 Maintainer 能合并。
glab api -X POST "projects/GROUP%2FPROJECT/protected_branches" -f name='release/*' -f push_access_level=0 -f merge_access_level=40
2. 推送分支的同时创建以 main 为目标、合并后删除源分支的 MR。
git push -u gitlab feature/x -o merge_request.create -o merge_request.target=main -o merge_request.remove_source_branch
Free 套餐下,团队希望“main 必须经过 MR,且至少有人看过”。能做到什么程度?
受保护分支 push=No one 可强制所有改动走 MR;merge=Maintainers 可让“合并者”成为把关人(作者是 Developer 时,必须由 Maintainer 合并,相当于一人审查);开启“流水线必须成功”和“所有讨论必须解决”。但 Free 不能强制“必需审批人数”与 CODEOWNERS 审批,如需要请升级 Premium 或在流水线中加自定义检查。
经核实的教学视频
官方文档
视频分镜脚本(约 75 秒)
画面:开发者推 main,请求穿过 Workhorse、Gitaly。
旁白:推送到达 Gitaly,在更新分支之前先执行 pre-receive 钩子。
画面:钩子向 Rails 发问“allowed?”,Rails 查受保护分支表,返回红色 false。
旁白:Rails 做访问检查:受保护分支、推送规则、文件大小,任何一项失败都会拒绝。
画面:终端显示 remote: GitLab 拒绝消息;随后开发者改走 MR,绿色合并。
旁白:分支没有任何改变。正确的做法是推到新分支、创建合并请求。
模块 6 · GitLab:审批规则、CODEOWNERS 与推送规则
- 配置项目审批规则(所需人数、审批人、目标分支)与审批设置(禁止作者批准、禁止提交者批准、禁止在 MR 中修改规则、新提交时移除审批)。
- 编写 CODEOWNERS:查找位置(根目录 →
docs/→.gitlab/,第一个找到的生效)、最后匹配优先、[Section]与^[Optional]语法;配合受保护分支的“需要代码所有者批准”。 - 配置推送规则:提交消息正则、分支名正则、禁止机密文件、文件名正则、最大文件大小、拒绝未签名提交、DCO;知道“禁止机密文件”只看文件名,内容检测是 Ultimate 的机密推送保护。
What:三种机制的作用点
| 机制 | 作用时机 | 套餐 | 失败表现 |
|---|---|---|---|
| 审批规则 | MR 合并时 | Premium / Ultimate(Free 只有可选审批,任何人都可以“批准”但不强制) | MR 显示“需要 N 个审批”,合并按钮不可用 |
| CODEOWNERS | MR 合并时(自动生成“代码所有者”审批规则);开启“需要代码所有者批准”后也拒绝直接推送 | Premium / Ultimate | MR 中列出各段所有者的待审批 |
| 推送规则 | git push 时(pre-receive,M5 的同一链路) | Premium / Ultimate | remote: GitLab: … + pre-receive hook declined |
| 机密推送保护 | git push 时按内容扫描密钥模式 | Ultimate | 推送被阻止并给出命中的提交与文件 |
审批规则与设置
- 规则:名称、所需审批数(0 = 可选规则)、审批人(用户/群组)、适用的目标分支(全部或某个受保护分支)。可以有多条,全部满足才可合并。
- 设置(项目 → 设置 → 合并请求 → 审批):禁止 MR 创建者批准(默认作者不计入)、禁止提交者批准(默认允许,建议开启)、禁止在 MR 中编辑审批规则、新提交推送时移除全部审批或只移除受影响的代码所有者审批、批准时要求重新认证等。
CODEOWNERS
- 位置(按顺序查找,第一个找到的生效,其余忽略):
./CODEOWNERS→./docs/CODEOWNERS→./.gitlab/CODEOWNERS。按 MR 的目标分支上的文件版本评估。 - 模式类似 gitignore;最后一个匹配的条目生效,所以
*默认所有者写在最前面。 - 段(Section):
[Docs]开始一个段,每个段独立计算(每段都需要一位所有者批准);[Docs][2]要求该段 2 人批准;^[Docs]为可选段(不强制);段名后可写默认所有者[Docs] @docs-team。 - 所有者可以是
@用户、@群组、@群组/子群组、邮箱、或角色(如@@maintainer,以文档为准)。所有者必须对项目有访问权限才有效。 - 要强制代码所有者批准:保护目标分支并开启 Require approval from code owners。注意:Allowed to push and merge 中的用户仍可直接推送绕过 MR,因此 main 的 push 应设为 No one。
推送规则(项目 → 设置 → 仓库 → 推送规则)
| 类别 | 规则 | 示例 |
|---|---|---|
| 用户验证 | 拒绝未验证用户、拒绝不一致的用户名、检查作者是否为 GitLab 用户、作者邮箱正则 | @example\.com$ |
| 提交消息 | 必须匹配 / 不得匹配正则 | ^(feat|fix|docs|chore|refactor|test)(\(.+\))?: .+ |
| 分支名 | 分支名正则 | ^(main|release/.+|(feature|fix|docs)/[a-z0-9._-]+)$ |
| 防误操作 | 拒绝未签名提交、禁止用 git push 删除标签 | — |
| 文件 | 禁止推送机密文件、禁止的文件名正则、最大文件大小(MB) | (\.tfstate|\.env)$、10 |
| DCO | 拒绝缺少 Signed-off-by: 的提交 | git commit -s |
“禁止推送机密文件”依据 GitLab 维护的 files_denylist.yml,按文件名/路径匹配:AWS 凭据文件(如 .aws/credentials)、各类 SSH 私钥(id_rsa、id_ed25519 及 _sk 变体)、以 .pem、.key、.history、_history 结尾的文件等。它不检查文件内容——第 1 课练习仓库中 config.env 里的 FAKE_SECRET=... 不会被它拦住;按内容检测需要 Ultimate 的机密推送保护,或在客户端用 pre-commit/gitleaks(第 5 课)。
推送规则只检查新推送的提交,不追溯已有历史;新建项目继承群组/实例的推送规则,已有项目需要手动更新。
When:推荐组合
| 目标 | 配置 |
|---|---|
| main 至少 2 人审查,安全目录须安全团队批准 | 审批规则 “Default” 2 人 + CODEOWNERS [Security] /auth/ @acme/security + 受保护分支开启代码所有者批准 |
| Conventional Commits | 推送规则提交消息正则(服务端强制),客户端 commit-msg 钩子提前提示 |
| 防止私钥与状态文件进仓库 | 禁止机密文件 + 文件名正则 \.tfstate$ + 最大文件大小 |
决策图:MR 能否合并
对照图:推送规则与机密推送保护
交互:MR 审批计算器
勾选 MR 改动的路径和已批准的人,计算器按“最后匹配生效”解析 CODEOWNERS,并结合项目审批规则与设置判断能否合并。
* @alice @bob [Security] /app/auth/ @acme/security [Infra][2] /compose.yaml @carol @dave @erin ^[Docs] /docs/ @frank改动的文件
核心命令(GitLab REST API,经 glab api 调用)
| 端点 | 作用 | 常用字段 |
|---|---|---|
POST projects/:id/approval_rules | 新建审批规则 | name、approvals_required、user_ids[]、group_ids[]、protected_branch_ids[] |
POST projects/:id/approvals | 审批设置 | merge_requests_author_approval、merge_requests_disable_committers_approval、disable_overriding_approvers_per_merge_request、reset_approvals_on_push |
GET projects/:id/merge_requests/:iid/approval_state | 查看 MR 各规则状态 | 含代码所有者规则 |
PATCH projects/:id/protected_branches/main | 开启代码所有者批准 | code_owner_approval_required=true |
POST/PUT projects/:id/push_rule | 新建 / 修改推送规则 | commit_message_regex、branch_name_regex、prevent_secrets、file_name_regex、max_file_size、deny_delete_tag、member_check、reject_unsigned_commits |
P="alice-git-lab%2Fmyapp" # 审批设置:作者与提交者都不能批准;新推送重置审批;禁止在 MR 中改规则 glab api -X POST "projects/$P/approvals" -F merge_requests_author_approval=false \ -F merge_requests_disable_committers_approval=true -F reset_approvals_on_push=true \ -F disable_overriding_approvers_per_merge_request=true # 审批规则:2 人 glab api -X POST "projects/$P/approval_rules" -f name=Default -F approvals_required=2 # 受保护分支开启代码所有者批准 glab api -X PATCH "projects/$P/protected_branches/main" -F code_owner_approval_required=true
CODEOWNERS 文件
# 默认所有者(最先写,后面更具体的条目覆盖它) * @alice-git-lab/maintainers [Security] /app/auth/ @alice-git-lab/security [Infra][2] /compose.yaml @carol @dave @erin /Dockerfile @carol @dave @erin ^[Docs] @frank /docs/
推送规则
glab api -X POST "projects/$P/push_rule" \
-f commit_message_regex='^(feat|fix|docs|chore|refactor|test|ci)(\(.+\))?: .{1,72}' \
-f branch_name_regex='^(main|release/.+|(feature|fix|docs|test)/[a-z0-9._-]+)$' \
-F prevent_secrets=true -f file_name_regex='(\.tfstate|\.tfstate\.backup)$' \
-F max_file_size=10 -F deny_delete_tag=true
glab api "projects/$P/push_rule"
{"id":55,"project_id":…,"commit_message_regex":"^(feat|fix|docs|chore|refactor|test|ci)(\\(.+\\))?: .{1,72}",
"branch_name_regex":"^(main|release/.+|(feature|fix|docs|test)/[a-z0-9._-]+)$",
"deny_delete_tag":true,"prevent_secrets":true,"max_file_size":10,…}
Free 套餐调用这些端点会返回 403/404。正则使用 RE2 语法(不支持回溯与前瞻),以文档为准。
实验 6:CODEOWNERS + 推送规则(约 7 分钟,需要 Premium 试用;Free 可只做第 1、5 步)
cd ~/git-lab/myapp && git switch main && git pull gitlab main git switch -c chore/codeowners mkdir -p .gitlab && printf '* @%s\n\n[Docs]\n/docs/ @%s\n' "$(glab api user | python3 -c 'import sys,json;print(json.load(sys.stdin)["username"])')" "$(glab api user | python3 -c 'import sys,json;print(json.load(sys.stdin)["username"])')" > .gitlab/CODEOWNERS git add .gitlab/CODEOWNERS && git commit -m "chore: add CODEOWNERS" git push -u gitlab chore/codeowners -o merge_request.create -o merge_request.target=main
git switch -c test/push-rules git commit --allow-empty -m "update stuff" git push gitlab test/push-rules
remote: GitLab: Commit message does not follow the pattern '^(feat|fix|docs|chore|refactor|test|ci)(\(.+\))?: .{1,72}'
! [remote rejected] test/push-rules -> test/push-rules (pre-receive hook declined)
git commit --amend -m "test: verify push rules" && git push gitlab test/push-rules
ssh-keygen -t ed25519 -f ./id_ed25519 -N '' -q git add id_ed25519 && git commit -m "test: try to push a key" git push gitlab test/push-rules # 被拒绝:文件名命中机密文件列表 git reset --hard HEAD~1 && rm -f id_ed25519 id_ed25519.pub git push gitlab HEAD:Bad_Branch # 被拒绝:分支名不匹配
再试一次修改 config.env 中的 FAKE_SECRET=... 并推送——推送规则不会拦截(只看文件名),这正是需要内容扫描的原因。
git push gitlab --delete test/push-rules;不打算继续 Premium 试用时,记下哪些设置在试用结束后会失效(审批规则、推送规则、代码所有者批准)。案例 1:CODEOWNERS 放错位置,安全审批从未生效
问题:仓库根目录和 .gitlab/ 下各有一个 CODEOWNERS,团队一直在改 .gitlab/ 下的版本。根因:GitLab 只用第一个找到的(根目录优先)。修复:只保留一个;在 CI 中用 GitLab 的 CODEOWNERS 校验(MR 页面会提示语法错误)并检查重复文件。
案例 2:提交消息正则阻断了合并提交
问题:开启 Conventional Commits 正则后,部分开发者本地 git merge 生成的 “Merge branch …” 提交被拒绝。做法:在 MR 中使用 squash 并设置 squash 提交消息模板;正则中允许 ^Merge branch,或要求本地用 rebase。启示:上线推送规则前先用 git log --format=%s 对历史跑一遍正则估算影响。
案例 3:私钥泄露的“最后一道闸”
问题:开发者误把 deploy.pem 加入提交。结果:“禁止机密文件”规则拦截了推送(.pem 后缀)。后续:团队同时启用客户端 gitleaks 钩子与 Ultimate 的机密推送保护,覆盖文件内容中的密钥。
常见错误与解决
remote: GitLab: Commit message does not follow the pattern '…'
原因:本次推送中任何一个新提交的消息不匹配。修复:找出不合规提交并改写(仅限尚未合并的私有分支)。
git log --format='%h %s' gitlab/main..HEAD | grep -Ev '^[0-9a-f]+ (feat|fix|docs|chore|refactor|test|ci)(\(.+\))?: ' git rebase -i gitlab/main # 把不合规的提交标记为 reword,逐个改写消息
remote: GitLab: File name … was prohibited by the pattern … / 文件被识别为机密文件
原因:文件名命中机密文件列表或文件名正则。注意:即使后面的提交删除了文件,只要推送范围内任何提交包含它,仍会被拒绝——需要改写这些提交(git reset 或 git rebase),而不是再加一个删除提交。
remote: GitLab: File "…" is larger than the allowed size of 10 MiB
原因:推送规则最大文件大小(或实例级上限)。修复:改用 Git LFS(GitLab 支持),或把大文件移出仓库;同样需要改写包含大文件的提交。M7 有详细流程。
MR 显示 “Code owners approval is required” 但列出的所有者是空的 / 无效
原因:CODEOWNERS 中的用户名/群组不存在或对项目没有访问权限;群组需要被邀请到项目或是项目上级群组。MR 页面和文件浏览器会标出无效的所有者。
1. 查看 MR !7 各审批规则(含代码所有者)的状态。
glab api "projects/GROUP%2FPROJECT/merge_requests/7/approval_state" | python3 -m json.tool
2. 用推送规则禁止推送 Terraform 状态文件并限制单文件 5 MB。
glab api -X PUT "projects/GROUP%2FPROJECT/push_rule" -f file_name_regex='\.tfstate(\.backup)?$' -F max_file_size=5
审计要求:“任何改动 /app/auth/ 的代码必须有安全团队一人批准,且作者不能自己批;文档改动不阻塞。”写出配置。
Premium:.gitlab/CODEOWNERS 中 [Security] 段 /app/auth/ @acme/security,文档用可选段 ^[Docs];main 受保护(push=No one)并开启 Require approval from code owners;审批设置中禁止创建者批准、禁止提交者批准、禁止在 MR 中编辑规则;新提交推送时移除代码所有者审批(或全部审批)。确保 @acme/security 群组对项目有访问权限。
教学视频
未找到经核实的 GitLab CODEOWNERS / 推送规则专题视频。可参考 M5 视频分页中的 GitLab 入门课程。
官方文档
- Merge request approval rules
- Merge request approval settings
- Code Owners · CODEOWNERS syntax
- Push rules
- Secret push protection
视频分镜脚本(约 75 秒)
画面:MR 改了 auth 和 docs 两个目录,CODEOWNERS 文件从下往上扫描,最后匹配的行高亮。
旁白:代码所有者按最后一个匹配的条目确定,每个段都需要一位所有者批准,可选段不阻塞。
画面:推送 id_rsa,被红色闸门挡住;推送 config.env 里的假密钥,闸门没反应。
旁白:禁止机密文件只看文件名,内容扫描要靠机密推送保护。
画面:提交消息“update stuff”被拒,改成“fix: …”后通过。
旁白:推送规则在服务端强制统一的提交规范,配合客户端钩子提前提醒。
模块 7 · 跨平台镜像与排错实战
- 搭建 GitLab → CodeCommit 推送镜像(专用 IAM 用户 + HTTPS Git 凭据 + 最小权限),理解更新时机(推送触发,5 分钟内;只镜像受保护分支时 1 分钟内)、Keep divergent refs 与“只镜像受保护分支”。
- 知道拉取镜像(Premium)与推送镜像的区别,以及“一个仓库只能有一个写入源”的原则。
- 按排障树诊断:CodeCommit 403、凭据助手冲突、GitLab 受保护分支拒绝、大文件/推送过大、LFS 支持差异、镜像分叉。
What:GitLab 的两种镜像
| 推送镜像(Push) | 拉取镜像(Pull) | |
|---|---|---|
| 方向 | GitLab → 外部(CodeCommit、GitHub、另一个 GitLab) | 外部 → GitLab |
| 套餐 | Free / Premium / Ultimate | Premium / Ultimate |
| 触发 | 上游有推送时;管理员可强制“立即更新”。推送后 5 分钟内同步,开启“只镜像受保护分支”时 1 分钟内 | 定时轮询(可由 API 触发) |
| 认证 | 用户名+密码(令牌)或 SSH 公钥 | 同左 |
| 分叉处理 | 默认覆盖镜像上分叉的 ref;开启 Keep divergent refs 则保留并跳过,更新标记为失败 | 本地分叉时停止更新该分支 |
推送镜像本质上是 GitLab 后台执行类似 git push --force(按所选 ref 范围)到目标 URL。源中“合并到默认分支后删除”的分支,会在下一次推送时从镜像删除;仍有未合并改动的分支会保留。GitLab 静默模式(Silent Mode)会停用镜像推拉。
Why:为什么镜像到 CodeCommit
- AWS 隔离网络(无公网)中的构建或部署需要读代码 → CodeCommit + VPC 接口终端节点。
- 灾备:GitLab.com 故障时 AWS 内仍有只读副本。
- 合规:需要在 AWS 账号内用 CloudTrail 审计“谁读了代码”。
How:GitLab → CodeCommit 推送镜像的要点(依 GitLab 官方步骤)
- 在 AWS 创建专用 IAM 用户,内联策略只允许对目标仓库的 Git 拉取/推送动作,ARN 必须显式包含区域与账号。
- 为该用户生成 HTTPS Git 凭据(不是 IAM 用户 ID,也不是访问密钥)。
- GitLab 项目 → 设置 → 仓库 → 镜像仓库:URL 填
https://git-codecommit.<区域>.amazonaws.com/v1/repos/<仓库>,方向 Push,认证方式“用户名和密码”,填入 Git 凭据。 - GitLab 建议对 CodeCommit 保持勾选“只镜像受保护分支”(推送更频繁、更快)。
- 点“立即更新”,看到“上次成功更新”的时间即成功;失败时出现红色
error标签,悬停查看错误。
平台差异速查(排错时最常用)
| 项目 | CodeCommit | GitLab.com |
|---|---|---|
| Git LFS | 不支持(2026 Q1 路线图项目,截至 2026-11-15 文档未见上线) | 支持(所有套餐) |
| 单个对象 / 文件 | 单个 blob ≤ 2 GB;控制台/API/CLI 单文件 ≤ 6 MB | 推送规则或实例设置的最大文件大小;仓库与推送大小有平台限制(见 GitLab.com 设置文档) |
| 单次推送引用数 | ≤ 4,000(创建、删除、更新合计) | 以平台限制为准 |
| 拒绝推送的反馈 | HTTP 403,无原因文字 | remote: GitLab: … 具体原因 |
| 认证失败 | 403(凭据错误与权限不足表现相同) | 401 / “HTTP Basic: Access denied” |
凭据助手冲突是怎么发生的
Git 会按顺序询问 credential.helper 列表中的每个助手(系统级 → 全局 → 仓库级累加)。如果你配置了 AWS CLI 凭据助手,但系统里还有 osxkeychain(macOS Git 默认)或 Git Credential Manager(Windows 默认):
- 第一次推送:AWS 助手现算出签名密码,推送成功后 Git 执行
credential approve,所有助手都保存这份密码——包括钥匙串。 - 约 15 分钟后签名过期,下一次推送时钥匙串排在前面,直接返回过期密码 → 403;Git 执行
credential reject后可能恢复,也可能反复失败。 - 修复:用 URL 作用域的配置为 CodeCommit 主机清空助手列表(空值
helper =),再只加 AWS 助手;删除钥匙串中git-codecommit条目。最佳方案是改用 git-remote-codecommit(不经过凭据助手)。
架构图:GitLab 主仓库 + CodeCommit 只读镜像
流程图:凭据助手冲突导致的“间歇性 403”
交互:推送镜像同步模拟器
在源(GitLab)和目标(CodeCommit)上制造事件,观察镜像在“覆盖分叉 ref”和“Keep divergent refs”两种模式下的结果。
核心命令
| 命令 | 作用 | 说明 |
|---|---|---|
glab api -X POST projects/:id/remote_mirrors | 创建推送镜像 | url(可内嵌凭据,需 URL 编码)、enabled、only_protected_branches、keep_divergent_refs |
glab api projects/:id/remote_mirrors | 查看镜像状态 | update_status、last_successful_update_at、last_error |
glab api -X POST projects/:id/remote_mirrors/:mid/sync | 强制立即同步 | 受频率限制 |
glab api -X PUT projects/:id/remote_mirrors/:mid | 修改(含 keep_divergent_refs) | Keep divergent refs 创建后只能通过 API 修改 |
git config --show-origin --get-all credential.helper | 查看所有助手及来源 | 排查冲突第一步 |
GIT_TRACE=1 GIT_CURL_VERBOSE=1 git … | 输出 HTTP 与助手调用细节 | 注意输出中可能含令牌,勿外发 |
git count-objects -vH、git rev-list --objects --all | git cat-file --batch-check | 找大对象 | 见下 |
git lfs ls-files / git lfs env | LFS 状态 | 目标不支持 LFS 时的判断依据 |
创建镜像(API 版,等价于界面操作)
aws codecommit create-repository --repository-name myapp-mirror --repository-description "Read-only mirror of GitLab" \
--profile cc-admin --region ca-central-1
# M2 实验第 3 步已创建 gitlab-mirror-bot 与 MirrorPushOnly 策略;生成 Git 凭据
read -r CCUSER CCPASS _ <<<"$(aws iam create-service-specific-credential --user-name gitlab-mirror-bot \
--service-name codecommit.amazonaws.com --profile cc-admin \
--query 'ServiceSpecificCredential.[ServiceUserName,ServicePassword,ServiceSpecificCredentialId]' --output text)"
ENC_PASS=$(python3 -c 'import sys,urllib.parse;print(urllib.parse.quote(sys.argv[1],safe=""))' "$CCPASS")
P="alice-git-lab%2Fmyapp"
glab api -X POST "projects/$P/remote_mirrors" \
-f url="https://${CCUSER}:${ENC_PASS}@git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp-mirror" \
-F enabled=true -F only_protected_branches=true -F keep_divergent_refs=false
unset CCPASS ENC_PASS
MID=$(glab api "projects/$P/remote_mirrors" | python3 -c 'import sys,json;print(json.load(sys.stdin)[0]["id"])') glab api -X POST "projects/$P/remote_mirrors/$MID/sync" sleep 30; glab api "projects/$P/remote_mirrors/$MID" | python3 -m json.tool | grep -E 'url|update_status|last_successful|last_error'
"url": "https://*****:*****@git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp-mirror",
"update_status": "finished",
"last_successful_update_at": "2026-11-15T10:42:07.000Z",
"last_error": null,
镜像目标“对人类只读”的 IAM 策略(挂给开发者权限集,不挂给镜像机器人)
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Deny",
"Action": ["codecommit:GitPush","codecommit:PutFile","codecommit:DeleteFile","codecommit:DeleteBranch",
"codecommit:CreateBranch","codecommit:MergeBranchesByFastForward","codecommit:MergeBranchesBySquash",
"codecommit:MergeBranchesByThreeWay","codecommit:MergePullRequestByFastForward",
"codecommit:MergePullRequestBySquash","codecommit:MergePullRequestByThreeWay"],
"Resource": "arn:aws:codecommit:ca-central-1:111122223333:myapp-mirror"
}]
}
这里拒绝该仓库的所有写操作,所以不需要 References 条件(也就不需要 Null 条件)。
凭据助手冲突修复(macOS 示例)
git config --show-origin --get-all credential.helper # 删除钥匙串中缓存的 CodeCommit 条目 git credential-osxkeychain erase <<'EOF' protocol=https host=git-codecommit.ca-central-1.amazonaws.com EOF # 仅对 CodeCommit 主机:清空继承的助手,再只使用 AWS 助手(或干脆改用 GRC) git config --global credential.https://git-codecommit.ca-central-1.amazonaws.com.helper '' git config --global --add credential.https://git-codecommit.ca-central-1.amazonaws.com.helper '!aws codecommit credential-helper $@' git config --global credential.https://git-codecommit.ca-central-1.amazonaws.com.UseHttpPath true
Windows(GCM):在“凭据管理器 → Windows 凭据”中删除 git:https://git-codecommit… 条目;Linux 若配置了 store/cache 助手,同样按主机清空。
找出大对象
git count-objects -vH
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '$1=="blob" && $3 > 50*1024*1024 {printf "%.1f MB %s\n", $3/1048576, $4}' | sort -rn | head
实验 7:GitLab → CodeCommit 推送镜像 + 故障演练(约 9 分钟)
update_status: finished,并用开发者身份克隆镜像核对 main 的提交 SHA。
git ls-remote gitlab refs/heads/main git ls-remote codecommit::ca-central-1://cc-dev@myapp-mirror refs/heads/main
两个 SHA 必须相同。因为只镜像受保护分支,feature 分支不会出现在镜像里。
aws iam update-service-specific-credential --user-name gitlab-mirror-bot \ --service-specific-credential-id ACCAXXXXXXXXXXXXXXXXX --status Inactive --profile cc-admin glab api -X POST "projects/$P/remote_mirrors/$MID/sync"; sleep 30 glab api "projects/$P/remote_mirrors/$MID" | grep -E 'update_status|last_error'
"update_status": "failed", "last_error": "… The requested URL returned error: 403 …",
恢复:把状态改回 Active 再同步。
keep_divergent_refs=true 重复一次,镜像更新被标记为失败。演练后恢复只读策略并把 keep_divergent_refs 设回 false。git push gitlab main,对照 M5 消息;再对 CodeCommit 镜像做同样操作,对照 403。体会两个平台反馈的差异。git lfs track "*.psd" 并提交一个文件,推到 GitLab 成功;尝试直接推到 CodeCommit 测试仓库,观察 LFS 上传失败(CodeCommit 不提供 LFS 端点)。实验后删除测试分支,不要合并到 main,否则会污染镜像。案例 1:镜像突然全部失败——密码里有斜杠
问题:轮换 Git 凭据后,镜像报“URL 格式错误”或认证失败。根因:新密码含 /、+、=,直接拼进 URL 时被当作路径分隔。修复:在界面的“密码”字段单独填写(不放进 URL),或 API 中对密码做 URL 编码。
案例 2:macOS 开发者“每隔一会儿就 403”
问题:半个团队用 AWS CLI 凭据助手,推送间歇性 403。根因:osxkeychain 缓存了过期的签名密码。修复:统一切换到 git-remote-codecommit;临时方案是按主机清空助手列表并删除钥匙串条目。AWS 故障排除文档专门描述了这一现象。
案例 3:镜像仓库超过 4000 个引用
问题:首次镜像一个有 9000 个标签的老仓库到 CodeCommit 失败。根因:CodeCommit 单次推送最多 4000 个引用。修复:首次用命令行分批推送标签(M8 的分批脚本),之后再启用 GitLab 镜像做增量。
交互排障树
按你看到的现象逐步选择;每个诊断都给出命令和修复方法。
常见报错原文
fatal: unable to access 'https://git-codecommit.…/v1/repos/…/': The requested URL returned error: 403
见排障树“CodeCommit 403”分支:先查凭据助手,再查 IAM(含条件策略与 SCP),再查区域与仓库名。
remote: HTTP Basic: Access denied. If a password was provided for Git authentication, the password was incorrect or you're required to use a token instead of a password.
GitLab 认证失败:开启 2FA 的账号必须用个人访问令牌(scope write_repository)或 OAuth(GCM/glab);令牌过期或被撤销也是同一消息。
batch response: … / LFS: Repository or object not found(推送到 CodeCommit)
目标平台不支持 LFS。选择:主仓库留在支持 LFS 的平台;镜像到 CodeCommit 的仓库不使用 LFS;或在等待 CodeCommit LFS 正式上线前把大文件放 S3。
error: RPC failed; HTTP 413 / remote end hung up unexpectedly(大推送)
推送包过大或超过代理/平台限制。分批推送(按提交范围或分支)、移除大文件(第 7 课 git filter-repo)、或改用 LFS(目标支持时)。
1. 一条命令同时比较 GitLab 与 CodeCommit 镜像上所有标签是否一致。
diff <(git ls-remote --tags gitlab | sort) <(git ls-remote --tags codecommit::ca-central-1://cc-dev@myapp-mirror | sort) && echo "tags in sync"
2. 查看是哪个凭据助手为 CodeCommit 返回了密码。
GIT_TRACE=1 git ls-remote https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/myapp 2>&1 | grep -i "credential"
镜像显示“上次成功更新”停在 3 天前,但没人收到告警。如何修复并防止再次发生?
查看 last_error:常见为 Git 凭据被停用/轮换、IAM 策略变更(例如有人把 DenyChangesToMain 挂到了镜像机器人)、目标仓库被改名、镜像目标出现分叉(Keep divergent refs)。修复后强制同步。预防:定时作业用 API 检查 last_successful_update_at 超过阈值即告警;CodeCommit 侧用通知规则 branches-and-tags-updated 的“缺席”做心跳;凭据轮换走 Runbook,同步更新 GitLab 镜像配置。
教学视频
未找到经核实的 “GitLab 推送镜像到 CodeCommit” 专题视频。
官方文档
视频分镜脚本(约 75 秒)
画面:GitLab main 前进,一分钟后箭头把提交推到 CodeCommit。
旁白:推送镜像在上游有推送时触发,只镜像受保护分支时一分钟内完成。
画面:有人直接改了 CodeCommit main,下一次同步时这次提交被覆盖消失。
旁白:默认会覆盖分叉的引用。所以镜像目标要对人类只读。
画面:钥匙串图标递出一把生锈的钥匙,CodeCommit 门口亮起 403。
旁白:间歇性 403,往往是钥匙串缓存了过期的签名密码。改用 git-remote-codecommit 就没有这个问题。
模块 8 · 迁移、成本与自测
- 规划 GitHub / Azure Repos ↔ GitLab / CodeCommit 的迁移:Git 数据(
clone --mirror+ 选择性推送)与平台数据(导入器)分开处理,避开隐藏引用与 4000 引用限制。 - 估算 CodeCommit 成本(活跃用户的定义、免费额度、超额单价)并对比 GitLab 套餐。
- 完成本课自测。
What:迁移的两类数据
| 数据 | 怎么迁 | 工具 |
|---|---|---|
| Git 引用与对象(分支、标签、全部历史) | git clone --mirror → 只推送 refs/heads/* 与 refs/tags/* | Git 本身 |
| PR/MR、评论、Issue、Wiki、发布 | 平台导入器或 API | GitLab 的 GitHub 导入器(迁 PR→MR、Issue 等);“通过 URL 导入仓库”只迁 Git 数据 |
| LFS 对象 | git lfs fetch --all → git lfs push --all | 目标须支持 LFS(CodeCommit 截至核实日不支持) |
| 权限、分支保护、审批、CODEOWNERS 语义 | 按目标平台重新配置(本课 M3/M5/M6 的对照表) | IaC:Terraform 的 gitlab / aws provider |
| 流水线、Webhook、集成 | 重建;AWS 侧改用 CodeConnections | 第 5 课 |
Why:为什么不能直接 git push --mirror
- 隐藏引用:从 GitHub 镜像克隆会带上
refs/pull/*,从 GitLab 会带上refs/merge-requests/*、refs/keep-around/*等。推送到目标时可能被拒绝(GitHub 拒绝更新隐藏引用)或在目标里变成大量垃圾引用。 - CodeCommit 单次推送最多 4000 个引用:老仓库的标签动辄上万,需要分批。
- 受保护分支:目标若已配置保护(例如 main push=No one),首次推送会被拒绝——先推送、后保护。
How:迁移步骤(通用)
- 冻结窗口公告;源仓库设为只读(GitHub 归档、Azure Repos 禁用仓库、GitLab 归档、CodeCommit 用 IAM Deny)。
git clone --mirror源仓库;如有 LFS,git lfs fetch --all。- 在目标创建空仓库,暂不启用分支保护与推送规则(或允许迁移账号绕过)。
- 推送
refs/heads/*与refs/tags/*(CodeCommit 按 ≤ 4000 引用分批)。 - 校验:分支/标签数量、每个分支 SHA 一致、
git fsck。 - 导入平台数据(导入器),重建保护、审批、CODEOWNERS(CodeCommit 无 CODEOWNERS,改用审批规则模板)、Webhook/连接。
- 开发者执行
git remote set-url origin …;源仓库保留只读一段时间或做反向镜像以便回滚。
CodeCommit 成本
| 项目 | 内容(以 官方定价页 为准) |
|---|---|
| 免费层 | 对新老客户长期有效;前 5 个活跃用户免费(AWS 公开的免费额度为每月 50 GB 存储、10,000 次 Git 请求;以定价页为准) |
| 额外活跃用户 | 官方示例:一个月 6 个活跃用户计费 $1 → 每增加 1 个活跃用户 $1/月(每个付费用户附带的额外存储与请求额度以定价页为准) |
| 超额 | 存储 $0.06 / GB-月;Git 请求 $0.001 / 次 |
| 活跃用户定义 | 当月访问过 CodeCommit 仓库的每个唯一 AWS 身份:IAM 用户/角色、联合用户、根账号;通过 Git、控制台、CLI、SDK 访问都算;CodeBuild、CodePipeline 等服务使用的身份也算;当月未访问不计费 |
| 相关费用 | VPC 接口终端节点(按小时+流量)、KMS 客户托管密钥、CloudTrail 数据事件、SNS 等另计 |
GitLab 成本要点
- GitLab.com Free:免费,但私有命名空间有成员数量限制、存储与计算分钟数配额;Premium / Ultimate 按席位(用户)年付,价格见 about.gitlab.com/pricing。
- 本课用到的审批规则、CODEOWNERS 审批、推送规则、拉取镜像需要 Premium;机密推送保护需要 Ultimate。
- Self-Managed:CE 免费,但服务器、备份、升级与人力是主要成本。
git push --mirror 是最完整的迁移”——它会连带隐藏引用,并且会删除目标上源中不存在的引用,指向错误远程时非常危险。迁移矩阵
交互:CodeCommit 月度成本计算器
输入活跃身份与用量,计算器按“前 5 个活跃用户免费、之后 $1/用户、超额 $0.06/GB-月、$0.001/次”估算(额度按 AWS 公开的 50 GB、10,000 次 + 每付费用户 10 GB、2,000 次计算,仅供估算,以定价页为准)。
迁移命令
| 命令 | 作用 |
|---|---|
git clone --mirror URL repo.git | 完整裸克隆(含所有引用) |
git push TARGET 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' | 只推送分支与标签,避开隐藏引用 |
git for-each-ref --format='%(refname)' refs/tags | split -l 3000 | 按批次切分引用(CodeCommit ≤ 4000/次) |
git lfs fetch --all / git lfs push --all TARGET | 迁移 LFS 对象(目标须支持) |
glab repo create / GitLab 导入界面 | 创建目标项目或使用导入器 |
git remote set-url origin NEW | 开发者切换远程 |
GitHub → CodeCommit(分批推送标签)
git clone --mirror https://github.com/acme/legacy-app.git legacy-app.git
cd legacy-app.git
git for-each-ref --format='%(refname)' refs/pull | wc -l # 这些不迁
aws codecommit create-repository --repository-name legacy-app --profile cc-admin --region ca-central-1
TARGET=codecommit::ca-central-1://cc-admin@legacy-app
git push "$TARGET" 'refs/heads/*:refs/heads/*'
git for-each-ref --format='%(refname):%(refname)' refs/tags > tags.txt
split -l 3000 tags.txt tagbatch_
for f in tagbatch_*; do git push "$TARGET" $(cat "$f") || break; done
# 校验
diff <(git for-each-ref --format='%(objectname) %(refname)' refs/heads refs/tags | sort) \
<(git ls-remote "$TARGET" | awk '{print $1" "$2}' | grep -v '\^{}' | sort) && echo "refs match"
Azure Repos → GitLab(仅 Git 数据)
git clone --mirror https://dev.azure.com/acme/Shop/_git/myapp myapp.git glab repo create acme/myapp --private cd myapp.git git push https://gitlab.com/acme/myapp.git 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' # 再按 M5/M6 重建受保护分支、审批与 CODEOWNERS
CodeCommit → GitLab / GitHub(离开 CodeCommit)
git clone --mirror codecommit::ca-central-1://cc-dev@myapp myapp-cc.git cd myapp-cc.git git push https://gitlab.com/acme/myapp.git 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' # CodeCommit 的 PR 与评论不会随 Git 迁移;需要时用 aws codecommit get-pull-request / get-comments-for-pull-request 导出存档
GitLab 的“从 GitHub 导入”可以迁移 PR(→MR)、Issue、评论、里程碑等,适合 GitHub → GitLab;具体可导入的对象与要求(令牌范围、用户映射)以 GitLab 导入文档为准。
实验 8:把 myapp 从 GitLab 迁移到一个新的 CodeCommit 仓库并校验(约 5 分钟)
cd /tmp && git clone --mirror https://gitlab.com/alice-git-lab/myapp.git myapp-mig.git && cd myapp-mig.git git for-each-ref --format='%(refname)' | sed -E 's#^(refs/[^/]+).*#\1#' | sort | uniq -c
6 refs/heads
3 refs/merge-requests
2 refs/tagsaws codecommit create-repository --repository-name myapp-migrated --profile cc-dev --region ca-central-1 git push codecommit::ca-central-1://cc-dev@myapp-migrated 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*'
git clone 一份并 git fsck --full。aws codecommit delete-repository --repository-name myapp-migrated;rm -rf /tmp/myapp-mig.git。案例 1:200 个仓库从 CodeCommit 迁到 GitLab(2024)
做法:脚本遍历 list-repositories,逐个 clone --mirror → glab repo create → 推送分支与标签 → 校验 → 在 CodeCommit 上挂 Deny 写策略设为只读;PR 历史导出为 JSON 存入 S3;CodePipeline 改用 CodeConnections。教训:未迁移的“隐性依赖”是 EventBridge 规则和触发器(Lambda),切换后一周内陆续暴露。
案例 2:GitHub → GitLab 导入后 CODEOWNERS 不生效
问题:GitHub 的 .github/CODEOWNERS 原样迁过来了,但 GitLab 只认根目录、docs/、.gitlab/。修复:移动到 .gitlab/CODEOWNERS,把 GitHub 团队名(@acme/team)映射为 GitLab 群组路径,并确认群组已被邀请到项目;开启代码所有者批准(Premium)。
案例 3:CodeCommit 账单出现 40 个活跃用户
问题:团队只有 15 人,但账单有 40 个活跃用户。根因:每个流水线的 CodeBuild/CodePipeline 服务角色、多个跨账号角色都计入活跃身份。优化:合并流水线服务角色,或改用 CodeConnections 让流水线直接从 GitLab 取代码(视整体成本而定)。
常见错误与解决
! [remote rejected] refs/pull/1/head -> refs/pull/1/head (deny updating a hidden ref)
原因:用 git push --mirror 推送了 GitHub 的隐藏引用。修复:只推送 refs/heads/* 与 refs/tags/*。
推送到 CodeCommit 时报错(引用数量过多,单次推送超过 4000 个引用)
修复:按批次(≤ 3000)推送标签;先推分支,再分批推标签。
remote: GitLab: You are not allowed to push code to protected branches on this project.(迁移首推时)
原因:目标项目默认分支创建即受保护,或群组级保护已生效。修复:迁移账号使用 Maintainer 并临时允许推送,或先推送再配置保护;导入器方式不受此影响。
迁移后开发者 git pull 报 “fatal: refusing to merge unrelated histories”
原因:目标仓库在迁移前被初始化了 README 提交(创建项目时勾选了“初始化仓库”)。修复:重建空仓库再推送;不要在已有历史的目标上合并不相关历史。
1. 统计一个镜像克隆中每类引用的数量。
git for-each-ref --format='%(refname)' | cut -d/ -f1-2 | sort | uniq -c
2. 把所有开发者的 origin 从 CodeCommit 切换到 GitLab(单仓库)。
git remote set-url origin https://gitlab.com/acme/myapp.git && git fetch origin && git branch -u origin/main main
公司要把 30 个 Azure Repos 仓库迁到 CodeCommit(AWS 优先战略),其中 3 个使用 LFS。给出方案。
27 个普通仓库:clone --mirror + 只推分支与标签(必要时分批),在 CodeCommit 上用审批规则模板替代 Azure 的最少审阅者策略,用 IAM Deny 替代“禁止直接推送”,构建验证改为 EventBridge + CodeBuild 回写 PR;Azure 中的“自动包含审阅者”没有对应功能,需用审批人池近似。3 个 LFS 仓库:CodeCommit 截至核实日不支持 LFS,建议暂留在支持 LFS 的平台(或迁到 GitLab/GitHub)并用 CodeConnections 接入 AWS 流水线,待 CodeCommit LFS 正式上线(以用户指南为准)再评估。成本按活跃身份(含流水线服务角色)估算。
经核实的教学视频
未找到经核实的“跨平台仓库迁移”专题视频。
官方文档
- Migrate a Git repository to CodeCommit
- How to migrate your CodeCommit repository to another Git provider(AWS 博客,2024)
- GitLab:Import from GitHub
- CodeCommit pricing
- GitLab pricing
视频分镜脚本(约 60 秒)
画面:镜像克隆展开为 heads、tags、pull 三类引用,pull 被划掉。
旁白:迁移只推分支和标签,隐藏引用留在原地。
画面:上万个标签被分成三千一包,逐包推到 CodeCommit。
旁白:CodeCommit 单次推送最多四千个引用,大仓库要分批。
画面:账单上开发者头像旁边,还站着几个机器人图标。
旁白:CodeCommit 按活跃身份计费,流水线的服务角色也算在内。
命令速查表
CodeCommit 认证
aws configure sso --profile cc-dev && aws sso login --profile cc-dev pipx install git-remote-codecommit git clone codecommit::ca-central-1://cc-dev@myapp git remote add cc codecommit://cc-dev@myapp aws iam create-service-specific-credential --user-name U --service-name codecommit.amazonaws.com aws iam update-service-specific-credential --user-name U --service-specific-credential-id ID --status Inactive aws iam upload-ssh-public-key --user-name U --ssh-public-key-body file://~/.ssh/codecommit_rsa.pub ssh git-codecommit.ca-central-1.amazonaws.com
CodeCommit 仓库与 PR
aws codecommit create-repository --repository-name R --repository-description D aws codecommit get-repository --repository-name R aws codecommit list-branches --repository-name R aws codecommit update-default-branch --repository-name R --default-branch-name main aws codecommit create-pull-request --title T --targets repositoryName=R,sourceReference=feat,destinationReference=main aws codecommit update-pull-request-approval-state --pull-request-id N --revision-id REV --approval-state APPROVE aws codecommit evaluate-pull-request-approval-rules --pull-request-id N --revision-id REV aws codecommit merge-pull-request-by-squash --pull-request-id N --repository-name R aws codecommit delete-repository --repository-name R
CodeCommit 保护与通知
# IAM Deny: codecommit:GitPush/PutFile/DeleteFile/DeleteBranch/Merge*
# StringEqualsIfExists codecommit:References [refs/heads/main]
# Null codecommit:References "false"
aws codecommit create-approval-rule-template --approval-rule-template-name N \
--approval-rule-template-content '{"Version":"2018-11-08","DestinationReferences":["refs/heads/main"],"Statements":[{"Type":"Approvers","NumberOfApprovalsNeeded":2,"ApprovalPoolMembers":["arn:aws:sts::ACCT:assumed-role/ROLE/*"]}]}'
aws codecommit associate-approval-rule-template-with-repository --approval-rule-template-name N --repository-name R
aws codecommit put-repository-triggers --cli-input-json file://trigger.json
aws codestar-notifications create-notification-rule --name N --resource ARN --detail-type FULL \
--event-type-ids codecommit-repository-pull-request-created --targets TargetType=SNS,TargetAddress=TOPICCodeConnections
aws codeconnections create-connection --provider-type GitLab --connection-name N # → PENDING # 控制台:Settings → Connections → Update pending connection → AVAILABLE aws codeconnections get-connection --connection-arn ARN --query Connection.ConnectionStatus aws codeconnections list-connections --provider-type-filter GitHub aws codeconnections create-host --name H --provider-type GitLabSelfManaged --provider-endpoint https://gitlab.corp aws codeconnections delete-connection --connection-arn ARN # IAM: 服务角色 UseConnection(FullRepositoryId 条件);创建者 PassConnection(PassedToService)
GitLab 项目、受保护分支与 MR
glab auth login --hostname gitlab.com && glab auth status glab repo create GROUP/NAME --private --defaultBranch main glab api "projects/GROUP%2FNAME/protected_branches" glab api -X POST "projects/GROUP%2FNAME/protected_branches" -f name=main -f push_access_level=0 -f merge_access_level=30 # 访问级别:0=No one 30=Developer 40=Maintainer 60=Admin git push -u gitlab feat -o merge_request.create -o merge_request.target=main -o merge_request.remove_source_branch glab mr create --target-branch main --fill --yes glab mr approve N && glab mr merge N --squash --remove-source-branch --yes
GitLab 审批 / CODEOWNERS / 推送规则(Premium+)
glab api -X POST "projects/P/approval_rules" -f name=Default -F approvals_required=2 glab api -X POST "projects/P/approvals" -F merge_requests_author_approval=false -F merge_requests_disable_committers_approval=true glab api -X PATCH "projects/P/protected_branches/main" -F code_owner_approval_required=true glab api -X POST "projects/P/push_rule" -f commit_message_regex='^(feat|fix|docs|chore): ' \ -F prevent_secrets=true -F max_file_size=10 -F deny_delete_tag=true # CODEOWNERS 位置:./CODEOWNERS → ./docs/CODEOWNERS → ./.gitlab/CODEOWNERS(首个生效) # [Section] [Section][2] ^[Optional] 最后匹配的条目生效
镜像
glab api -X POST "projects/P/remote_mirrors" \ -f url="https://USER:URLENCODED_PASS@git-codecommit.ca-central-1.amazonaws.com/v1/repos/R" \ -F enabled=true -F only_protected_branches=true -F keep_divergent_refs=false glab api "projects/P/remote_mirrors" glab api -X POST "projects/P/remote_mirrors/MID/sync" glab api -X DELETE "projects/P/remote_mirrors/MID" git ls-remote gitlab refs/heads/main; git ls-remote codecommit://cc-dev@R refs/heads/main
排错与迁移
git config --show-origin --get-all credential.helper GIT_TRACE=1 GIT_CURL_VERBOSE=1 git ls-remote REMOTE 2>&1 | grep -E "helper|HTTP/" git config --global credential.https://git-codecommit.REGION.amazonaws.com.helper '' aws sts get-caller-identity git clone --mirror SRC repo.git git push DST 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' git for-each-ref --format='%(refname):%(refname)' refs/tags | split -l 3000 - tagbatch_ git lfs ls-files; git count-objects -vH
平台对照速记
| 需求 | CodeCommit | GitLab | GitHub(第 3 课) | Azure Repos(第 3 课) |
|---|---|---|---|---|
| 禁止直推 main | IAM Deny + References + Null | 受保护分支 push=No one | 规则集 | 分支策略 |
| 必需审批 | 审批规则模板 | 审批规则(Premium) | 规则集审批数 | 最少审阅者 |
| 路径所有者 | — | CODEOWNERS(Premium) | CODEOWNERS | 自动包含审阅者 |
| 提交规范 / 禁止机密文件 | —(需自建检查) | 推送规则(Premium) | 规则集元数据规则 / 推送保护 | — |
| 推荐人类认证 | GRC + Identity Center | OAuth(GCM/glab)/ PAT | GCM | GCM + Entra ID |
| LFS | 不支持(截至 2026-11-15) | 支持 | 支持 | 支持 |
综合实验:双平台保护 + GitLab → CodeCommit 推送镜像
| 资源 | 费用 | 所需权限 | 清理方式 |
|---|---|---|---|
CodeCommit 仓库 myapp、myapp-mirror | 免费层(≤ 5 活跃用户) | cc-dev(PowerUser)、cc-admin(IAM) | aws codecommit delete-repository |
| IAM:DenyChangesToMain、deny-mirror-writes、gitlab-mirror-bot + Git 凭据、审批规则模板 | 免费 | IAM 管理员 | 删除凭据 → 删除内联策略 → 删除用户 → 分离并删除策略 → 删除模板 |
GitLab 项目 alice-git-lab/myapp、镜像、PAT | Free | 群组 Owner | 删除镜像 → 删除项目 → 撤销 PAT |
| (可选)CodeConnections 连接、SNS 主题 | 免费 / 免费层 | cc-admin | delete-connection、sns delete-topic |
aws sso login --profile cc-dev(开发者)与 --profile cc-admin(管理员);glab auth status;确认 ~/git-lab/myapp 干净(git status)。
export AWS_REGION=ca-central-1
ACCOUNT=$(aws sts get-caller-identity --profile cc-admin --query Account --output text)
GL_NS=alice-git-lab; P="${GL_NS}%2Fmyapp"cd ~/git-lab/myapp aws codecommit get-repository --repository-name myapp --profile cc-dev >/dev/null 2>&1 || \ aws codecommit create-repository --repository-name myapp --profile cc-dev git remote get-url cc 2>/dev/null || git remote add cc codecommit::ca-central-1://cc-dev@myapp git push cc --all && git push cc --tags # 管理员:创建并挂载 DenyChangesToMain(M3 命令分页),关联 2-approver-rule-for-main 模板 git commit --allow-empty -m "test: direct push" && git push cc main; git reset --hard HEAD~1
期望:最后一次推送返回 403。
glab repo view "$GL_NS/myapp" >/dev/null 2>&1 || glab repo create "$GL_NS/myapp" --private --defaultBranch main git remote get-url gitlab 2>/dev/null || git remote add gitlab "https://gitlab.com/$GL_NS/myapp.git" git push gitlab --all && git push gitlab --tags glab api -X DELETE "projects/$P/protected_branches/main" || true glab api -X POST "projects/$P/protected_branches" -f name=main -f push_access_level=0 -f merge_access_level=30 git commit --allow-empty -m "test: direct push" && git push gitlab main; git reset --hard HEAD~1
期望:remote: GitLab: You are not allowed to push code to protected branches on this project.
myapp-mirror;确认 gitlab-mirror-bot 的内联策略只允许对 arn:aws:codecommit:ca-central-1:${ACCOUNT}:myapp-mirror 的 GitPull/GitPush;生成 Git 凭据;把 deny-mirror-writes 挂到开发者权限集。
aws codecommit create-repository --repository-name myapp-mirror --profile cc-admin aws iam get-user-policy --user-name gitlab-mirror-bot --policy-name MirrorPushOnly --profile cc-admin --query PolicyDocument aws iam list-attached-user-policies --user-name gitlab-mirror-bot --profile cc-admin # 不应有 DenyChangesToMain
MID=$(glab api "projects/$P/remote_mirrors" | python3 -c 'import sys,json;print(json.load(sys.stdin)[0]["id"])') glab api -X POST "projects/$P/remote_mirrors/$MID/sync"; sleep 45 [ "$(git ls-remote gitlab refs/heads/main | cut -f1)" = "$(git ls-remote codecommit::ca-central-1://cc-dev@myapp-mirror refs/heads/main | cut -f1)" ] && echo "MIRROR OK"
git clone 成功(只读可用)。
git switch -c docs/final-lab && echo "final lab $(date -u +%F)" >> docs/adr/0004-hosting.md git commit -am "docs: final lab check" && git push -u gitlab docs/final-lab -o merge_request.create -o merge_request.target=main glab mr merge --squash --remove-source-branch --yes sleep 60; git ls-remote codecommit::ca-central-1://cc-dev@myapp-mirror refs/heads/main git push codecommit::ca-central-1://cc-dev@myapp-mirror HEAD:refs/heads/hack # 期望 403
AVAILABLE 后删除。# GitLab:删除镜像与项目,撤销 PAT(用户设置 → 访问令牌)
glab api -X DELETE "projects/$P/remote_mirrors/$MID"
glab repo delete "$GL_NS/myapp" --yes # GitLab.com 可能先进入“待删除”状态,按保留期后彻底删除
# AWS:仓库
for r in myapp myapp-mirror; do aws codecommit delete-repository --repository-name $r --profile cc-admin; done
# 审批规则模板(先解除关联,仓库已删除时可直接删)
aws codecommit delete-approval-rule-template --approval-rule-template-name 2-approver-rule-for-main --profile cc-admin
# 镜像机器人:凭据 → 内联策略 → 用户
for id in $(aws iam list-service-specific-credentials --user-name gitlab-mirror-bot --profile cc-admin \
--query 'ServiceSpecificCredentials[].ServiceSpecificCredentialId' --output text); do
aws iam delete-service-specific-credential --user-name gitlab-mirror-bot --service-specific-credential-id $id --profile cc-admin; done
aws iam delete-user-policy --user-name gitlab-mirror-bot --policy-name MirrorPushOnly --profile cc-admin
aws iam delete-user --user-name gitlab-mirror-bot --profile cc-admin
# 客户托管策略:先从角色/权限集分离,再删除
aws iam list-entities-for-policy --policy-arn arn:aws:iam::${ACCOUNT}:policy/DenyChangesToMain --profile cc-admin
# … detach-role-policy / detach-customer-managed-policy-reference-from-permission-set …
aws iam delete-policy --policy-arn arn:aws:iam::${ACCOUNT}:policy/DenyChangesToMain --profile cc-admin
# 本地:移除远程
git remote remove cc; git remote remove gitlab
aws sso logout
如创建过 SSH 公钥、SNS 主题、通知规则、触发器、CodeConnections 连接,也一并删除;GitLab 侧撤销 AWS 连接的 OAuth 授权。
开放式自测(先自己回答,再展开参考答案)
1. 为什么 Identity Center 用户不能使用 CodeCommit 的 HTTPS Git 凭据?他们应该怎么连接?
Git 凭据是 IAM 用户的服务专用凭据,Identity Center 用户在 IAM 中只是代入角色的会话,没有 IAM 用户对象。应安装 AWS CLI v2 与 git-remote-codecommit,用 SSO 配置文件和 codecommit::区域://配置文件@仓库 URL 连接,GRC 用会话临时凭据现算 SigV4 签名。
2. CodeCommit 分支保护策略中 Null 条件的作用是什么?去掉会怎样?
"Null": {"codecommit:References": "false"} 要求请求中存在 References 上下文键,Deny 才可能生效。去掉后 StringEqualsIfExists 在键不存在时为真,推送过程中不带该键的请求也被拒绝,导致推送任何分支都失败。官方要求所有 Deny 且含 GitPush 的策略都加 Null 条件。
3. CodeCommit 审批规则模板能否阻止直接推送到 main?完整方案是什么?
不能,它只约束 PR 合并。完整方案:IAM Deny(GitPush、PutFile、DeleteFile、DeleteBranch、Merge*)+ References/Null 条件挂到开发者,审批规则模板约束 PR,发布负责人/流水线角色保留合并权限,并对审批规则覆盖事件告警。
4. 描述 GitLab 拒绝推送到受保护分支的内部过程。
Gitaly 接收推送(receive-pack),在更新 ref 前运行 pre-receive 钩子,调用 Rails 内部 API /internal/allowed;Rails 检查项目权限、受保护分支、推送规则等,失败返回 false 和消息;钩子非零退出,ref 不更新;客户端显示 remote: GitLab: … 与 pre-receive hook declined。
5. CodeConnections 的 UseConnection 与 PassConnection 分别给谁?连接为什么会是 PENDING?
UseConnection 给运行时使用连接的服务角色(如 CodePipeline 服务角色),可加 FullRepositoryId 等条件;PassConnection 给创建/更新流水线、把连接 ARN 交给服务的操作者(PassedToService 条件)。CLI/CloudFormation 创建的连接默认 PENDING,必须在控制台完成与提供商的 OAuth 握手才变 AVAILABLE。
6. GitLab → CodeCommit 推送镜像有哪些常见故障?如何监控?
Git 凭据被停用/轮换、密码未 URL 编码、IAM 策略变化(镜像机器人被挂了 Deny)、镜像目标被人直接修改导致分叉(Keep divergent refs 时失败,默认则被覆盖)、超过 4000 引用、含 LFS 对象(CodeCommit 不支持)。监控:定时用 API 检查 update_status/last_successful_update_at,CodeCommit 通知规则做心跳,凭据轮换走 Runbook。
结业测验(12 题,覆盖全部模块)
来源与版本说明
| 主题 | 核实结论 | 来源(访问日期 2026-11-15) |
|---|---|---|
| CodeCommit 状态 | 2024-07-25 不再向新客户开放;2025-11-24 宣布恢复 GA(文档历史 2025-11-25);29 个区域;价格与 99.9% SLA 不变;路线图:Git LFS 2026 Q1、eu-south-2 / ca-west-1 自 2026 Q3 起。截至核实日文档历史与配额页无 LFS 与新区域上线记录 | AWS DevOps 博客(2025-11-24) · 文档历史 · 配额 |
| CodeCommit 连接方式 | Git 凭据为“最简单的方式”;联合访问/临时凭据/Web 身份无法使用 Git 凭据,推荐 git-remote-codecommit;GRC 也可用于 IAM 用户且无需 Git 凭据;需 AWS CLI v2 支持 SSO 与临时凭据;SSH 与 CLI 凭据助手为备选,后者可能与系统助手冲突;URL 格式 codecommit://profile@repo、codecommit::region://repo | Setting up · git-remote-codecommit |
| 分支限制策略 | Deny 动作列表(GitPush、DeleteBranch、DeleteFile、PutFile、MergeBranchesBy*×3、MergePullRequestBy*×3);StringEqualsIfExists codecommit:References + Null … "false",Null 对含 GitPush 的 Deny 策略为必需;建议挂到组或角色 | Limit pushes and merges to branches |
| 审批规则模板 | 内容 Version 2018-11-08、DestinationReferences、Approvers/NumberOfApprovalsNeeded/ApprovalPoolMembers;代入角色会话需通配符;不支持跨账号审批;配额:模板 1000/区域、PR 上规则 ≤ 30(模板 ≤ 25)、池成员 ≤ 50、内容 ≤ 3000 字符 | Create an approval rule template · 配额页 |
| 通知与触发器 | 通知事件 ID(comments、approvals-status-changed / rule-override、pull-request-created / source-updated / status-changed / merged、branches-and-tags-created / deleted / updated);触发器事件 all / updateReference / createReference / deleteReference;SNS FIFO 不支持;每仓库 ≤ 10 个触发器 | Notification concepts · Trigger for SNS |
| CodeCommit 配额 | blob ≤ 2 GB;控制台/API/CLI 单文件 ≤ 6 MB;单次推送 ≤ 4000 引用;仓库 ≤ 5000/账号(可调);ca-central-1 在区域列表中 | 配额页 |
| CodeCommit 定价 | 免费层对新老客户长期有效;示例 6 个活跃用户计 $1;超额存储 $0.06/GB-月、请求 $0.001/次;活跃用户定义含服务创建的身份。定价页当时未列出免费额度数值,本课中的 50 GB / 10,000 次 / 每付费用户 10 GB、2,000 次为 AWS 长期公开的额度,按“以定价页为准”标注 | CodeCommit pricing |
| CodeConnections | 提供商:Bitbucket、GitHub/GHEC、GHES、GitLab、GitLab 自管、Azure DevOps;2024-07-01 起控制台使用 codeconnections ARN,旧前缀仍可用,策略前缀须与资源匹配;CLI/CloudFormation 创建为 PENDING,需控制台更新;GitLab 连接需 Owner 角色、仅为授权账号提供访问;UseConnection / PassConnection 及条件键 | Create a connection to GitLab · IAM 参考 |
| GitLab 版本 | 19.5 于 2026-10-15 发布;每月发布 | GitLab 19.5 release notes |
| GitLab 受保护分支 | 所有套餐;选项 No one / Developers + Maintainers / Maintainers(+ 部署密钥);按用户/群组授权、群组级受保护分支、代码所有者批准为 Premium/Ultimate;默认分支自动受保护;多规则时最宽松生效;“需要代码所有者批准”同时拒绝直接推送 | Protected branches |
| GitLab 审批 / CODEOWNERS / 推送规则 | 审批规则页面为 Premium/Ultimate;默认禁止创建者批准,提交者默认可批准;CODEOWNERS(Premium/Ultimate)按根目录 → docs/ → .gitlab/ 查找首个;推送规则(Premium/Ultimate)含提交消息/分支名/作者邮箱正则、拒绝未签名、禁止删标签、禁止机密文件(files_denylist.yml,按文件名)、文件名正则、最大文件大小、DCO | Approval rules · Code Owners · Push rules |
| GitLab 推送镜像 | 所有套餐;推送后 5 分钟内同步,只镜像受保护分支时 1 分钟内;默认覆盖分叉 ref,Keep divergent refs 时标记失败且只能用 API 修改;CodeCommit 配置步骤(IAM 用户、显式区域与账号的 ARN、Git 凭据、建议只镜像受保护分支) | Push mirroring |
视频核实说明
以下视频于 2026-11-15 通过 YouTube oEmbed 接口核实了标题与频道:Valentin Despa《Git for GitLab (Beginner's FULL COURSE)》(4lxvVj7wlZw);TechWorld with Nana《GitLab CI CD Tutorial for Beginners [Crash Course]》(qP8kir2GUgo);Programming with Mosh《Git Tutorial for Beginners: Learn Git in 1 Hour》(8JJ101D3knE);freeCodeCamp.org《Git and GitHub for Beginners - Crash Course》(RGOj5yH7evk)。oEmbed 不返回时长与发布日期,因此卡片中未写时长。未找到经核实的 AWS CodeCommit(git-remote-codecommit、审批规则、2025 年恢复 GA 后)、CodeConnections、GitLab CODEOWNERS/推送规则、GitLab→CodeCommit 镜像的专题视频,相应模块提供了分镜脚本。所有视频均为英文,界面可能与当前版本不同。
语音讲解说明
“语音”分页使用浏览器的 Web Speech API(speechSynthesis,zh-CN)实时朗读讲解稿,英文术语用 en-US 发音,效果取决于操作系统安装的语音。本课件没有内嵌 MP3;需要离线音频时,可把讲解稿交给 TTS 服务生成。
已知限制
- 实验命令未在本课件生成环境中实际执行;“预期输出”中的账号 ID、ARN、SHA、凭据 ID、时间与版本号均为示例值。
- CodeCommit Git LFS 与新区域:只依据截至 2026-11-15 的用户指南文档历史与配额页判断为“尚未上线”,若 AWS 之后发布,请以 What's New 为准。
- CodeCommit 免费额度的具体数值(50 GB、10,000 次、每付费用户 10 GB / 2,000 次)未在核实当日的定价页正文中显示,课件中以“以定价页为准”标注;成本计算器仅供估算。
- git-remote-codecommit 的签名细节(用户名/密码格式、规范请求)依据其开源实现整理,属于实现细节,可能变化。
- GitLab 推送规则、机密文件拦截等的拒绝消息原文随版本变化,课件中标注为示例;受保护分支的两条拒绝消息为长期稳定的原文。
- GitLab 功能的套餐归属可能调整(例如功能下放到 Free),以各文档页面顶部的 Tier 标注为准;GitLab.com Free 的成员数、存储等配额以 GitLab.com 当前政策为准。
- 动画中的时序、签名值、计时为示意,用于说明机制。