Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active July 3, 2024 08:00
Show Gist options
  • Save luckylittle/1de324c7848d9bcbaf9d34a990fbffe8 to your computer and use it in GitHub Desktop.
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
#!/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
@luckylittle
Copy link
Author

The folder structure looks like this:

├── helm-repository
│   └── charts
│       ├── authentication
│       │   └── charts
│       ├── cluster-cert-manager
│       ├── cluster-efs
│       │   └── templates
│       ├── cluster-ingress
│       │   └── templates
│       ├── cluster-ldap
│       │   └── templates
│       ├── cluster-ldap-sync
│       │   └── templates
│       ├── cluster-logging
│       │   └── templates
│       ├── cluster-seed
│       │   └── templates
│       ├── collectord
│       │   └── charts
│       ├── compliance-operator-full-stack
│       │   └── templates
│       │       └── compliance
│       ├── gitops-application-argocd
│       │   └── templates
│       ├── gitops-linker-app
│       │   └── templates
│       ├── gitops-linker-team
│       │   └── templates
│       ├── gitops-payload
│       │   └── templates
│       ├── helper-operator
│       │   └── templates
│       │       ├── jobs
│       │       └── operators
│       ├── helper-status-checker
│       │   └── templates
│       ├── monitoring
│       │   └── charts
│       ├── namespaces
│       │   └── templates
│       ├── test-app
│       │   └── templates
│       └── vault
│           ├── scripts
│           └── templates
│               └── tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment