|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# See README.adoc |
| 4 | +# |
| 5 | +set -euo pipefail |
| 6 | +# set -x |
| 7 | + |
| 8 | +REMOTE="origin" |
| 9 | +# Collects "<repo>|<title>|<url>" entries for every PR raised during the run, |
| 10 | +# so we can print a consolidated summary at the end. |
| 11 | +CREATED_PRS=() |
| 12 | +#---------------------------------------------------------------------------------------------------- |
| 13 | +# tags should be semver-compatible e.g. 23.1 and not 23.01 |
| 14 | +# this is needed for cargo commands to work properly: although it is not strictly needed |
| 15 | +# for the name of the release branch, the branch naming will be consistent with the cargo versioning. |
| 16 | +#---------------------------------------------------------------------------------------------------- |
| 17 | +RELEASE_REGEX="^[0-9][0-9]\.([1-9]|[1][0-2])$" |
| 18 | + |
| 19 | +update_products() { |
| 20 | + if [ -d "$BASE_DIR/$DOCKER_IMAGES_REPO" ]; then |
| 21 | + echo "Directory exists. Switching to ${RELEASE_BRANCH} branch and Updating..." |
| 22 | + cd "$BASE_DIR/$DOCKER_IMAGES_REPO" |
| 23 | + git switch "${RELEASE_BRANCH}" && git pull |
| 24 | + else |
| 25 | + echo "Repo directory ($BASE_DIR/$DOCKER_IMAGES_REPO) doesn't exist. Cloning and switching to ${RELEASE_BRANCH} branch" |
| 26 | + git clone --branch ${RELEASE_BRANCH} --depth 1 "git@github.com:stackabletech/${DOCKER_IMAGES_REPO}.git" "$BASE_DIR/$DOCKER_IMAGES_REPO" |
| 27 | + cd "$BASE_DIR/$DOCKER_IMAGES_REPO" |
| 28 | + # try to switch to the release branch (if continuing from someone else), or create it |
| 29 | + git switch "${RELEASE_BRANCH}" 2> /dev/null || git switch -c "${RELEASE_BRANCH}" |
| 30 | + fi |
| 31 | + |
| 32 | + # Create the work branch from the release branch so changes go through a PR |
| 33 | + # rather than being pushed directly to the release branch. |
| 34 | + git switch -c "${WORK_BRANCH}" |
| 35 | + |
| 36 | + echo "Pls manually bump the UBI base images in $BASE_DIR/$DOCKER_IMAGES_REPO" |
| 37 | + echo 'Tip: I found the following images when searching for "registry.access.redhat.com/ubi" in Dockerfiles:' |
| 38 | + grep -r 'FROM registry.access.redhat.com/ubi' **/Dockerfile |
| 39 | + |
| 40 | + read -r -p "Press Enter once you have updated the UBI base images (or Ctrl+C to abort)... " |
| 41 | + |
| 42 | + # Pick up any edits the user made. Skip if they already committed themselves |
| 43 | + # or if they made no changes at all. |
| 44 | + if ! git diff --quiet || ! git diff --cached --quiet; then |
| 45 | + echo "Staging and committing UBI base image bumps..." |
| 46 | + git add --update |
| 47 | + git commit -m "chore: UBI base image bumps" |
| 48 | + fi |
| 49 | + |
| 50 | + raise_pr "$DOCKER_IMAGES_REPO" "chore: UBI base image bumps" |
| 51 | + |
| 52 | + echo |
| 53 | + echo "Check $BASE_DIR/$DOCKER_IMAGES_REPO" |
| 54 | +} |
| 55 | + |
| 56 | +update_operators() { |
| 57 | + while IFS="" read -r operator || [ -n "$operator" ] |
| 58 | + do |
| 59 | + echo ">>> Now working on ${operator}" |
| 60 | + |
| 61 | + if [ -d "$BASE_DIR/${operator}" ]; then |
| 62 | + echo "Directory exists. Switching to ${RELEASE_BRANCH} branch and Updating..." |
| 63 | + cd "$BASE_DIR/${operator}" |
| 64 | + git switch "${RELEASE_BRANCH}" && git pull |
| 65 | + else |
| 66 | + echo "Repo directory ($BASE_DIR/$operator) doesn't exist. Cloning and switching to ${RELEASE_BRANCH} branch" |
| 67 | + git clone --branch ${RELEASE_BRANCH} --depth 1 "git@github.com:stackabletech/${operator}.git" "$BASE_DIR/${operator}" |
| 68 | + cd "$BASE_DIR/${operator}" |
| 69 | + # try to switch to the release branch (if continuing from someone else), or create it |
| 70 | + git switch "${RELEASE_BRANCH}" || git switch -c "${RELEASE_BRANCH}" |
| 71 | + fi |
| 72 | + |
| 73 | + # Create the work branch from the release branch so changes go through a PR |
| 74 | + # rather than being pushed directly to the release branch. |
| 75 | + git switch -c "${WORK_BRANCH}" |
| 76 | + |
| 77 | + cargo update |
| 78 | + # cargo test # Will be done by CI and takes too long |
| 79 | + make regenerate-nix |
| 80 | + # We are explicitly not regenerating the CRDs, as we don't want CRD changes to sneak in. |
| 81 | + # We rather let the CI checks fail and inspect manually. |
| 82 | + git add Cargo.lock Cargo.nix |
| 83 | + git commit -m "chore: Rust dependency patch level updates" |
| 84 | + |
| 85 | + echo "FYI, these are the major/minor bumps we did't do:" |
| 86 | + cargo +nightly -Z unstable-options update --breaking --dry-run |
| 87 | + |
| 88 | + raise_pr "$operator" "chore: Rust dependency patch level updates" |
| 89 | + done < <(yq '... comments="" | .operators[] ' "$INITIAL_DIR"/release/config.yaml) |
| 90 | +} |
| 91 | + |
| 92 | +update_repos() { |
| 93 | + local BASE_DIR="$1"; |
| 94 | + |
| 95 | + if [ "products" == "$WHAT" ] || [ "all" == "$WHAT" ]; then |
| 96 | + update_products |
| 97 | + fi |
| 98 | + if [ "operators" == "$WHAT" ] || [ "all" == "$WHAT" ]; then |
| 99 | + update_operators |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +raise_pr() { |
| 104 | + local REPOSITORY="$1"; |
| 105 | + local PR_TITLE="$2"; |
| 106 | + |
| 107 | + # Skip when nothing was committed on the work branch (e.g. products flow without manual edits) |
| 108 | + local COMMITS_AHEAD |
| 109 | + COMMITS_AHEAD=$(git rev-list --count "${RELEASE_BRANCH}..${WORK_BRANCH}") |
| 110 | + if [ "$COMMITS_AHEAD" -eq 0 ]; then |
| 111 | + echo "No commits on ${WORK_BRANCH} relative to ${RELEASE_BRANCH} for ${REPOSITORY}, skipping push and PR creation" |
| 112 | + return |
| 113 | + fi |
| 114 | + |
| 115 | + if $PUSH; then |
| 116 | + echo "Pushing ${WORK_BRANCH} to ${REPOSITORY}" |
| 117 | + git push -u "$REMOTE" "$WORK_BRANCH" |
| 118 | + echo "Creating PR ${WORK_BRANCH} -> ${RELEASE_BRANCH} on ${REPOSITORY}" |
| 119 | + local PR_URL |
| 120 | + PR_URL=$(gh pr create \ |
| 121 | + --base "$RELEASE_BRANCH" \ |
| 122 | + --head "$WORK_BRANCH" \ |
| 123 | + --title "$PR_TITLE" \ |
| 124 | + --body "Patch level maintenance updates for \`${RELEASE_BRANCH}\` (created by release-branch-hygiene.sh).") |
| 125 | + CREATED_PRS+=("${REPOSITORY}|${PR_TITLE}|${PR_URL}") |
| 126 | + else |
| 127 | + echo "Dry-run: not pushing ${WORK_BRANCH} or creating PR for ${REPOSITORY}" |
| 128 | + git push --dry-run -u "$REMOTE" "$WORK_BRANCH" |
| 129 | + fi |
| 130 | +} |
| 131 | + |
| 132 | +print_summary() { |
| 133 | + echo |
| 134 | + echo "================ PR Summary ================" |
| 135 | + if [ ${#CREATED_PRS[@]} -eq 0 ]; then |
| 136 | + if $PUSH; then |
| 137 | + echo "No PRs were created." |
| 138 | + else |
| 139 | + echo "Dry-run: no PRs were created (re-run with --push to actually open PRs)." |
| 140 | + fi |
| 141 | + return |
| 142 | + fi |
| 143 | + echo "Created ${#CREATED_PRS[@]} PR(s):" |
| 144 | + for entry in "${CREATED_PRS[@]}"; do |
| 145 | + IFS='|' read -r repo title url <<<"$entry" |
| 146 | + echo " - ${repo}: ${title}" |
| 147 | + echo " ${url}" |
| 148 | + done |
| 149 | +} |
| 150 | + |
| 151 | +cleanup() { |
| 152 | + local BASE_DIR="$1"; |
| 153 | + |
| 154 | + if $CLEANUP; then |
| 155 | + echo "Cleaning up..." |
| 156 | + rm -rf "$BASE_DIR" |
| 157 | + fi |
| 158 | +} |
| 159 | + |
| 160 | +parse_inputs() { |
| 161 | + RELEASE="" |
| 162 | + PUSH=false |
| 163 | + CLEANUP=false |
| 164 | + WHAT="" |
| 165 | + |
| 166 | + while [[ "$#" -gt 0 ]]; do |
| 167 | + case $1 in |
| 168 | + -b|--branch) RELEASE="$2"; shift ;; |
| 169 | + -w|--what) WHAT="$2"; shift ;; |
| 170 | + -p|--push) PUSH=true ;; |
| 171 | + -c|--cleanup) CLEANUP=true ;; |
| 172 | + *) echo "Unknown parameter passed: $1"; exit 1 ;; |
| 173 | + esac |
| 174 | + shift |
| 175 | + done |
| 176 | + #----------------------------------------------------------- |
| 177 | + # remove leading and trailing quotes |
| 178 | + #----------------------------------------------------------- |
| 179 | + RELEASE="${RELEASE%\"}" |
| 180 | + RELEASE="${RELEASE#\"}" |
| 181 | + RELEASE_BRANCH="release-$RELEASE" |
| 182 | + # A single timestamp is shared across all repos in this run so the work |
| 183 | + # branches are easy to correlate. |
| 184 | + WORK_BRANCH="${RELEASE_BRANCH}-maintenance-$(date +%s)" |
| 185 | + |
| 186 | + INITIAL_DIR="$PWD" |
| 187 | + DOCKER_IMAGES_REPO=$(yq '... comments="" | .images-repo ' "$INITIAL_DIR"/release/config.yaml) |
| 188 | + TEMP_RELEASE_FOLDER="/tmp/stackable-$RELEASE_BRANCH" |
| 189 | + |
| 190 | + echo "Settings: ${RELEASE_BRANCH}: Work branch: ${WORK_BRANCH}: Push: $PUSH: Cleanup: $CLEANUP" |
| 191 | +} |
| 192 | + |
| 193 | +main() { |
| 194 | + parse_inputs "$@" |
| 195 | + #----------------------------------------------------------- |
| 196 | + # check if tag argument provided |
| 197 | + #----------------------------------------------------------- |
| 198 | + if [ -z "${RELEASE}" ]; then |
| 199 | + echo "Usage: create-release-branch.sh -b <branch> [-p] [-c] [-w products|operators|demos|all]" |
| 200 | + exit 1 |
| 201 | + fi |
| 202 | + #----------------------------------------------------------- |
| 203 | + # check if argument matches our tag regex |
| 204 | + #----------------------------------------------------------- |
| 205 | + if [[ ! $RELEASE =~ $RELEASE_REGEX ]]; then |
| 206 | + echo "Provided branch name [$RELEASE] does not match the required regex pattern [$RELEASE_REGEX]" |
| 207 | + exit 1 |
| 208 | + fi |
| 209 | + |
| 210 | + echo "Creating temporary working directory if it doesn't exist [$TEMP_RELEASE_FOLDER]" |
| 211 | + mkdir -p "$TEMP_RELEASE_FOLDER" |
| 212 | + update_repos "$TEMP_RELEASE_FOLDER" |
| 213 | + cleanup "$TEMP_RELEASE_FOLDER" |
| 214 | + print_summary |
| 215 | +} |
| 216 | + |
| 217 | +main "$@" |
0 commit comments