diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29e8db85..5e09a141 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} diff --git a/.github/workflows/notifications.yml b/.github/workflows/notifications.yml new file mode 100644 index 00000000..8011f377 --- /dev/null +++ b/.github/workflows/notifications.yml @@ -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("&","&");gsub("<","\\<");gsub(">","\\>");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: ``, + 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: ``, + short: true + }] + }] + } + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}