Created
September 5, 2013 12:35
-
-
Save dopiaza/6449517 to your computer and use it in GitHub Desktop.
Git post-update commit hook to post a message to a Slack channel. Uses https://gist.github.com/dopiaza/6449505
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is designed to be called from a git post-update hook | |
# Typical usage | |
# In your post-update hook, the first parameter is the branch ref. So in post-update, you would | |
# invoke this script something like this: | |
# /usr/local/bin/post-update-slack $1 <branch to match> <token> <channel> | |
# The script will only post to slack if the branch being updated matches the one listed as <branch to match> | |
# Use "any" to match any branch (hopefully you don't have a branch called "any") | |
# Example: | |
# /usr/local/bin/post-update-slack $1 any my_token git-commits | |
# /usr/local/bin/post-update-slack $1 master my_token releases | |
ref=$1 | |
matchbranch=$2 | |
token=$3 | |
channel=$4 | |
if [ $(git rev-parse --is-bare-repository) = true ] | |
then | |
repo=$(basename "$PWD") | |
repo=${repo%.git} | |
else | |
repo=$(basename $(readlink -nf "$PWD"/..)) | |
fi | |
branch=$(git rev-parse --symbolic --abbrev-ref $ref) | |
if [[ $matchbranch == "any" ]] || [[ $branch == $matchbranch ]] | |
then | |
message=$(git log -1 $ref "--pretty=format:%ci %cn %h %s") | |
text="[$repo:$branch] $message" | |
/usr/local/bin/slackpost $token $channel $text 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment