如何通过Terraform为Azure DevOps非默认分支配置分支策略
配置Azure DevOps非默认分支的分支策略
要将azuredevops_branch_policy_status_check策略应用到指定的非默认分支(比如test-branch),你需要修改资源中的settings.scope块,具体调整如下:
- 移除默认分支绑定配置:删除原代码中
match_type = "DefaultBranch"的scope块,不再将策略关联到默认分支。 - 指定目标非默认分支:在保留的scope块中,将
repository_ref的值替换为目标分支的完整引用路径refs/heads/test-branch,保持match_type = "Exact"以精确匹配该分支。
修改后的完整Terraform代码示例:
resource "azuredevops_user_entitlement" "example-entitlement" { principal_name = var.username account_license_type = "basic" } resource "azuredevops_branch_policy_status_check" "example-status-check" { project_id = azuredevops_project.project.id enabled = true blocking = true settings { name = "Release" author_id = azuredevops_user_entitlement.example-entitlement.id invalidate_on_update = true applicability = "conditional" display_name = "PreCheck" scope { repository_id = azuredevops_git_repository.example-import.id repository_ref = "refs/heads/test-branch" # 替换为目标非默认分支的完整引用 match_type = "Exact" } } }
关键说明
- Azure DevOps中分支的合法引用格式为
refs/heads/[分支名],直接填写分支名会导致匹配失败。 - 若需将策略应用到多个非默认分支,可添加多个独立的
scope块,每个块对应一个分支的配置。
内容的提问来源于stack exchange,提问作者Aleksandra
相关产品推荐
相关产品推荐

