Skip to content

Instantly share code, notes, and snippets.

@ritcheyer
Forked from dopiaza/post-update-slack
Created January 11, 2016 09:20
Show Gist options
  • Save ritcheyer/ffc7dc1f6a3cb20ab476 to your computer and use it in GitHub Desktop.
Save ritcheyer/ffc7dc1f6a3cb20ab476 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
#!/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