Last active
July 3, 2024 08:00
-
-
Save luckylittle/1de324c7848d9bcbaf9d34a990fbffe8 to your computer and use it in GitHub Desktop.
Pre-commit git hook for Helm charts - it does not allow you to commit without changing the version in Chart.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
##################################################### | |
## TO INSTALL THIS GIT HOOK: ## | |
## touch .git/hooks/pre-commit ## | |
## echo '!/bin/sh'> .git/hooks/pre-commit ## | |
## echo './pre-commit.sh' >> .git/hooks/pre-commit ## | |
## chmod +x pre-commit.sh .git/hooks/pre-commit ## | |
##################################################### | |
# Function to check if Chart.yaml file has been updated | |
function check_chart_version { | |
local dir="$1" | |
local chart_file="$dir/Chart.yaml" | |
if [[ -f "$chart_file" ]]; then | |
# Check if any file in the chart directory or its subdirectories has changed | |
if git diff --cached --name-only | grep -q "^$dir/"; then | |
# Check if Chart.yaml is in the list of changed files | |
if ! git diff --cached --name-only | grep -q "^$chart_file$"; then | |
echo "Error: You must update the Helm chart version in $chart_file" | |
exit 1 | |
fi | |
fi | |
fi | |
} | |
# Get the list of changed files | |
changed_files=$(git diff --cached --name-only) | |
# Extract the unique chart directories that contain changes | |
changed_chart_dirs=$(echo "$changed_files" | grep '^charts/' | awk -F'/' '{print $1"/"$2}' | sort -u) | |
# Check each chart directory for version update | |
for dir in $changed_chart_dirs; do | |
check_chart_version "$dir" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The folder structure looks like this: