Skip to content

Instantly share code, notes, and snippets.

@akhan4u
Created October 20, 2020 13:29
Show Gist options
  • Save akhan4u/5a3e86f6fcc11311539042ee04f2106a to your computer and use it in GitHub Desktop.
Save akhan4u/5a3e86f6fcc11311539042ee04f2106a to your computer and use it in GitHub Desktop.
Git pre-commit hook for linting and validating Cloudformation templates.
#!/bin/sh
# Only Run If Any Cloudformation Template Is Modified And Checks Linting Of The Template
#------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
#------------------
FILES=$(git diff --cached --name-only --diff-filter=ACMR "*.yml" "*.yaml" | sed 's| |\\ |g')
if [ -z "$FILES" ]; then
exit 0
else
echo "${GREEN}Linting Cloudformation Templates.....${NC}"
echo "$PWD/$FILES" | xargs yamllint -c yamllint-rules
testResults=$?
if [ $testResults -gt 0 ]
then
echo "\n${RED}Template Linting Failed! Please fix the errors and try again${NC}\ngit commit ABORTED 🔥"
exit 1
else
echo "${GREEN}SUCCESS!${NC}"
# Run Cloudformation Template Validation locally. Make Sure you have AWS CLI Configured and Working properly
echo "${GREEN}Validating Cloudformation Templates.....${NC}"
echo "$PWD/$FILES" | xargs -t -I {} aws cloudformation validate-template --template-body file://{} 1> /dev/null
validateResults=$?
if [ $validateResults -gt 0 ]
then
echo "\n${RED}Template Validation Failed! Please fix the errors and try again${NC}\ngit commit ABORTED 🔥"
exit 1
else
echo "${GREEN}SUCCESS!${NC}"
fi
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment