LoginSignup
2
1

More than 5 years have passed since last update.

git pull を slackに通知する

Posted at

各サーバーで git pull コマンドが使われたときに Slack へ通知するようにしました。

ChatOps的には、 Slack から git pull できるようにしたかったですが、
Slack をチーム内で試験的に導入していることもあり、今回は控えめです。

git の クライアントサイドのフック機能を使いました。
デフォルトでは、git pull にフックするサンプルは用意されていないので新規でファイルを作成します。

:arrow_down: Qiita にいい感じにまとめている記事がありました
Git hooksまとめ

実装

各サーバーに SSH接続

cd App/.git/hooks
vi post-merge

スクリプトはこんな風にしてみました。
channel に通知したいチャンネル名を指定します。
環境変数$APP_ENVにはサーバー名を入れています。
Slack の incomming webhook の APIキーは :arrow_down: を参考に取得してください。
http://qiita.com/satoshi03/items/14495bf431b1932cb90b

#!/bin/bash
commitid=$(git log -1 --pretty=format:"%h" --branches)
commitmessage=$(git log -1 --pretty=format:"%s" --branches)
committer=$(git log -1 --pretty=format:"%an" --branches)
slack="https://hooks.slack.com/services/[SLACK-INCOMMING-WEBHOOK-API-KEY]"
channel="#test"

payload="payload={
  \"channel\": \"${channel}\",
  \"username\": \"${APP_ENV}\",
  \"attachments\": [
    {
      \"fallback\": \"git pull\",
      \"color\": \"#36a64f\",
      \"pretext\": \"git pull\",
      \"author_name\": \"${committer}\",
      \"title\": \"${commitid} - ${commitmessage}\",
    }
  ],
  \"icon_emoji\": \":warning:\"
}"
curl -X POST --data-urlencode "${payload}" "${slack}"

これで、git pull 実行時にslackに通知することができました。

image

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1