Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Created June 27, 2016 05:59
Show Gist options
  • Save hkurokawa/19cf829edead6a875384f51c10795823 to your computer and use it in GitHub Desktop.
Save hkurokawa/19cf829edead6a875384f51c10795823 to your computer and use it in GitHub Desktop.
#! /bin/sh
WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXX
CHANNEL=#mychannel
echo "Starting notification for ${CHANNEL} with ${WEBHOOK_URL}"
if [ -z "$(which curl)" ];then
echo "curl command doesn't exist!" 1>&2
exit 1
fi
if [ $? -eq 0 ]; then
msg="デプロイに成功しました。
color="good"
else
msg="デプロイに失敗しました!
color="danger"
fi
prno=$(git log --pretty=format:'%s' -1 | sed -n -e 's/^.*Merge pull request #\([0-9][0-9]*\).*/\1/p')
if [ ! -z "${prno}" ]; then
fields="[{\"title\": \"Pull Request\", \"value\": \"<https://github.com/hkurokawa/sample/pull/${prno}|#${prno}>\"}]"
else
fields="[]"
fi
data="{\"text\": \"${msg}\", \"channel\": \"${CHANNEL}\", \"icon_emoji\": \":ghost:\", \"username\": \"my-bot\", \"attachments\": [{\"fallback\": \"$(git log -1 --oneline)\", \"color\": \"${color}\", \"author_name\": \"$(git log --pretty=format:'%an' -1)\", \"title\": \"$(git log --pretty=format:'%s' -1)\", \"title_link\": \"https://github.com/hkurokawa/sample/commit/$(git log --pretty=format:'%H' -1)\", \"text\": \"$(git log --pretty=format:'%b' -1)\", \"ts\": \"$(git log --pretty=format:'%ct' -1)\", \"fields\": ${fields}, \"footer\": \"GitHub Commit\"}]}"
curl -X POST -H 'Content-type: application/json' --data "${data}" ${WEBHOOK_URL}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment