用途
git-repo 用于通过 OpenTofu 在 github-infrastructure 仓库中新增模块,从而创建新的 GitHub 仓库。它面向需要以基础设施即代码方式管理 GitHub 仓库的工作流,把“创建仓库”转化为一次可审查的代码变更和 PR。
主要能力
- 收集仓库创建参数:name(必填,kebab-case)、description(必填,一行描述)、visibility(默认 private,可选 public)、is_template(默认 false)、default_branch(默认 main)、extra_branches(默认空)、status_checks(默认包含 "Claude Code Review"、"CodeRabbit"、"Lint")、category(默认自动推断)。
- 分类推断规则:名称以 -spec 结尾归为 Specs;以 -template 结尾归为 Templates;名称包含 infrastructure、ci-、org- 归为 Infrastructure;其余归为 Projects。
- 在临时目录克隆 github-infrastructure 仓库并创建特性分支。
- 生成仓库模块文件:tofu/repositories/<repo-name>/ 下的 main.tf、outputs.tf、versions.tf。
- 更新根模块:在 tofu/main.tf 对应分类注释下新增 module 块;在 tofu/outputs.tf 的 repositories 与 branch_protections 输出中新增对应条目。
- 提交、推送并创建 PR,最后清理临时目录并报告 PR URL。
使用方式
整体流程分六步:
- 收集参数(名称、描述、可见性、模板、默认分支、额外分支、状态检查、分类)。
- 克隆到临时目录: WORK_DIR=$(mktemp -d) git clone --depth 1 git@github.com:OlechowskiMichal/github-infrastructure.git "$WORK_DIR/github-infrastructure" cd "$WORK_DIR/github-infrastructure" git checkout -b feat/add-<repo-name>
- 创建模块文件,使用 references/templates.md 中的精确模板。
- 更新根模块,在正确分类下添加 module 引用,并在两个输出中补充条目:
module "<module_name>" {
source = "./repositories/<repo-name>"
}
模块名需将连字符和点替换为下划线,并去掉开头点。
输出示例:
repositories:
"<repo-name>" = module.<module_name>.repositorybranch_protections:
"<repo-name>" = module.<module_name>.branch_protection - 提交、推送并创建 PR: tofu -chdir=tofu fmt -recursive git add tofu/ git commit -m "feat: add <repo-name> repository" git push -u origin feat/add-<repo-name> gh pr create --title "feat: add <repo-name> repository" --body "Add OpenTofu module for <repo-name>"
- 清理临时目录: [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ] && rm -rf "$WORK_DIR" 最后向用户报告 PR URL。
OPA 政策要求
CI 会拒绝违反以下要求的配置,属于强制项:
- vulnerability_alerts = true
- delete_branch_on_merge = true
- allow_merge_commit = false
- allow_squash_merge = true
- has_wiki = false
- has_projects = false
- has_issues = true
- 分支保护需包含 required_status_checks 与 required_pull_request_reviews
- allows_force_pushes = false、allows_deletions = false
- 至少 1 个批准审查
- github_branch_protection.extra 块必须始终存在(即使 for_each = toset([]))
- 仓库资源需 lifecycle { prevent_destroy = true }
- archive_on_destroy = true
适用场景
适合以 github-infrastructure 仓库集中管理 GitHub 仓库配置的团队或个人,需要按统一模板与政策创建新仓库时使用;尤其适合要求 PR 审查、分支保护、CI 状态检查等治理规范的仓库创建。
限制
资料未提供其他收费、价格、平台兼容性或性能方面的信息。使用前提是已具备对该 github-infrastructure 仓库的访问权限、OpenTofu 与 gh 命令环境,并遵循上述 OPA 强制要求,否则 CI 会拒绝。