尝试使用Terraform管理Github仓库

为什么

如果将Github仓库的管理权限手动管理,就无法看到每个仓库的管理员权限属于谁(也许可以通过更改某些权限来显示管理员身份,但如果有这样的功能,请告知)。

编码化

由于存在Github的Terraform提供者,通过对Github存储库和存储库权限进行代码化,我们可以实现信息的透明化并且更容易进行确认。

使用Terraform准备管理Github(Terraform云)

Screen Shot 2021-07-08 at 10.04.57.png

只需将您想要编写的资源写入tf文件并应用即可实现基本功能。准备就绪。

使用Terraform创建资源

代码库

resource "github_repository" "terraform-github-template" {
  name        = "terraform-github-template"
  description = "template repository create by terraform"

  visibility = "public"
}

Repository的分支保护

resource "github_branch_protection" "terraform-github-template-main" {
  repository_id = github_repository.terraform-github-template.node_id

  pattern          = "main"
  enforce_admins   = true
  allows_deletions = false
}

如果在上述准备中应用了自动化(使用Terraform Cloud),

    1. 如果想要在Terraform Cloud中自動應用代碼合併時,需要設定版本控制並勾選自動應用。

 

    1. 在版本控制中設定的儲存庫中創建初始PR來確認Terraform Cloud是否運作正常。

 

    合併後會自動應用。

导入现有资源

请导入https://github.com/nakamasato/eks

    1. 准备tf文件

import.tf
resource “github_repository” “eks” {
name = “eks”

visibility = “public”
}

导入terraform import github_repository.terraform terraform (在Terraform Cloud中需要进行 terraform login)

terraform import github_repository.eks eks

(可选)修改tf文件

当执行terraform plan时,可能会发现自己创建的tf文件和导入的state文件之间存在差异,需要进行修正直到出现 No changes. Your infrastructure matches the configuration.

例 最终状态应该是这样的

import.tf
resource “github_repository” “eks” {
name = “eks”
description = “Manage EKS cluster with Terraform (development purpose only)”
allow_merge_commit = false
allow_rebase_merge = false
has_downloads = true
has_issues = true
has_projects = true
has_wiki = true
vulnerability_alerts = true
topics = [
]

visibility = “public”
}

推送代码(Terraform Cloud没有更改)

请注意:

即使在Terraform Cloud中通过环境变量设置了GITHUB_TOKEN,但在导入时,本地也需要设置GITHUB_TOKEN,否则将会出现以下错误。

terraform import github_repository.python-sample python-sample            
Acquiring state lock. This may take a few moments...
╷
│ Error: Cannot import non-existent remote object
│ 
│ While attempting to import an existing object to "github_repository.python-sample", the provider detected that no object exists with the given id. Only pre-existing
│ objects can be imported; check that the id is correct and that it is associated with the provider's configured region or endpoint, or use "terraform apply" to
│ create a new remote object for this resource.

设置GITHUB_TOKEN。

export GITHUB_TOKEN=xxxxx

再次尝试:

terraform import github_repository.python-sample python-sample
Acquiring state lock. This may take a few moments...

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

总结

我尝试使用Terraform来管理Github上的存储库和分支权限。可以管理存储库的管理权限以及谁具有合并权限等,可以公开谁管理和具有合并权限的信息。

请提供以下信息供参考。

    • Github Repository

 

    • Github Branch Protection

 

    • terraform-import failure: Cannot import non-existent remote objec

 

    • Some resources can only be used in the context of an organization

 

    Error: This resource can only be used in the context of an organization, “foo” is a user

待办事项

Error: This resource can only be used in the context of an organization, “xxxxx” is a user. というエラーが出てうまく管理できない事がある (Some resources can only be used in the context of an organization Error: This resource can only be used in the context of an organization, “foo” is a userにissueがあるがまだうまく解決できてないので次回)

bannerAds