Skip to content

Instantly share code, notes, and snippets.

@pgib
Last active August 11, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgib/131365897bcd93d34e69a54be9105518 to your computer and use it in GitHub Desktop.
Save pgib/131365897bcd93d34e69a54be9105518 to your computer and use it in GitHub Desktop.
CircleCI precommit hook
#!/usr/bin/env bash
# Slightly modified from: https://circleci.com/blog/circleci-hacks-validate-circleci-config-on-every-commit-with-a-git-hook/
# The following line is needed by the CircleCI Local Build Tool (due to Docker interactivity)
exec < /dev/tty
# Only validate if we've changed the .circleci/config.yml file
if git diff --cached --name-only | grep --quiet ".circleci/config.yml"; then
# Make sure we have the circleci utility installed
# https://circleci.com/docs/local-cli
circleci_path=$(which circleci)
return_code=$?
if [ ${return_code} -eq 0 ]; then
# If validation fails, tell Git to stop and provide error message. Otherwise, continue.
if ! eMSG=$(circleci config validate -c .circleci/config.yml); then
echo "CircleCI Configuration Failed Validation."
echo $eMSG
exit 1
fi
else
echo "circleci utility not found, so we can't validate the changed config file."
sleep 2
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment