forked from CloudPirates-io/helm-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-appversion.sh
More file actions
executable file
·360 lines (302 loc) · 11.6 KB
/
Copy pathupdate-appversion.sh
File metadata and controls
executable file
·360 lines (302 loc) · 11.6 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
# Update AppVersion script for Helm charts
# This script updates the appVersion in Chart.yaml with the image.tag from values.yaml
# It's designed to be triggered after generate-changelog.sh
set -eo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored messages
log_info() {
echo -e "${GREEN}[INFO]${NC} $1" || echo "[INFO] $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check required tools
check_dependencies() {
local missing_deps=()
if ! command -v yq &> /dev/null; then
missing_deps+=("yq")
fi
if [ ${#missing_deps[@]} -gt 0 ]; then
log_error "Missing required dependencies: ${missing_deps[*]}"
log_error "Please install yq: https://github.com/mikefarah/yq"
exit 1
fi
}
# Function to extract version from image tag
# This function handles various tag formats and ensures only semantic versions are returned:
# - "1.2.3" -> "1.2.3"
# - "1.2.3@sha256:..." -> "1.2.3"
# - "v1.2.3" -> "1.2.3"
# - "v1.2.3@sha256:..." -> "1.2.3"
# - "1.2.3-alpine" -> "1.2.3" (removes non-semver suffixes)
# - "1.2.3-alpha.1" -> "1.2.3-alpha.1" (keeps semver pre-release)
# - "RELEASE.2025-09-07T16-13-09Z" -> null (not semver)
# - "464e93ac" -> null (not semver)
extract_version_from_tag() {
local tag="$1"
# Remove quotes if present
tag=$(echo "$tag" | sed 's/^"//;s/"$//')
# Split by @ to remove digest if present
tag=$(echo "$tag" | cut -d'@' -f1)
# Remove 'v' prefix if present (but keep it for versions that start with v followed by number)
if [[ "$tag" =~ ^v[0-9] ]]; then
tag=$(echo "$tag" | sed 's/^v//')
fi
# Extract semantic version - prioritize core version (MAJOR.MINOR.PATCH)
# Only keep pre-release if it follows strict semver rules
# First, try to extract the core version (MAJOR.MINOR.PATCH or MAJOR.MINOR)
local core_version
core_version=$(echo "$tag" | grep -oE '^[0-9]+\.[0-9]+(\.[0-9]+)?' || echo "")
if [ -z "$core_version" ]; then
# No valid core version found
echo ""
return
fi
# If we only have MAJOR.MINOR, convert to MAJOR.MINOR.0 for strict semver compliance
if [[ "$core_version" =~ ^[0-9]+\.[0-9]+$ ]]; then
core_version="${core_version}.0"
fi
# Check if there's anything after the core version
local remaining_part
remaining_part=$(echo "$tag" | sed "s/^$(echo "$core_version" | sed 's/\.0$//')//")
if [ -z "$remaining_part" ]; then
# Just the core version, perfect
echo "$core_version"
return
fi
# Check if the remaining part is a valid semver pre-release/build
# Valid pre-release: -alpha, -alpha.1, -rc.1, -beta.2 (alphanumeric + hyphen, no leading zeros in numeric parts)
if [[ "$remaining_part" =~ ^-([a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$ ]]; then
local prerelease_part="${BASH_REMATCH[1]}"
# Validate each identifier in pre-release (no leading zeros in numeric identifiers)
local valid_prerelease=true
IFS='.' read -ra IDENTIFIERS <<< "$prerelease_part"
for identifier in "${IDENTIFIERS[@]}"; do
# Check if it's a numeric identifier with leading zero (invalid)
if [[ "$identifier" =~ ^0[0-9]+$ ]]; then
valid_prerelease=false
break
fi
# Check for mixed patterns like "alpine3", "management", "pg17" which are not valid semver
if [[ "$identifier" =~ [a-zA-Z][0-9] ]] || [[ "$identifier" =~ [0-9][a-zA-Z] ]]; then
valid_prerelease=false
break
fi
# Check for words like "management", "alpine" (common non-semver suffixes)
if [[ "$identifier" =~ ^(alpine|management|ubuntu|debian|slim|fat)$ ]]; then
valid_prerelease=false
break
fi
done
if [ "$valid_prerelease" = true ]; then
# Valid semver pre-release, keep the full version
echo "${core_version}${remaining_part}"
else
# Invalid pre-release, return just core version
echo "$core_version"
fi
else
# Not a valid semver pre-release format, return just core version
echo "$core_version"
fi
}
# Function to update appVersion in Chart.yaml
update_chart_appversion() {
local chart_name="$1"
local chart_dir="charts/${chart_name}"
local chart_yaml="${chart_dir}/Chart.yaml"
local values_yaml="${chart_dir}/values.yaml"
log_info "Processing chart: $chart_name"
# Validate chart directory exists
if [ ! -d "$chart_dir" ]; then
log_error "Chart directory not found: $chart_dir"
return 1
fi
# Validate Chart.yaml exists
if [ ! -f "$chart_yaml" ]; then
log_error "Chart.yaml not found: $chart_yaml"
return 1
fi
# Validate values.yaml exists
if [ ! -f "$values_yaml" ]; then
log_error "values.yaml not found: $values_yaml"
return 1
fi
# Extract current appVersion from Chart.yaml
local current_app_version
current_app_version=$(yq eval '.appVersion' "$chart_yaml" 2>/dev/null || echo "null")
if [ "$current_app_version" = "null" ]; then
log_warn "No appVersion found in $chart_yaml"
current_app_version="(not set)"
fi
# Extract image tag from values.yaml
# Try different possible paths for the image tag
local image_tag=""
# Try image.tag first (most common)
image_tag=$(yq eval '.image.tag' "$values_yaml" 2>/dev/null || echo "null")
# If not found, try other common patterns
if [ "$image_tag" = "null" ]; then
# Try under different sections that might contain image configurations
for path in \
'.*.image.tag' \
'.images.*.tag' \
'.global.image.tag' \
'.*.*.image.tag'; do
local temp_tag
temp_tag=$(yq eval "$path" "$values_yaml" 2>/dev/null | head -1 || echo "null")
if [ "$temp_tag" != "null" ] && [ -n "$temp_tag" ]; then
image_tag="$temp_tag"
log_info "Found image tag at path: $path"
break
fi
done
fi
if [ "$image_tag" = "null" ] || [ -z "$image_tag" ]; then
log_warn "No image tag found in $values_yaml for chart $chart_name"
log_warn "Skipping appVersion update"
return 0
fi
log_info "Found image tag: $image_tag"
# Extract version from the image tag
local new_app_version
new_app_version=$(extract_version_from_tag "$image_tag")
if [ -z "$new_app_version" ]; then
log_warn "Could not extract valid semantic version from image tag: $image_tag"
log_warn "Skipping appVersion update for chart $chart_name"
return 0
fi
log_info "Extracted semantic version: $new_app_version"
# Check if appVersion needs to be updated
if [ "$current_app_version" = "\"$new_app_version\"" ] || [ "$current_app_version" = "$new_app_version" ]; then
log_info "appVersion is already up to date ($new_app_version)"
return 0
fi
# Update appVersion in Chart.yaml
log_info "Updating appVersion from $current_app_version to $new_app_version"
# Use yq to update the appVersion
yq eval ".appVersion = \"$new_app_version\"" -i "$chart_yaml"
if [ $? -eq 0 ]; then
log_info "Successfully updated appVersion in $chart_yaml"
else
log_error "Failed to update appVersion in $chart_yaml"
return 1
fi
return 0
}
# Function to process all charts or specific charts
process_charts() {
local chart_names=("$@")
local success_count=0
local fail_count=0
local skip_count=0
for chart_name in "${chart_names[@]}"; do
# Skip the 'common' chart as it typically doesn't have an image
if [ "$chart_name" = "common" ]; then
log_info "Skipping 'common' chart (no image expected)"
skip_count=$((skip_count + 1))
continue
fi
if update_chart_appversion "$chart_name"; then
success_count=$((success_count + 1))
else
fail_count=$((fail_count + 1))
fi
done
# Summary
log_info "AppVersion update complete"
log_info "Success: $success_count, Failed: $fail_count, Skipped: $skip_count"
if [ $fail_count -gt 0 ]; then
return 1
fi
return 0
}
# Main script logic
main() {
log_info "Starting appVersion update"
# Check dependencies
check_dependencies
# Parse command line arguments
local chart_names=()
local update_all=false
while [[ $# -gt 0 ]]; do
case $1 in
--chart)
chart_names+=("$2")
shift 2
;;
--all)
update_all=true
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Description:"
echo " Updates the appVersion in Chart.yaml with the version extracted from image.tag in values.yaml"
echo " Designed to be run after generate-changelog.sh to keep appVersion in sync with image versions"
echo ""
echo "Options:"
echo " --chart CHART_NAME Update appVersion for specific chart (can be specified multiple times)"
echo " --all Update appVersion for all charts"
echo " --help, -h Show this help message"
echo ""
echo "Examples:"
echo " $0 --chart nginx --chart redis"
echo " $0 --all"
echo ""
echo "Version Extraction Logic:"
echo " - Removes SHA256 digest: '1.2.3@sha256:...' -> '1.2.3'"
echo " - Removes 'v' prefix: 'v1.2.3' -> '1.2.3'"
echo " - Extracts semantic version only: '1.2.3-alpine' -> '1.2.3'"
echo " - Preserves semver pre-release: '1.2.3-alpha.1' -> '1.2.3-alpha.1'"
echo " - Skips non-semver tags: 'RELEASE.2025-09-07' -> (skipped)"
echo " - Skips git hashes: '464e93ac' -> (skipped)"
echo ""
echo "Supported tag paths in values.yaml:"
echo " - image.tag (most common)"
echo " - *.image.tag"
echo " - images.*.tag"
echo " - global.image.tag"
exit 0
;;
*)
log_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# If --all flag is set, find all charts
if [ "$update_all" = true ]; then
log_info "Discovering all charts..."
while IFS= read -r chart_dir; do
local chart_name
chart_name=$(basename "$chart_dir")
chart_names+=("$chart_name")
done < <(find ./charts -mindepth 1 -maxdepth 1 -type d)
fi
# Validate we have charts to process
if [ ${#chart_names[@]} -eq 0 ]; then
log_error "No charts specified. Use --chart, --all, or --help"
exit 1
fi
log_info "Processing ${#chart_names[@]} chart(s): ${chart_names[*]}"
# Process charts
if process_charts "${chart_names[@]}"; then
log_info "All updates completed successfully"
exit 0
else
log_error "Some updates failed"
exit 1
fi
}
# Run main function
main "$@"