一:背景
我们在通过terraform手动创建workspace绑定github的时候,无法指定具体的分支,我们可以通过terraform的“tfe_workspace”方式来进行管理,详细可以参照官网的设置Terraform Registry。
二: 设置VCS Providers
具体方式可以参照我的上一篇文章DevOps - (1)Terraform+阿里云实现云资源的CI/CD_terraform 和cicd_菲尼克斯PROFICLOUD剑客-徐传森的博客-CSDN博客
三: 通过terraform管理自动创建workspace
# terraform managed workspaces
#如果有多个环境,比如development环境对应github的main branch,production 环境对应github的 production branch,那么可以通过如下的方式去设置
resource "tfe_workspace" "example" {
for_each = local.environments
name = "demo-${each.key}"
organization = data.tfe_organization.demo.name
tag_names = ["test", "app"]
auto_apply = false
allow_destroy_plan = false
file_triggers_enabled = false
queue_all_runs = false
terraform_version = "1.3.7"
vcs_repo {
identifier = "github账号/仓库名"
branch = each.key == "production" ? "production" : "main"
oauth_token_id = data.tfe_oauth_client.github_oauth_client.oauth_token_id
}
}
data "tfe_oauth_client" "github_oauth_client" {
# In TF Cloud under VCS Providers, you find the OAuth Token ID starting with "ot-"
# Now go to https://app.terraform.io/api/v2/organizations/<Your org>/oauth-tokens
# There you will find a relationship to an OAuth client with an ID starting with "oc-"
oauth_client_id = "oc-xxxxxx"
}
四: 手动触发
通过terraform自动创建的workspace,第一次视乎无法被自动触发,需要手动运行一次。然后后面就可以通过github的对应分支自动触发了。
登录terraform控制台,点击左上角的Action-》start new run。