Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/setup-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions scripts/setup-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Options:
--check Only report what is missing or differs, change nothing. Exits with 1 if anything is missing.
--app <slug> Slug of the GitHub App that pushes releases, bypass actor of the rulesets. Default: mycore-release
--branch <pattern> 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.
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -217,15 +221,19 @@ 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" \
--argjson rules "$rules" --argjson actors "$actors" \
'{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"
Expand Down Expand Up @@ -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
Expand Down