Report job failure #16
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: Report job failure | |
on: | |
workflow_dispatch: { } | |
workflow_run: | |
workflows: | |
- '*' | |
types: | |
- completed | |
jobs: | |
notify-on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' && github.actor != 'dependabot[bot]' && github.event.workflow_run.pull_requests[0].user.login != 'dependabot[bot]' }} | |
steps: | |
- name: Prepare Slack Message | |
id: slack-user | |
shell: bash | |
env: | |
SLACK_ID: ${{ secrets.SLACK_ID }} | |
run: | | |
channel_id=$(echo "$SLACK_ID" | jq -r .\"${{ github.actor }}\") | |
echo "::add-mask::$channel_id" | |
echo SLACK_CHANNEL=$channel_id >> $GITHUB_ENV | |
jobs=$(curl -L \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
${{ github.event.workflow_run.jobs_url }}) | |
header="GitHub Workflow <${{ github.event.workflow_run.html_url }}|${{ github.event.workflow_run.name }} ${{ github.event.workflow_run.run_number }}> failed:" | |
echo SLACK_MESSAGE_HEADER=$header >> $GITHUB_ENV | |
context="*Branch:* <${{ github.server_url }}/${{ github.repository }}/tree/${{ github.event.workflow_run.head_branch }}|${{ github.event.workflow_run.head_branch }}>\n*Commit:* <${{ github.server_url }}/${{ github.repository }}/commit/${{ github.event.workflow_run.head_commit.id }}|$(echo "${{ github.event.workflow_run.head_commit.id }}" | cut -c1-8) $(echo -e "${{ github.event.workflow_run.head_commit.message }}" | head -n1)>" | |
echo SLACK_MESSAGE_CONTEXT=$context >> $GITHUB_ENV | |
echo -e "SLACK_MESSAGE_BODY=$(jq -r '(.jobs[] | select(.conclusion == "failure") | {job: .name, step: (.steps[] | select(.conclusion == "failure").name), url: .html_url}) | "• <" + (.url) + "|" + (.job) + " / " + (.step) + ">"' <<<$jobs)" | sed -e '$ ! s/$/\\n/' -e 's/"/\\"/g' | tr -d '\n' >> $GITHUB_ENV | |
- name: Send to Slack | |
id: slack | |
uses: slackapi/[email protected] | |
with: | |
channel-id: ${{ env.SLACK_CHANNEL }} | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "${{ env.SLACK_MESSAGE_HEADER }}" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "${{ env.SLACK_MESSAGE_BODY }}" | |
} | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{ | |
"type": "mrkdwn", | |
"text": "${{ env.SLACK_MESSAGE_CONTEXT }}" | |
} | |
] | |
} | |
] | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACKBOT_TOKEN }} |