From db944c8ab46fabc01bc2a34d96c27f730ab3d22a Mon Sep 17 00:00:00 2001 From: Aleksey Zalesov Date: Sat, 24 Feb 2018 16:01:22 +0300 Subject: [PATCH] Add pre-commit hook to check playbook syntax --- hooks/pre-commit.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 hooks/pre-commit.sh diff --git a/hooks/pre-commit.sh b/hooks/pre-commit.sh new file mode 100755 index 0000000..12835d4 --- /dev/null +++ b/hooks/pre-commit.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +# This is the pre-commit hook that runs each time with `git commit` +# and checks syntax of the playbooks +# To install run +# ln -s ../../hooks/pre-commit.sh .git/hooks/pre-commit + +echo "Running pre-commit hook" + +STASH_NAME="pre-commit-$(date +%s)" +git stash save -q --keep-index $STASH_NAME + +echo "Checking the playbook syntax" +ansible-playbook -i hosts site.yml --syntax-check +RESULT=$? + +if [[ $RESULT == 0 ]]; then + echo "Running Ansible linter" + ansible-lint *.yml + RESULT=$? +fi + +STASHES=$(git stash list) + +if [[ $STASHES =~ .*$STASH_NAME ]]; then + git stash pop -q +fi + +[ $RESULT -ne 0 ] && exit 1 +exit 0