generate tag-protection ruleset for release tags
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/plan Pipeline was successful

This commit is contained in:
2026-11-10 21:56:48 +11:00
parent 132e93c0d4
commit d7359d1fc7
4 changed files with 83 additions and 8 deletions
+3 -2
View File
@@ -26,11 +26,12 @@ codeowners: [] # push access, for adding to CODEOWNERS later
merge_type: squash # squash | merge | rebase
delete_branch_on_merge: true
branch_protection: !private # generated `branch-protection` ruleset on the default branch
tag_protection: !private # generated `tag-protection` ruleset: refs/tags/v* undeletable, immutable
required_checks: [] # generated `required-checks` ruleset (strict)
rulesets: [] # custom rulesets from ruleset/
```
`ruleset/<name>.yaml` (custom extras; `branch-protection` and `required-checks` are reserved):
`ruleset/<name>.yaml` (custom extras; `branch-protection`, `required-checks` and `tag-protection` are reserved):
```yaml
target: branch
@@ -44,4 +45,4 @@ rules:
non_fast_forward: true
```
Private repositories cannot use `branch_protection`, `required_checks` or `rulesets` on the free plan; plan fails if they do.
Private repositories cannot use `branch_protection`, `tag_protection`, `required_checks` or `rulesets` on the free plan; plan fails if they do.
+17
View File
@@ -58,6 +58,23 @@ locals {
}
})
} if length(r.required_checks) > 0
},
{
for name, r in var.repository : "${name}/tag-protection" => {
repository = name
ruleset = {
name = "tag-protection"
target = "tag"
enforcement = "active"
conditions = { ref_name = { include = ["refs/tags/v*"], exclude = [] } }
rules = {
deletion = true
non_fast_forward = true
pull_request = null
required_status_checks = null
}
}
} if coalesce(r.tag_protection, !r.private)
}],
)...)
}
+55 -4
View File
@@ -34,8 +34,19 @@ run "public_repo_defaults" {
}
assert {
condition = keys(github_repository_ruleset.this) == [".github/branch-protection"] && length(github_team_membership.this) == 2
error_message = "public repo must get only the generated branch-protection ruleset"
condition = keys(github_repository_ruleset.this) == [".github/branch-protection", ".github/tag-protection"] && length(github_team_membership.this) == 2
error_message = "public repo must get only the generated branch-protection and tag-protection rulesets"
}
assert {
condition = (
github_repository_ruleset.this[".github/tag-protection"].target == "tag" &&
github_repository_ruleset.this[".github/tag-protection"].enforcement == "active" &&
github_repository_ruleset.this[".github/tag-protection"].conditions[0].ref_name[0].include == tolist(["refs/tags/v*"]) &&
github_repository_ruleset.this[".github/tag-protection"].rules[0].deletion &&
github_repository_ruleset.this[".github/tag-protection"].rules[0].non_fast_forward
)
error_message = "tag-protection ruleset must make refs/tags/v* undeletable and immutable"
}
assert {
@@ -99,13 +110,13 @@ run "generated_and_custom_rulesets" {
variables {
repository = {
checked = { name = "checked", private = false, branch_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] }
checked = { name = "checked", private = false, branch_protection = false, tag_protection = false, required_checks = ["ci/woodpecker"], rulesets = ["signed"] }
}
}
assert {
condition = toset(keys(github_repository_ruleset.this)) == toset(["checked/required-checks", "checked/signed"])
error_message = "required_checks without branch_protection yields only required-checks plus custom rulesets"
error_message = "required_checks with protections off yields only required-checks plus custom rulesets"
}
assert {
@@ -131,6 +142,46 @@ run "private_branch_protection_rejected" {
expect_failures = [var.repository]
}
run "public_tag_protection_only" {
command = plan
variables {
repository = {
tags = { name = "tags", private = false, branch_protection = false }
}
}
assert {
condition = keys(github_repository_ruleset.this) == ["tags/tag-protection"]
error_message = "tag_protection defaults on for public repos independently of branch_protection"
}
}
run "private_tag_protection_rejected" {
command = plan
variables {
repository = {
secret = { name = "secret", tag_protection = true }
}
}
expect_failures = [var.repository]
}
run "reserved_tag_protection_name_rejected" {
command = plan
variables {
ruleset = {
tag-protection = { name = "tag-protection", target = "tag", rules = { deletion = true } }
}
repository = {}
}
expect_failures = [var.ruleset]
}
run "private_required_checks_rejected" {
command = plan
+8 -2
View File
@@ -46,8 +46,8 @@ variable "ruleset" {
default = {}
validation {
condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks"])) == 0
error_message = "Ruleset names branch-protection and required-checks are reserved for generated rulesets."
condition = length(setintersection(keys(var.ruleset), ["branch-protection", "required-checks", "tag-protection"])) == 0
error_message = "Ruleset names branch-protection, required-checks and tag-protection are reserved for generated rulesets."
}
}
@@ -63,6 +63,7 @@ variable "repository" {
merge_type = optional(string, "squash")
delete_branch_on_merge = optional(bool, true)
branch_protection = optional(bool)
tag_protection = optional(bool)
required_checks = optional(list(string), [])
rulesets = optional(list(string), [])
}))
@@ -78,6 +79,11 @@ variable "repository" {
error_message = "branch_protection is not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && coalesce(repo.branch_protection, false)])}."
}
validation {
condition = alltrue([for repo in var.repository : !(repo.private && coalesce(repo.tag_protection, false))])
error_message = "tag_protection is not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && coalesce(repo.tag_protection, false)])}."
}
validation {
condition = alltrue([for repo in var.repository : !(repo.private && length(repo.required_checks) > 0)])
error_message = "required_checks are not enforced on private repositories on the GitHub free plan: ${join(", ", [for name, repo in var.repository : name if repo.private && length(repo.required_checks) > 0])}."