Skip to content

Instantly share code, notes, and snippets.

@joest67
Created December 11, 2014 08:46
Show Gist options
  • Save joest67/89c416fd84d1153b4bbc to your computer and use it in GitHub Desktop.
Save joest67/89c416fd84d1153b4bbc to your computer and use it in GitHub Desktop.
Notify us by hipchat when someone deploy
#!/bin/sh
# Source config file
# should contains follows variables:
# AUTH_TOKEN
# ROOM_ID
source config.sh
my_func() {
return 1
}
my_func || RST=$?
############## HipChat Notify ####################
#
# Usage: hipchat_notify
# room_id message from
# color green/red
hipchat_notify() {
_URL="https://api.hipchat.com/v1/rooms/message"
_AUTH_TOKEN="$AUTH_TOKEN"
_MSG_FORMAT="text"
if [ $# -eq 4 ]; then
_ROOM_ID=$1
_MESSAGE=$2
_FROM=$3
_COLOR=$4
else
exit 1
fi
# api version1
curl -X POST \
$_URL \
--data-urlencode "color=$_COLOR" \
--data-urlencode "notify=1" \
--data-urlencode "room_id=$_ROOM_ID" \
--data-urlencode "message_format=$_MSG_FORMAT" \
--data-urlencode "auth_token=$_AUTH_TOKEN" \
--data-urlencode "from=$_FROM" \
--data-urlencode "message=$_MESSAGE"
if [[ ${RST+1} -eq 1 ]]; then
return $RST
else
return 0
fi
}
ROOM_ID="$ROOM_ID"
# title
AUTHOR="$BUILD_USER"
CURRENT_COMMIT=`git log -1 --pretty=format:%H`
TITLE="@$AUTHOR is deploying zeus@${CURRENT_COMMIT:0:7} to $server"
# content
PREV_COMMIT_LOG=".commit"
LAST_COMMIT=`cat $PREV_COMMIT_LOG`
CONTENT=`git log --pretty=oneline --abbrev-commit --first-parent $LAST_COMMIT...$CURRENT_COMMIT`
if [ -n "$CONTENT" ]; then
CONTENT="/code $CONTENT"
else
CONTENT="nothing changed from last commit"
fi
# result
CI_LINK="(ci: $BUILD_URL)"
if [[ ${RST+1} && $RST -ne 0 ]]; then
RESULT="Deploy fail $CI_LINK"
COLOR="red"
else
RESULT="Deploy success $CI_LINK"
COLOR="green"
fi
# from
FROM="Deploy"
##### title #####
hipchat_notify "$ROOM_ID" "$TITLE" "$FROM" "green"
##### content #####
hipchat_notify "$ROOM_ID" "$CONTENT" "$FROM" "purple"
##### result #####
hipchat_notify "$ROOM_ID" "$RESULT" "$FROM" "$COLOR"
###################### End Hipchat Deploy ##################
###################### Email Deploy #######################
email_notify() {
echo $CONTENT | mail -s "$TITLE" "$RECEIVER_LIST"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment