From 6afd464ea467d5dfdbbe46767035858527512e1f Mon Sep 17 00:00:00 2001 From: Lennard Golsch Date: Wed, 2 Dec 2026 14:31:09 +0100 Subject: [PATCH 1/3] feat: set up only the default branch when it is the only --branch --- docs/setup-environment.md | 4 ++++ scripts/setup-environment.sh | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/setup-environment.md b/docs/setup-environment.md index 4d4cd52..b8bfe84 100644 --- a/docs/setup-environment.md +++ b/docs/setup-environment.md @@ -44,6 +44,10 @@ actors are added, the expected bypass actors that may bypass it only for pull re to active, everything else stays. Deployment branches of other patterns stay as well and are reported as warnings. +The name of the default branch given to `--branch` is no version branch. For a repository without version branches, +`--branch main` alone sets up only `main` as deployment branch and no ruleset "Version branches", as there is no branch +to protect. Run the script again without it once the first version branch exists. + The script can be run again at any time, it only changes what is missing. ## What it only reports diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index abb5d8e..e4ff14c 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -14,6 +14,8 @@ Options: --check Only report what is missing or differs, change nothing. Exits with 1 if anything is missing. --app Slug of the GitHub App that pushes releases, bypass actor of the rulesets. Default: mycore-release --branch Pattern of the version branches, repeatable. Default: [0-9][0-9][0-9][0-9].[0-9][0-9].x + The name of the default branch is no version branch: --branch main alone sets up only main, + for repositories without version branches. --docker Also the Docker Hub login, DOCKERHUB_USERNAME and DOCKERHUB_TOKEN. --reset-secrets Ask for all secrets, also the existing ones. -h, --help Show this help. @@ -124,6 +126,7 @@ env_path="repos/$repo/environments/$environment" version_branches=() for pattern in "${branches[@]}"; do + [ "$pattern" != "$default_branch" ] || continue contains "$pattern" "${version_branches[@]}" || version_branches+=("$pattern") done deployment_patterns=("$default_branch") @@ -269,9 +272,13 @@ ruleset() { } echo "Rulesets" -branch_include="$(printf '%s\n' "${version_branches[@]}" | jq -R '"refs/heads/" + .' | jq -s .)" -ruleset "Version branches" branch "$branch_include" '["creation"]' \ - "$(jq -n --argjson a "$bypass" --argjson b "$app_bypass" '$a + $b')" +if [ ${#version_branches[@]} -gt 0 ]; then + branch_include="$(printf '%s\n' "${version_branches[@]}" | jq -R '"refs/heads/" + .' | jq -s .)" + ruleset "Version branches" branch "$branch_include" '["creation"]' \ + "$(jq -n --argjson a "$bypass" --argjson b "$app_bypass" '$a + $b')" +else + note "no version branches, no ruleset \"Version branches\" needed" +fi if [ "$environment" = release ]; then ruleset "Release tags" tag '["refs/tags/v*"]' '["creation","update","deletion"]' "$app_bypass" fi From 97ab061e47925560d414ad3ffa9e64a598dd5bb3 Mon Sep 17 00:00:00 2001 From: Lennard Golsch Date: Wed, 2 Dec 2026 14:34:50 +0100 Subject: [PATCH 2/3] feat: show the version branches and keep an existing ruleset without them --- scripts/setup-environment.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index e4ff14c..39049ec 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -135,6 +135,7 @@ for pattern in "${version_branches[@]}"; do done echo "$repo, environment $environment" +echo " default branch $default_branch, version branches: ${version_branches[*]:-none}" app_id='' if [ "$environment" = release ]; then @@ -277,7 +278,13 @@ if [ ${#version_branches[@]} -gt 0 ]; then ruleset "Version branches" branch "$branch_include" '["creation"]' \ "$(jq -n --argjson a "$bypass" --argjson b "$app_bypass" '$a + $b')" else - note "no version branches, no ruleset \"Version branches\" needed" + existing="$(gh api "repos/$repo/rulesets?includes_parents=false" --paginate \ + | jq -r '.[] | select(.name == "Version branches" and .target == "branch") | .id')" + if [ -n "$existing" ]; then + note "no version branches, ruleset \"Version branches\" exists and stays unchanged" + else + note "no version branches, no ruleset \"Version branches\" needed" + fi fi if [ "$environment" = release ]; then ruleset "Release tags" tag '["refs/tags/v*"]' '["creation","update","deletion"]' "$app_bypass" From e4d3ceae75849bdf18cbf80844bc2ff67ecb5180 Mon Sep 17 00:00:00 2001 From: Lennard Golsch Date: Wed, 2 Dec 2026 14:36:12 +0100 Subject: [PATCH 3/3] refactor: look up rulesets in one place, document the version branch output --- docs/setup-environment.md | 4 +++- scripts/setup-environment.sh | 13 ++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/setup-environment.md b/docs/setup-environment.md index b8bfe84..cc4239d 100644 --- a/docs/setup-environment.md +++ b/docs/setup-environment.md @@ -46,7 +46,9 @@ and are reported as warnings. The name of the default branch given to `--branch` is no version branch. For a repository without version branches, `--branch main` alone sets up only `main` as deployment branch and no ruleset "Version branches", as there is no branch -to protect. Run the script again without it once the first version branch exists. +to protect. Run the script again without it once the first version branch exists. An existing ruleset +"Version branches" stays unchanged and is pointed out, an existing deployment branch of a version branch pattern is +reported as warning. The output starts with the default branch and the version branches the script uses. The script can be run again at any time, it only changes what is missing. diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index 39049ec..ae6af59 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -221,6 +221,11 @@ if [ -n "$app_id" ]; then app_bypass="[{\"actor_id\":${app_id},\"actor_type\":\"Integration\",\"bypass_mode\":\"always\"}]" fi +ruleset_ids() { + gh api "repos/$repo/rulesets?includes_parents=false" --paginate \ + | jq -r --arg name "$1" --arg target "$2" '.[] | select(.name == $name and .target == $target) | .id' +} + ruleset() { local name="$1" target="$2" include="$3" rules="$4" actors="$5" ids id current wanted merged differences wanted="$(jq -n --arg name "$name" --arg target "$target" --argjson include "$include" \ @@ -228,8 +233,7 @@ ruleset() { '{name: $name, target: $target, enforcement: "active", conditions: {ref_name: {include: $include, exclude: []}}, rules: ($rules | map({type: .})), bypass_actors: $actors}')" - ids="$(gh api "repos/$repo/rulesets?includes_parents=false" --paginate \ - | jq -r --arg name "$name" --arg target "$target" '.[] | select(.name == $name and .target == $target) | .id')" + ids="$(ruleset_ids "$name" "$target")" if [ -z "$ids" ]; then found "ruleset \"$name\"" [ "$check" = true ] || gh api -X POST "repos/$repo/rulesets" --silent --input - <<< "$wanted" @@ -278,9 +282,8 @@ if [ ${#version_branches[@]} -gt 0 ]; then ruleset "Version branches" branch "$branch_include" '["creation"]' \ "$(jq -n --argjson a "$bypass" --argjson b "$app_bypass" '$a + $b')" else - existing="$(gh api "repos/$repo/rulesets?includes_parents=false" --paginate \ - | jq -r '.[] | select(.name == "Version branches" and .target == "branch") | .id')" - if [ -n "$existing" ]; then + version_rulesets="$(ruleset_ids "Version branches" branch)" + if [ -n "$version_rulesets" ]; then note "no version branches, ruleset \"Version branches\" exists and stays unchanged" else note "no version branches, no ruleset \"Version branches\" needed"