Notify on Workflow Failure #1368
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Notify on Workflow Failure | |
on: | |
workflow_run: | |
workflows: ["Backend CI", "Backend CD", "Frontend CD", "Frontend CI"] | |
types: | |
- completed | |
jobs: | |
notify: | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: 변수 추출 | |
run: | | |
SLACK_WEBHOOK_URL=${{ secrets.SLACK_CICD_FAIL_WEBHOOK_URL }} | |
SLACK_IDS=${{ secrets.SLACK_IDS }} | |
echo "SLACK_WEBHOOK_URL=$SLACK_WEBHOOK_URL" >> $GITHUB_ENV | |
echo "SLACK_IDS=$SLACK_IDS" >> $GITHUB_ENV | |
- name: CI 또는 CD 실패 시 Slack 알람 보내기 | |
run: | | |
WORKFLOW_NAME="${{ github.event.workflow_run.name }}" | |
WORKFLOW_URL="${{ github.event.workflow_run.html_url }}" | |
ASSIGNEE="${{ github.event.workflow_run.actor.login }}" | |
mentions="" | |
parse_slack_ids() { | |
echo "$SLACK_IDS" | jq -r 'to_entries | map("\(.key):\(.value)") | .[]' | |
} | |
slack_id=$(parse_slack_ids | grep "^$ASSIGNEE:" | cut -d':' -f2) | |
if [ -n "$slack_id" ]; then | |
MENTION="<@$slack_id>" | |
else | |
MENTION="$ASSIGNEE" | |
fi | |
if [ ! -z "$MENTION" ]; then | |
message="$MENTION 님, 실행된 $WORKFLOW_NAME 워크플로가 실패했습니다. <$WORKFLOW_URL|확인하러 가기>." | |
curl -X POST -H 'Content-type: application/json' \ | |
--data "{\"text\":\"$message\"}" \ | |
"$SLACK_WEBHOOK_URL" | |
echo "Sent message: $message" | |
else | |
echo "No MENTIONER to notify" | |
fi |