我正在使用 Terraform 来管理 Github 组织。我们有一个标准的“通用存储库”模块,用于确保我们的存储库共享通用配置。我想添加对配置 GitHub 页面的支持,这需要对pages
元素的支持,如下所示:
pages {
build_type = "legacy"
cname = "example.com"
source {
branch = "master"
path = "/docs"
}
}
一切都是可选的。特别是,source
只有在 时才是必需的build_type == "legacy" || build_type == null
,整个pages
块可以省略。我不知道如何进行source
条件化,所以我最终将其拆分成两个dynamic
块,如下所示:
# There are two `dynamic "pages"` blocks here to account for the fact that `source` is only required
# if `build_type` is "legacy". The `for_each` at the top of each block will only enable the block when
# the necessary conditions are met.
dynamic "pages" {
# enable this block if `pages` is not null and `build_type` is "legacy" (or null)
for_each = var.pages == null ? [] : var.pages.build_type == "legacy" || var.pages.build_type == null ? ["enabled"] : []
content {
source {
branch = var.pages.source.branch
path = var.pages.source.path
}
cname = var.pages.cname
build_type = var.pages.build_type
}
}
dynamic "pages" {
# enable this block if `pages` is not null and `build_type` is "workflow"
for_each = var.pages == null ? [] : var.pages.build_type == "workflow" ? ["enabled"] : []
content {
cname = var.pages.cname
build_type = var.pages.build_type
}
}
pages
我在模块中定义变量如下:
variable "pages" {
description = "Configuration for github pages"
type = object({
source = optional(object({
branch = string
path = string
}))
build_type = optional(string, "legacy")
cname = optional(string)
})
default = null
}
有没有更好的方法来解决这个问题?