|
| 1 | +#!/bin/bash |
| 2 | +################################################################### |
| 3 | +#Script Name : git tag modified helm chart |
| 4 | +#Description : creates a git tag for each modified helm chart |
| 5 | +#Args : - |
| 6 | +#Hint : Only works with Helm Chart Version compare |
| 7 | +################################################################### |
| 8 | +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) |
| 9 | +# shellcheck disable=SC1091 |
| 10 | +source "$SCRIPT_DIR"/../shared-variables.sh |
| 11 | + |
| 12 | +declare resultPwd exitCode |
| 13 | + |
| 14 | +set +e |
| 15 | +echo "The script you are running has:" |
| 16 | +echo "basename: [$(basename "$0")]" |
| 17 | +echo "dirname : [$(dirname "$0")]" |
| 18 | +echo "pwd : [$(pwd)]" |
| 19 | + |
| 20 | +declare -a filesToRemove=("pr-status-check-human-friendly.txt" "modified_files.txt") |
| 21 | +REMOVE_FILES_IF_EXISTS "$resultPwd" "${filesToRemove[@]}" |
| 22 | +################################################################### |
| 23 | +if [ -z "$COMMIT_HASH" ]; then |
| 24 | + COMMIT_HASH="89b624080bb62e7f5ac340d81fef96c9d6446d22" |
| 25 | + echo "!!! WARNING: \$COMMIT_HASH NEEDS TO BE SET e.g. Default set COMMIT_HASH=${COMMIT_HASH}! \"export COMMIT_HASH=${COMMIT_HASH}\"!!!" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | + |
| 30 | +################################################################### |
| 31 | +# Function to print a row with fixed-width columns |
| 32 | +print_row() { |
| 33 | + printf "%-70s %-50s %-15s %-15s\n" "$1" "$2" "$3" "$4" |
| 34 | +} |
| 35 | + |
| 36 | +git_tag_check() { |
| 37 | + local tag=$1 |
| 38 | + git rev-parse -q --verify "refs/tags/$tag" &>/dev/null |
| 39 | +} |
| 40 | +################################################################### |
| 41 | + |
| 42 | +# List modified files of commit |
| 43 | +git diff-tree --no-commit-id --name-only -r "$COMMIT_HASH" >modified_files.txt |
| 44 | +# shellcheck disable=SC2181 |
| 45 | +if [ $? != 0 ]; then |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | +# Iterate over the array of paths with filenames and extract the directory paths |
| 49 | +unique_paths=() |
| 50 | +while IFS= read -r path; do |
| 51 | + |
| 52 | + dir=$(dirname "$path") |
| 53 | + |
| 54 | + if [[ "$dir" == *templates ]]; then |
| 55 | + dir=${dir%/*} |
| 56 | + fi |
| 57 | + dir=${dir%*/templates*} # retain the part before "/templates" |
| 58 | + |
| 59 | + if [ -f "$dir/Chart.yaml" ] || [ -f "$dir/Chart.yml" ]; then |
| 60 | + unique_paths+=("$dir") |
| 61 | + fi |
| 62 | +done <"modified_files.txt" |
| 63 | + |
| 64 | +if [ ${#unique_paths[@]} -eq 0 ]; then |
| 65 | + echo -e "No Helm Charts have been modified!\n" |
| 66 | + echo -e "List of Modified files:" |
| 67 | + cat "modified_files.txt" |
| 68 | + exit "$exitCode" |
| 69 | +fi |
| 70 | + |
| 71 | +# Sort and remove duplicates from the list of paths |
| 72 | +# shellcheck disable=SC2207 |
| 73 | +unique_paths=($(echo "${unique_paths[@]}" | tr ' ' '\n' | sort -u)) |
| 74 | +{ |
| 75 | + # Header row |
| 76 | + printf "%-70s %-50s %-15s %-15s\n" "Modified Helm Chart:" "Name:" "Version:" "Status:" |
| 77 | + |
| 78 | + git checkout "$COMMIT_HASH" |
| 79 | + |
| 80 | + for helmChart in "${unique_paths[@]}"; do |
| 81 | + chartVersion=$(yq -r eval '.version' "$helmChart/Chart.yaml") |
| 82 | + chartName=$(yq -r eval '.name' "$helmChart/Chart.yaml") |
| 83 | + gitTag="$chartName-$chartVersion" |
| 84 | + if git_tag_check "$gitTag"; then |
| 85 | + print_row "$helmChart" "$chartName" "$chartVersion" "-" |
| 86 | + else |
| 87 | + |
| 88 | + git tag "$gitTag" "$COMMIT_HASH" |
| 89 | + # shellcheck disable=SC2181 |
| 90 | + if [ $? != 0 ]; then |
| 91 | + print_row "$helmChart" "$chartName" "$chartVersion" "ERROR: git tag $gitTag" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + git push origin "$gitTag" |
| 96 | + # shellcheck disable=SC2181 |
| 97 | + if [ $? != 0 ]; then |
| 98 | + print_row "$helmChart" "$chartName" "$chartVersion" "ERROR: git push origin $gitTag" |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + |
| 102 | + print_row "$helmChart" "$chartName" "$chartVersion" "git tagged&pushed $gitTag" |
| 103 | + |
| 104 | + fi |
| 105 | + |
| 106 | + done |
| 107 | + |
| 108 | +} >>"$resultPwd/pr-status-check-human-friendly.txt" |
| 109 | + |
| 110 | +cat "$resultPwd/pr-status-check-human-friendly.txt" |
| 111 | + |
| 112 | +exit "$exitCode" |
0 commit comments