Skip to content

Instantly share code, notes, and snippets.

@caiosba
Created June 12, 2014 00:30
Show Gist options
  • Save caiosba/d69be6e7e343e27a9e62 to your computer and use it in GitHub Desktop.
Save caiosba/d69be6e7e343e27a9e62 to your computer and use it in GitHub Desktop.
Git hook to run tests after commit and notify a Slack channel
#!/bin/bash
# Run tests after commit and notify a Slack channel
webhook_url="https://<your subdomain>.slack.com/services/hooks/incoming-webhook?token=<your token>"
channel="#<your channel>"
root=$(git rev-parse --show-toplevel)
cd $root
repo=$(basename $root)
user=$(git config --global --get user.name)
output=$(bundle exec rake test)
output=$(echo $output | sed 's/.*\(Finished tests in [0-9.]\+s, [0-9.]\+ tests\/s, [0-9.]\+ assertions\/s\.\).*[^0-9]\([0-9]\+ tests, .*\)/\1 \2/g' | sed 's/Finished tests/executed tests/g')
rev=$(git log -1 --format=format:%h)
ref=$(git rev-parse --abbrev-ref HEAD)
cd - >/dev/null
msg=$(echo "Build $rev of $repo/$ref by $user $output")
curl -X POST --data-urlencode "payload={\"channel\": \"$channel\", \"username\": \"Git CI\", \"text\": \"$msg\"}" $webhook_url
exit 0
@caiosba
Copy link
Author

caiosba commented Jun 12, 2014

Just copy this file to .git/hooks on the root of your repository and set the variables $webhook_url and $channel.

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