Terraform:创建 GitHub 组织存储库而不是用户存储库
Terraform: create GitHub Organization Repository instead of a user repository
TF 文件结构:
- ./
- main.tf(参见下面的代码,github 此处定义的提供商)
- module1(文件夹)
- module1.tfvars
- github-repos.tf(见下方代码)
- main.tf(空白)
- variables.tf(为模块定义,值来自tfvar文件)
- module2(文件夹)
- module3(文件夹)
我在根 main.tf
文件中设置了 GitHub 集成,如下所示:
./main.tf
terraform {
required_version = ">= 1.0.9"
backend "s3" {
}
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
}
provider "github" {
owner = "githuborgname"
base_url = "https://github.com/githuborgname/" # we have GitHub Enterprise
token = github_mgmt_token
}
github_mgmt_token
稍后来自同一个 main.tf
文件的输出并且似乎运行良好(因为在 PAT 用户回购下成功创建了回购)。
在一个模块中,我有一个 github-repos.tf
文件,如下所示:
./moduel1/github-repos.tf
resource "github_repository" "ssc" {
name = "ssc"
description = "text"
homepage_url = "https://internalurl.com"
visibility = "private"
delete_branch_on_merge = true
auto_init = true
gitignore_template = "Python"
archive_on_destroy = true
vulnerability_alerts = true
}
这成功创建了 repo,没有问题,但它在 PAT 用户帐户而不是 GitHub 组织中。
如何创建组织存储库?
我认为您需要使用提供者的 owner
参数:
owner - (Optional) This is the target GitHub organization or individual user account to manage. For example, torvalds and github are valid owners. It is optional to provide this value and it can also be sourced from the GITHUB_OWNER environment variable. When not provided and a token is available, the individual user account owning the token will be used. When not provided and no token is available, the provider may not function correctly.
取自https://registry.terraform.io/providers/integrations/github/latest/docs
即
provider "github" {
token = var.token
owner = "myorg" // <--- here
}
我认为这是 1.0.9
和 1.0.10
的错误(至少,也许其他版本也是如此),如果我提供 $GITHUB_OWNER
或 $OWNER
env var 当 运行 Terraform 应用时,这会按预期工作并在 org 中创建 repo。出于某种原因,它没有 honor/see/understand 这些版本中预期的提供者参数。
TF 文件结构:
- ./
- main.tf(参见下面的代码,github 此处定义的提供商)
- module1(文件夹)
- module1.tfvars
- github-repos.tf(见下方代码)
- main.tf(空白)
- variables.tf(为模块定义,值来自tfvar文件)
- module2(文件夹)
- module3(文件夹)
我在根 main.tf
文件中设置了 GitHub 集成,如下所示:
./main.tf
terraform {
required_version = ">= 1.0.9"
backend "s3" {
}
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
}
provider "github" {
owner = "githuborgname"
base_url = "https://github.com/githuborgname/" # we have GitHub Enterprise
token = github_mgmt_token
}
github_mgmt_token
稍后来自同一个 main.tf
文件的输出并且似乎运行良好(因为在 PAT 用户回购下成功创建了回购)。
在一个模块中,我有一个 github-repos.tf
文件,如下所示:
./moduel1/github-repos.tf
resource "github_repository" "ssc" {
name = "ssc"
description = "text"
homepage_url = "https://internalurl.com"
visibility = "private"
delete_branch_on_merge = true
auto_init = true
gitignore_template = "Python"
archive_on_destroy = true
vulnerability_alerts = true
}
这成功创建了 repo,没有问题,但它在 PAT 用户帐户而不是 GitHub 组织中。
如何创建组织存储库?
我认为您需要使用提供者的 owner
参数:
owner - (Optional) This is the target GitHub organization or individual user account to manage. For example, torvalds and github are valid owners. It is optional to provide this value and it can also be sourced from the GITHUB_OWNER environment variable. When not provided and a token is available, the individual user account owning the token will be used. When not provided and no token is available, the provider may not function correctly.
取自https://registry.terraform.io/providers/integrations/github/latest/docs
即
provider "github" {
token = var.token
owner = "myorg" // <--- here
}
我认为这是 1.0.9
和 1.0.10
的错误(至少,也许其他版本也是如此),如果我提供 $GITHUB_OWNER
或 $OWNER
env var 当 运行 Terraform 应用时,这会按预期工作并在 org 中创建 repo。出于某种原因,它没有 honor/see/understand 这些版本中预期的提供者参数。