Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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