-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathupdate-chart-version.sh
executable file
·227 lines (207 loc) · 6.81 KB
/
update-chart-version.sh
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env bash
# Write output to error output stream.
echo_stderr() {
echo -e "${1}" >&2
}
die()
{
local _return="${2:-1}"
echo_stderr "$1"
exit "${_return}"
}
help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Options:
--dry-run Get the final version without modifying.
--branch <branch name> Name of the target branch.
--tag <tag> Release tag.
--type <release/develop> Which branch to modify.
--chart-version <version> Version of the current chart.
--publish-release To modify the charts for a release.
Examples:
$(basename "$0") --branch release/x.y
EOF
}
check_tag_is_valid() {
local tag="$1"
local current_chart_version="$2"
if [[ "$(semver validate $tag)" != "valid" ]]; then
die "Tag is not a valid sevmer complaint version"
fi
if [[ $current_chart_version != *"-prerelease" ]]; then
die "Chart version($current_chart_version) should be a prerelease format to proceed for tag creation flow"
fi
allowed_diff=("" "patch" "prerelease")
diff="$(semver diff "$tag" "$current_chart_version")"
if ! [[ " ${allowed_diff[*]} " =~ " $diff " ]]; then
die "For release/x.y branch the current chart version($current_chart_version)'s X.Y must exactly match X.Y from tag ($tag)"
fi
}
# yq-go eats up blank lines
# this function gets around that using diff with --ignore-blank-lines
yq_ibl()
{
set +e
diff_out=$(diff -B <(yq '.' "$2") <(yq "$1" "$2"))
error=$?
if [ "$error" != "0" ] && [ "$error" != "1" ]; then
exit "$error"
fi
if [ -n "$diff_out" ]; then
echo "$diff_out" | patch --quiet --no-backup-if-mismatch "$2" -
fi
set -euo pipefail
}
# RULES: This would run only when changes are pushed to a release/x.y branch.
# 1. Branch name can only be of format release/x.y
# 2. If current chart version of type develop(on branch creation),
# then version generated is of format x.y.0-prerelease if type is "release"
# 3. If current chart version of type develop(on branch creation),
# then version generated is of format x.y+1.0-develop if type is develop.
# 4. If current chart version of type prerelease(after branch creation),
# then for type release it's a no op as it's already a prerelease format.
# 5. If current chart version of type prerelease(after branch creation),
# then for type develop it's a no op as the version to be would be same as it is currently.
# 6. Let's say for somereason someone tries to create a release/x.y branch from develop but chart version
# on develop is newer than x.y, example, release/2.2 and develop chart version is 2.5.0-develop.
# In that case for type release, the version would be 2.2.0-prerelease and for type develop it would be
# a no op as develop is already newer.
create_version_from_release_branch() {
if [[ "$BRANCH_NAME" =~ ^(release/[0-9]+\.[0-9]+)$ ]]; then
local EXTRACTED_VERSION=$(echo "$BRANCH_NAME" | grep -oP '(?<=release/)\d+\.\d+')
if [[ "$TYPE" == "release" ]]; then
if [[ "$CURRENT_CHART_VERSION" == *"-develop" ]]; then
VERSION="${EXTRACTED_VERSION}.0-prerelease"
elif [[ "$CURRENT_CHART_VERSION" == *"-prerelease" ]]; then
NO_OP=1
else
die "Current chart version doesn't match a develop or prerel format"
fi
elif [[ "$TYPE" == "develop" ]]; then
EXPECTED_VERSION="$(semver bump minor "$EXTRACTED_VERSION.0")-develop"
if [[ "$(semver compare $EXPECTED_VERSION $CURRENT_CHART_VERSION)" == 1 ]]; then
VERSION=$EXPECTED_VERSION
else
NO_OP=1
fi
fi
else
die "Branch name($BRANCH_NAME) is not of format release/x.y"
fi
}
# RULES: This would run only when tag is created.
# 1. Tag should be of format vX.Y.Z.
# 2. If tag is of format vX.Y.Z-rc, it would be a no op for the workflow.
# 3. The tag can only be vX.Y.Z if the current chart version is X.Y*-prerelease. Ex, v2.6.1 for v2.6.*-prerelease
# 4. For release branches if all the above holds then it bumps the patch version. Ex, v2.6.1 --> 2.6.2-prerelease
create_version_from_tag() {
if [[ "$TAG" =~ ^(v[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
local EXTRACTED_VERSION=$(echo "$TAG" | grep -oP '(?<=v)\d+\.\d+.\d+')
check_tag_is_valid "$EXTRACTED_VERSION" "$CURRENT_CHART_VERSION"
if [[ -z $PUBLISH_RELEASE ]]; then
VERSION="$(semver bump patch $EXTRACTED_VERSION)-prerelease"
if [[ -z $DRY_RUN ]]; then
echo "release/$(echo $EXTRACTED_VERSION | cut -d'.' -f1,2)"
fi
else
VERSION="$EXTRACTED_VERSION"
fi
elif [[ "$TAG" == *"-rc" ]]; then
if [[ -z $PUBLISH_RELEASE ]]; then
NO_OP=1
else
local EXTRACTED_VERSION=$(echo "$TAG" | grep -oP '(?<=v)\d+\.\d+\.\d+(-\w+)?')
check_tag_is_valid "$EXTRACTED_VERSION" "$CURRENT_CHART_VERSION"
VERSION="$EXTRACTED_VERSION"
fi
else
die "Invalid tag format. Expected 'vX.Y.Z'"
fi
}
update_chart_yaml() {
local VERSION=$1
local APP_VERSION=$2
yq_ibl ".version = \"$VERSION\" | .appVersion = \"$APP_VERSION\"" "$CHART_YAML"
yq_ibl ".version = \"$VERSION\"" "$CRD_CHART_YAML"
yq_ibl "(.dependencies[] | select(.name == \"crds\") | .version) = \"$VERSION\"" "$CHART_YAML"
yq_ibl ".zfsPlugin.image.tag = \"$VERSION\"" "$VALUES_YAML"
}
set -euo pipefail
DRY_RUN=
NO_OP=
CURRENT_CHART_VERSION=
PUBLISH_RELEASE=
# Set the path to the Chart.yaml file
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]:-"$0"}")")"
ROOT_DIR="$SCRIPT_DIR/.."
CHART_DIR="$ROOT_DIR/deploy/helm/charts"
CHART_YAML="$CHART_DIR/Chart.yaml"
VALUES_YAML="$CHART_DIR/values.yaml"
CRD_CHART_NAME="crds"
CRD_CHART_YAML="$CHART_DIR/charts/$CRD_CHART_NAME/Chart.yaml"
# Final computed version to be set in this.
VERSION=""
# Parse arguments
while [ "$#" -gt 0 ]; do
case $1 in
-d|--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
help
exit 0
;;
-b|--branch)
shift
BRANCH_NAME=$1
shift
;;
-t|--tag)
shift
TAG=$1
shift
;;
--type)
shift
TYPE=$1
shift
;;
--chart-version)
shift
CURRENT_CHART_VERSION=$1
shift
;;
--publish-release)
PUBLISH_RELEASE=1
shift
;;
*)
help
die "Unknown option: $1"
;;
esac
done
if [[ -z $CURRENT_CHART_VERSION ]]; then
CURRENT_CHART_VERSION=$(yq e '.version' "$CHART_YAML")
fi
if [[ -n "${BRANCH_NAME-}" && -n "${TYPE-}" ]]; then
create_version_from_release_branch
elif [[ -n "${TAG-}" ]]; then
create_version_from_tag
else
help
die "Either --branch and --type or --tag and must be specified."
fi
if [[ -z $NO_OP ]]; then
if [[ -n $VERSION ]]; then
if [[ -z $DRY_RUN ]];then
update_chart_yaml "$VERSION" "$VERSION"
else
echo "$VERSION"
fi
else
die "Failed to update the chart versions"
fi
fi