Woopii Vyeolog

[Github] Github Action + Discord webhook 알림 설정 본문

Git, Github

[Github] Github Action + Discord webhook 알림 설정

WooPii 2024. 9. 22. 22:52

 

내용

Github에  소스코드를 merge, push 등의 특정 이벤트가 발생할 때,

Discord에 알림이 가도록하는 설정

 

 

과정

1. Discord Webhook 생성

Discord 알림을 받고자 하는 채널의 설정에 들어가면, '연동' 항목에서 Webhook 생성을 할 수 있다

 

Webhook 생성 후 Webhook Url을 복사한다.

 

2. Github Action Workflow 생성

github action을 등록할려면 workflow를 생성해야 함

샘플로 Simple workflow 선택

 

Simple workflow 'Configure' 선택하면 자동으로 workflow를 생성해줌

discord 알림을 보내기 위해서 다음과 같이 workflow를 수정 후 commit 진행

 

# This is a basic workflow to help you get started with Actions

name: Notification

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
#  push:
#    branches: [ "develop"]
  pull_request:
    branches: [ "develop" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.
          
      # Discord Webhook notification
      - name: Send Discord Notification
        if: always() # 실행 성공/실패 상관없이 알림을 보냄
        run: |
          # Determine color based on job status
          STATUS_COLOR=$([[ "${{ job.status }}" == "success" ]] && echo "3066993" || echo "15158332")

          # Extract branch name from ref
          BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')

          # Construct issue link (modify according to your needs)
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            ISSUE_LINK="${{ github.event.pull_request.html_url }}"
            TITLE="::::: Pull Request Notification :::::"
            DESCRIPTION="**Event Time**: ${{ github.event.pull_request.updated_at }}\n \
                          **Event Type**: ${{ github.event_name }}\n \
                          **Repository**: ${{ github.repository }}\n \
                          **PR Title**: ${{ github.event.pull_request.title }}\n \
                          **PR Author**: ${{ github.event.pull_request.user.login }}\n \
                          **PR Body**: ${{ github.event.pull_request.body }}\n \
                          **PR Merged**: ${{ github.event.pull_request.merged }}\n \
                          **Job Status**: ${{ job.status }}"
          else
            ISSUE_LINK="https://github.com/${{ github.repository }}/commit/${{ github.sha }}"
            TITLE="::::: Push Notification :::::"
            DESCRIPTION="**Event Time**: ${{ github.event.head_commit.timestamp }}\n \
                          **Event Type**: ${{ github.event_name }}\n \
                          **Repository**: ${{ github.repository }}\n \
                          **Branch**: $BRANCH_NAME\n \
                          **Commit Message**: ${{ github.event.head_commit.message }}\n \
                          **Job Status**: ${{ job.status }}"
          fi
          
          curl -H "Content-Type: application/json" \
          -X POST \
          -d "{\"embeds\": [{
              \"title\": \"$TITLE\",
              \"description\": \"$DESCRIPTION\",
              \"color\": $STATUS_COLOR,
              \"url\": \"$ISSUE_LINK\",
              \"footer\": { \
                \"text\": \"Click the link above to view the details.\" \
              } \
          }]}" \
          ${{ secrets.DISCORD_WEBHOOK_URL }}

 

3. Secrets 작성

이후 github 설정에서 secrets에 Webhook Url을 등록해야함

repository에서 'Settings' -> 'Secrets And Variables' -> 'Actions' -> 'New Repository Secrets' 선택

name엔 'DISCORD_WEBHOOK_URL' 을 적고

Secret엔 Webhook Url을 입력

 

 

테스트

workflow에서 develop 브랜치에서의 push 이벤트로 세팅했으니, develop브렌치 에서 push 진행

push 이벤트 이후 알림 발송 확인

 

 

Comments