Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active March 26, 2018 08:11
Show Gist options
  • Save wookietreiber/8e92f6db1bc632d501a9fa0bafbcb391 to your computer and use it in GitHub Desktop.
Save wookietreiber/8e92f6db1bc632d501a9fa0bafbcb391 to your computer and use it in GitHub Desktop.
git pre-commit hook to check ansible yaml files with ansible-lint
#!/bin/bash
if git rev-parse --verify HEAD &> /dev/null ; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
files=$(
git diff-index --cached --name-only $against |
grep -E '\.ya?ml$'
)
if [[ -n $files ]] ; then
if ! command -v ansible-lint &> /dev/null ; then
echo -e "\e[1m\e[33m[warn]\e[0m \e[1mansible-lint\e[0m is not installed, skipping tests" >&2
exit 0
fi
fi
for file in $files ; do
tmp_lintr=$(mktemp)
git show ":$file" > "$tmp_lintr"
ansible-lint "$tmp_lintr" || exit 1
rm -f "$tmp_lintr"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment