diff --git a/docs/setup-environment.md b/docs/setup-environment.md index 4d4cd52..cc4239d 100644 --- a/docs/setup-environment.md +++ b/docs/setup-environment.md @@ -44,6 +44,12 @@ 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. 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. ## What it only reports diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index abb5d8e..ae6af59 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") @@ -132,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 @@ -217,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" \ @@ -224,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" @@ -269,9 +277,18 @@ 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 + 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" + fi +fi if [ "$environment" = release ]; then ruleset "Release tags" tag '["refs/tags/v*"]' '["creation","update","deletion"]' "$app_bypass" fi