| 
 | 1 | +#!/usr/bin/env bash  | 
 | 2 | +set -euo pipefail  | 
 | 3 | + | 
 | 4 | +# This script is used by the stackable-utils release script to update the demos  | 
 | 5 | +# repository branch references as well as the stackableRelease versions so that  | 
 | 6 | +# demos are properly versioned.  | 
 | 7 | + | 
 | 8 | +# Parse args:  | 
 | 9 | +# $1 if `commit` is specified as the first argument, then changes will be staged and committed.  | 
 | 10 | +COMMIT="${1:-false}"  | 
 | 11 | +COMMIT="${COMMIT/commit/true}"  | 
 | 12 | + | 
 | 13 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)  | 
 | 14 | + | 
 | 15 | +# Ensure we are not on the `main` branch.  | 
 | 16 | +if [[ "$CURRENT_BRANCH" == "main" ]]; then  | 
 | 17 | +  >&2 echo "Will not replace github references for the main branch. Exiting."  | 
 | 18 | +  exit 1  | 
 | 19 | +fi  | 
 | 20 | + | 
 | 21 | +# Ensure the index is clean  | 
 | 22 | +if ! git diff-index --quiet HEAD --; then  | 
 | 23 | +  >&2 echo "Dirty git index. Check working tree or staged changes. Exiting."  | 
 | 24 | +  exit 2  | 
 | 25 | +fi  | 
 | 26 | + | 
 | 27 | +# prepend a string to each line of stdout  | 
 | 28 | +function prepend {  | 
 | 29 | +  while read -r line; do  | 
 | 30 | +    echo -e "${1}${line}"  | 
 | 31 | +  done  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +# stage and commit based on a message  | 
 | 35 | +function maybe_commit {  | 
 | 36 | +  [ "$COMMIT" == "true" ] || return 0  | 
 | 37 | +  local MESSAGE="$1"  | 
 | 38 | +  PATCH=$(mktemp)  | 
 | 39 | +  git add -u  | 
 | 40 | +  git diff --staged > "$PATCH"  | 
 | 41 | +  git commit -S -m "$MESSAGE" --no-verify  | 
 | 42 | +  echo "patch written to: $PATCH" | prepend "\t"  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +if [[ "$CURRENT_BRANCH" == release-* ]]; then  | 
 | 46 | +  STACKABLE_RELEASE="${CURRENT_BRANCH#release-}"  | 
 | 47 | +  MESSAGE="Update stackableRelease to $STACKABLE_RELEASE"  | 
 | 48 | +  echo "$MESSAGE"  | 
 | 49 | +  # NOTE (@NickLarsenNZ): find is not required for such a trivial case, but it is done for consitency  | 
 | 50 | +  find stacks/stacks-v2.yaml \  | 
 | 51 | +    -exec grep --color=always -l stackableRelease {} \; \  | 
 | 52 | +    -exec sed -i -E "s|(stackableRelease:\s+)(\S+)|\1${STACKABLE_RELEASE}|" {} \; \  | 
 | 53 | +  | prepend "\t"  | 
 | 54 | +  maybe_commit "chore(release): $MESSAGE"  | 
 | 55 | + | 
 | 56 | +  # Replace 0.0.0-dev refs with ${STACKABLE_RELEASE}.0  | 
 | 57 | +  # TODO (@NickLarsenNZ): handle patches later, and what about release-candidates?  | 
 | 58 | +  SEARCH='stackable(0\.0\.0-dev|24\.7\.[0-9]+)' # TODO (@NickLarsenNZ): After https://github.com/stackabletech/stackable-cockpit/issues/310, only search for 0.0.0-dev  | 
 | 59 | +  REPLACEMENT="stackable${STACKABLE_RELEASE}.0" # TODO (@NickLarsenNZ): Be a bit smarter about patch releases.  | 
 | 60 | +  MESSAGE="Update image references with $REPLACEMENT"  | 
 | 61 | +  echo "$MESSAGE"  | 
 | 62 | +  find demos stacks -type f \  | 
 | 63 | +    -exec grep --color=always -lE "$SEARCH" {} \; \  | 
 | 64 | +    -exec sed -i -E "s|${SEARCH}|${REPLACEMENT}|" {} \; \  | 
 | 65 | +  | prepend "\t"  | 
 | 66 | +  maybe_commit "chore(release): $MESSAGE"  | 
 | 67 | + | 
 | 68 | +  # Look for remaining references  | 
 | 69 | +  echo "Checking files with older stackable release references which will be assumed to be intentional."  | 
 | 70 | +  grep --color=always -ronE "stackable24\.3(\.[0-9]+)" | prepend "\t"  | 
 | 71 | +  echo  | 
 | 72 | +else  | 
 | 73 | +  >&2 echo "WARNING: doesn't look like a release branch. Will not update stackableRelease versions in stacks and image references."  | 
 | 74 | +fi  | 
 | 75 | + | 
 | 76 | +MESSAGE="Replace githubusercontent references main->${CURRENT_BRANCH}"  | 
 | 77 | +echo "$MESSAGE"  | 
 | 78 | +# Search for githubusercontent urls and replace the branch reference with a placeholder variable  | 
 | 79 | +# This is done just in case the branch has special regex characters (like `/`).  | 
 | 80 | +# shellcheck disable=SC2016 # We intentionally don't want to expand the variable.  | 
 | 81 | +find demos stacks -type f \  | 
 | 82 | +  -exec grep --color=always -l githubusercontent {} \; \  | 
 | 83 | +  -exec sed -i -E 's|(stackabletech/demos)/main/|\1/\${UPDATE_BRANCH_REF}/|' {} \; \  | 
 | 84 | +| prepend "\t"  | 
 | 85 | + | 
 | 86 | +# Now, for all modified files, we can use envsubst  | 
 | 87 | +export UPDATE_BRANCH_REF="$CURRENT_BRANCH"  | 
 | 88 | +for MODIFIED_FILE in $(git diff --name-only); do  | 
 | 89 | +  # shellcheck disable=SC2016 # We intentionally don't want to expand the variable.  | 
 | 90 | +  envsubst '$UPDATE_BRANCH_REF' < "$MODIFIED_FILE" > "$MODIFIED_FILE.replacements"  | 
 | 91 | +  mv "$MODIFIED_FILE.replacements" "$MODIFIED_FILE"  | 
 | 92 | +done  | 
 | 93 | +maybe_commit "chore(release): $MESSAGE"  | 
0 commit comments