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