본문 바로가기

programming

Github actions 배포완료시 slack 으로 알림받기

1. 슬랙 앱 생성 및 설정

 

링크 접속: https://api.slack.com/apps

 

Slack API: Applications | Slack

Your Apps Don't see an app you're looking for? Sign in to another workspace.

api.slack.com

 

2. Create New App

- From scratch 선택

- App name : Front Release Notification App

- 슬랙 워크스페이스 선택

 

 

3. Incoming Webhooks 탭 이동

- On 활성화 처리

 

 

4. 화면 하단의 Add New Webhooks to Workspace 

- 게시할 채널 선택 : 알림 받을 채널을 선택

- 생성된 Webhook Url 복사

 

 

5. Github Repository 설정 - Secrets and Variables - Actions 로 접속

- New repository secert 클릭

- SLACK_WEBHOOK_URL

- 아까 복사한 웹훅 url 기입

 

 

6. github action yml 파일에 액션 추가

- 배포 완료시 슬랙으로 알림을 보내라는 action 을 추가

 

- name: Notify Slack on Success
        if: success()
        id: slack-success
        uses: slackapi/slack-github-action@v1.24.0
        with:
          payload: |
            {
              "channel": "슬랙 채널 ID",
              "attachments": [
                {
                  "color": "#36a64f",
                  "title": "✅ GitHub Action 성공",
                  "title_link": "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}",
                  "fields": [
                    {
                      "title": "Repository",
                      "value": "${{ github.repository }}",
                      "short": true
                    },
                    {
                      "title": "Tag",
                      "value": "${{ github.ref_name }}",
                      "short": true
                    }
                  ]      
                }
              ]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

      - name: Notify Slack on Failure
        if: failure() # 이 step은 job이 실패한 경우에만 실행됩니다.
        id: slack-failure
        uses: slackapi/slack-github-action@v1.24.0
        with:
          payload: |
            {
              "channel": "슬랙 채널 ID",
              "attachments": [
                {
                  "color": "#ff0000",
                  "title": "‼️ GitHub Action 실패",
                  "title_link": "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}",
                  "fields": [
                    {
                      "title": "Repository",
                      "value": "${{ github.repository }}",
                      "short": true
                    },
                    {
                      "title": "Tag",
                      "value": "${{ github.ref_name }}",
                      "short": true
                    }
                  ]      
                }
              ]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK