3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

特定のブランチへのプッシュをgithubからdiscordに通知をする方法

Last updated at Posted at 2023-03-17

はじめに

チーム開発をしていて特定のブランチへのプッシュをdiscordに通知したいなと思い、意外と面倒だったのでメモを残しておきます。
今回はmainとdevの2つのブランチを対象とします。
プルリクエストにも対応したものとなっているはずです。

方法

Discord上の操作

githubからdiscordに通知を飛ばす方法自体は、@toshiponさんが丁寧に解説されているので、そちらを参照してください!
https://qiita.com/toshipon/items/2a1513b584650dcd81c1

GitHub上の操作

  • GitHub上でリポジトリのSettings>(左のタブの)Secrets And variablesからRepository secretsに環境変数を追加
    • 今回は、Discordから発行されたのURLからWEBHOOK_IDとWEBHOOK_TOKENを作成する
  • GitHub上で .github/workflows/ディレクトリを作る(ディレクトリの作成はnew files において「/」を入力することでできる)
  • github actionを作る
    • 今回はdiscord.ymlを作成します
  • Settings>WebhooksにおいてDiscordのURLを設定する
  • Let me select individual events.を選択し、Workflow jobs と Workflow runs にチェックを付ける
    • 2つにチェックをつける必要があるかはわからないです
  • Update Webhookをクリックして完了

discord.ymlの内容

.github/workflows/discord.yml

name: Discord Message Notify

on:
  push:
    branches: 
    - main
    - dev
  pull_request:
    branches:
    - main
    - dev

jobs:
  notify_discord_of_push:
    name: Get Commit Message
    runs-on: ubuntu-latest
    steps:
    - name: Check out repo's head branch
      uses: actions/checkout@v2.4.0
      with:
          ref: ${{ github.head_ref }} #For pull_request events, GITHUB_REF contains the merge branch (refs/pull/:prNumber/merge).
    - name: Get commit message
      run: |
        echo "Commit_Message=$(git log --pretty=oneline --all --abbrev-commit)" >> $GITHUB_ENV
    - name: Notify discord
      uses: appleboy/discord-action@0.0.3
      with:
        webhook_id: ${{ secrets.WEBHOOK_ID }}
        webhook_token: ${{ secrets.WEBHOOK_TOKEN }}
        color: "#48f442"
        username: "GitHub Bot"
        args: "${{ github.event_name }} by @${{ github.actor }} \nmessage: ${{ env.Commit_Message }}"

補足

GitHub Actionsのドキュメント

- name: Check out repo's head branch
  uses: actions/checkout@v2.4.0
  with:
      ref: ${{ github.head_ref }} #For pull_request events, GITHUB_REF contains the merge branch (refs/pull/:prNumber/merge).

headブランチにチェックアウトします。

- name: Get commit message
  run: |
    echo "Commit_Message=$(git log --pretty=oneline --all --abbrev-commit)" >> $GITHUB_ENV

コミットメーセージを取得し、GITHUB_ENV 環境ファイルに書き込みます。
環境変数を GITHUB_ENV に書き込む例

- name: Notify discord
  uses: appleboy/discord-action@0.0.3
  with:
    webhook_id: ${{ secrets.WEBHOOK_ID }}
    webhook_token: ${{ secrets.WEBHOOK_TOKEN }}
    color: "#48f442"
    username: "GitHub Bot"
    args: "${{ github.event_name }} by @${{ github.actor }} \nmessage: ${{ env.Commit_Message }}"

DiscordのWebhookを使用してメッセージを送信します。
args: の部分でDiscordに送信するメッセージ内容を指定しています。
appleboy/discord-actionのリポジトリを使用させていただきました。

以下のようなメッセージが通知されると思います
スクリーンショット 2023-03-17 19.29.56.png

おわりに

 echo "Commit_Message=$(git log --pretty=oneline --all --abbrev-commit)" >> $GITHUB_ENV

の部分で時間がかかってしまいました。
runs-on: のところでubuntu-latestを設定しているため、Ubuntu環境でのコマンドを書けば良いのですが、書いたことがなかったため大変でした。
コマンドの勉強にもなったということで!

参考文献

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?