|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to bump Helm chart version and appVersion in Chart.yaml |
| 4 | +# Usage: ./bump-helm-chart.sh <version> |
| 5 | +# Examples: ./bump-helm-chart.sh v1.2.3 or ./bump-helm-chart.sh 1.2.3 |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +# Colors for output |
| 10 | +RED='\033[0;31m' |
| 11 | +GREEN='\033[0;32m' |
| 12 | +YELLOW='\033[1;33m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +# Function to print colored output |
| 16 | +print_error() { |
| 17 | + echo -e "${RED}Error: $1${NC}" >&2 |
| 18 | +} |
| 19 | + |
| 20 | +print_success() { |
| 21 | + echo -e "${GREEN}$1${NC}" |
| 22 | +} |
| 23 | + |
| 24 | +print_warning() { |
| 25 | + echo -e "${YELLOW}$1${NC}" |
| 26 | +} |
| 27 | + |
| 28 | +# Function to show usage |
| 29 | +show_usage() { |
| 30 | + echo "Usage: $0 <version>" |
| 31 | + echo "" |
| 32 | + echo "Examples:" |
| 33 | + echo " $0 v1.2.3" |
| 34 | + echo " $0 1.2.3" |
| 35 | + echo "" |
| 36 | + echo "The script will update both 'version' and 'appVersion' in Chart.yaml" |
| 37 | + echo " - version: semver without 'v' prefix (e.g., 1.2.3)" |
| 38 | + echo " - appVersion: string with 'v' prefix (e.g., \"v1.2.3\")" |
| 39 | +} |
| 40 | + |
| 41 | +# Function to validate semver format |
| 42 | +validate_semver() { |
| 43 | + local version="$1" |
| 44 | + # Check if version matches semver pattern (major.minor.patch with optional pre-release and build) |
| 45 | + if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$ ]]; then |
| 46 | + return 1 |
| 47 | + fi |
| 48 | + return 0 |
| 49 | +} |
| 50 | + |
| 51 | +# Function to parse version input |
| 52 | +parse_version() { |
| 53 | + local input_version="$1" |
| 54 | + local version |
| 55 | + |
| 56 | + # Remove 'v' prefix if present |
| 57 | + if [[ "$input_version" =~ ^v(.+)$ ]]; then |
| 58 | + version="${BASH_REMATCH[1]}" |
| 59 | + else |
| 60 | + version="$input_version" |
| 61 | + fi |
| 62 | + |
| 63 | + # Validate the version format |
| 64 | + if ! validate_semver "$version"; then |
| 65 | + print_error "Invalid version format: '$input_version'. Expected format: v1.2.3 or 1.2.3" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + |
| 69 | + echo "$version" |
| 70 | +} |
| 71 | + |
| 72 | +# Function to update Chart.yaml |
| 73 | +update_chart_yaml() { |
| 74 | + local chart_file="$1" |
| 75 | + local version="$2" |
| 76 | + local app_version="v$version" |
| 77 | + |
| 78 | + # Check if Chart.yaml exists |
| 79 | + if [[ ! -f "$chart_file" ]]; then |
| 80 | + print_error "Chart.yaml not found at: $chart_file" |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + |
| 84 | + # Update version and appVersion using sed |
| 85 | + # Update version (semver without v) |
| 86 | + sed -i.tmp "s/^version: .*$/version: $version/" "$chart_file" |
| 87 | + |
| 88 | + # Update appVersion (string with v) |
| 89 | + sed -i.tmp "s/^appVersion: .*$/appVersion: \"$app_version\"/" "$chart_file" |
| 90 | + |
| 91 | + # Remove temporary file created by sed |
| 92 | + rm -f "${chart_file}.tmp" |
| 93 | + |
| 94 | + print_success "Updated Chart.yaml:" |
| 95 | + print_success " version: $version" |
| 96 | + print_success " appVersion: \"$app_version\"" |
| 97 | +} |
| 98 | + |
| 99 | +# Main script logic |
| 100 | +main() { |
| 101 | + # Check if version argument is provided |
| 102 | + if [[ $# -eq 0 ]]; then |
| 103 | + print_error "No version provided" |
| 104 | + show_usage |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + |
| 108 | + # Check for help flag |
| 109 | + if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 110 | + show_usage |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | + |
| 114 | + local input_version="$1" |
| 115 | + local chart_file="cmd/relayproxy/helm-charts/relay-proxy/Chart.yaml" |
| 116 | + |
| 117 | + # Parse and validate version |
| 118 | + local parsed_version |
| 119 | + if ! parsed_version=$(parse_version "$input_version"); then |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + |
| 123 | + print_success "Parsed version: $parsed_version" |
| 124 | + |
| 125 | + # Update Chart.yaml |
| 126 | + if ! update_chart_yaml "$chart_file" "$parsed_version"; then |
| 127 | + exit 1 |
| 128 | + fi |
| 129 | + |
| 130 | + print_success "Helm chart version bump completed successfully! 🦁" |
| 131 | +} |
| 132 | + |
| 133 | +# Run main function with all arguments |
| 134 | +main "$@" |
0 commit comments