LoginSignup
7
6

More than 3 years have passed since last update.

GitHubのプッシュをSlackに通知

Last updated at Posted at 2020-03-08

Slackの設定

●Slack ヘルプセンター
https://slack.com/intl/ja-jp/help/articles/115005265063-Slack-%E3%81%A7%E3%81%AE-Incoming-Webhook-%E3%81%AE%E5%88%A9%E7%94%A8

「Incoming Webhook の設定」の手順で、Incoming Webhook URLを発行します。

Google Cloud Platform (Cloud Function)の設定

Google Cloud Platformの登録をしていない場合は、登録を。

手順

  • 「Cloud Function」に移動します。
  • 「関数を作成」で関数を作ります。設定は下記

名前:任意
割り当てられるメモリ:任意
トリガー:HTTP
認証:未認証の呼び出しを許可する
ソースコード:インライン エディタ
ランタイム:Python3.7
実行する関数:ソースに記述した関数名(sampleではslack_notice)

ソースコード

sample.py
import requests
import json

def slack_notice(request):

    webhook_url = "https://hooks.slack.com/<Incoming Webhook URL>"
    request_json = request.get_json()

    if 'commits' in request_json:
        name = request_json['commits'][0]['author']['name']
        text = name+"がプッシュしました。"
        requests.post(webhook_url, data = json.dumps({
            "text": text
        }))

GitHubの設定

「Setteing」→「Webhooks」→「Add webhook」に移動し、下記のように設定

Payload URL:Google Cloud Platformのトリガーに書かれているURL
Content type:application/json
SSL verification:Enable SSL verification
Which events would you like to trigger this webhook?:Just the push event.
Active:チェック

あとは試しにプッシュ!

でけた
img.png

7
6
0

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
7
6