我需要为很多存储库创建CI/CD 管道并保护 GITHUB 中的一些特定分支。但是如果远程分支不存在,我会得到一个错误。
只有当我已经在 GITHUB 中创建了远程分支时,它才有效。但我需要通过 Terraform 或自动化方式来完成这一切。
# Configure the GitHub Provider
provider "github" {
token = "${var.github_token}"
organization = "${var.github_organization}"
}
# Protect the CI/CD branch of the foo repository
resource "github_branch_protection" "foo" {
repository = "foo"
branch = "staging"
enforce_admins = true
required_pull_request_reviews {
required_approving_review_count = 2
}
}
使用 GITHUB 远程分支的 Terraform 结果:
github_branch_protection.foo: Creating...
github_branch_protection.foo: Creation complete after 3s [id=foo:staging]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
(实际错误)没有 GITHUB 远程分支的 Terraform 结果:
Error: PUT https://api.github.com/repos/jetprogramming/foo/branches/staging/protection: 404 Branch not found []