Skip to content

Commit

Permalink
Add more info to Notification and notify for additional workflows (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Oct 12, 2021
1 parent 8f39e76 commit 38f8710
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
55 changes: 0 additions & 55 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,58 +122,3 @@ jobs:
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GIT_COMMIT=${{ github.sha }}
notify:
name: Notify
runs-on: ubuntu-20.04
needs: build-docker
if: always() && github.ref == 'refs/heads/master'
steps:
- name: Workflow Status
id: check
uses: martialonline/workflow-status@v2
- name: Output Variables
id: commit
run: |
echo "::set-output name=sha::$(echo ${GITHUB_SHA} | cut -c1-7)"
echo "::set-output name=repo::$(echo ${GITHUB_REPOSITORY} | cut -d '/' -f 2)"
- name: Send Notification
uses: 8398a7/action-slack@v3
if:
steps.check.outputs.status == 'failure' ||
steps.check.outputs.status == 'cancelled'
with:
status: custom
custom_payload: |
{
username: 'Github',
icon_emoji: ':octocat:',
mention: 'channel',
attachments: [{
title: '[${{ steps.commit.outputs.repo }}] ${{ github.workflow }} pipeline has failed',
color: '${{ steps.check.outputs.status }}' == 'failure' ? 'danger' : 'warning',
fields: [{
title: 'Commit Hash',
value: '${{ steps.commit.outputs.sha }}',
short: true
},
{
title: 'Author',
value: '${{ github.actor }}',
short: true
},
{
title: 'Commit Message',
value: `${{ github.event.head_commit.message }}`,
short: false
},
{
title: 'Pipeline URL',
value: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
short: false
}]
}]
}
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
69 changes: 69 additions & 0 deletions .github/workflows/notifications.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Notification

on:
workflow_run:
branches: [master]
workflows:
- "Continuous Integration"
- "CodeQL"
- "Fossa"
- "Lint"
types:
- completed

jobs:
on-failure:
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: Output Variables
continue-on-error: true
id: vars
run: |
suite_id=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq '.check_suite_id')
event=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq -r '.event')
name=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].name')
url=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].html_url')
message_sanitized=$(awk '{gsub("&","&amp;");gsub("<","\\&lt;");gsub(">","\\&gt;");print}' <<<'${{ github.event.workflow_run.head_commit.message }}')
echo "::set-output name=name::$name"
echo "::set-output name=url::$url"
echo "::set-output name=event::$event"
echo "::set-output name=message::$message_sanitized"
- name: Send Notification
uses: 8398a7/action-slack@v3
with:
status: custom
custom_payload: |
{
username: 'Github',
icon_emoji: ':github:',
mention: 'channel',
attachments: [{
title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ steps.vars.outputs.event }})',
color: 'danger',
fields: [{
title: 'Commit',
value: `<https://github.com/${{ github.repository }}/commit/${{ github.event.workflow_run.head_commit.id }}|${{ steps.vars.outputs.message }}>`,
short: true
},
{
title: 'Failed Job',
value: `<${{ steps.vars.outputs.url }}|${{ steps.vars.outputs.name }}>`,
short: true
},
{
title: 'Author',
value: `${{ github.event.workflow_run.head_commit.author.name }}`,
short: true
},
{
title: 'Pipeline URL',
value: `<https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}|${{ github.event.workflow_run.id }}>`,
short: true
}]
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

0 comments on commit 38f8710

Please sign in to comment.