Slack Notifications #12591
This file contains hidden or 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: Slack Notifications | |
| on: | |
| workflow_dispatch: # for testing | |
| check_suite: | |
| types: [completed] | |
| jobs: | |
| notify: | |
| runs-on: github-ubuntu-latest-s # Public GitHub hosted runner required, Self-Hosted runners do not support Docker-in-Docker | |
| permissions: | |
| id-token: write | |
| checks: read | |
| if: github.event.check_suite.head_branch == 'master' && !contains(fromJson('["SUCCESS", "NEUTRAL", "SKIPPED"]'), github.event.check_suite.conclusion) | |
| steps: | |
| - name: Vault Secrets | |
| id: secrets | |
| uses: SonarSource/vault-action-wrapper@v3 | |
| with: | |
| secrets: development/kv/data/slack token | SLACK_TOKEN; | |
| - name: Check run details | |
| id: failedRun | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| shell: pwsh | |
| run: | | |
| $SuccessConclusions = @("SUCCESS", "NEUTRAL", "SKIPPED") | |
| $Event = Get-Content -Path $env:GITHUB_EVENT_PATH | ConvertFrom-Json -Depth 100 | |
| $Request = gh api $Event.check_suite.check_runs_url | |
| $FailedRuns = $Request | ConvertFrom-Json | select -ExpandProperty check_runs | where { $SuccessConclusions -NotContains $_.conclusion } | foreach { "* [$($_.name)]($($_.details_url))" } | |
| $OFS = [Environment]::NewLine # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#ofs | |
| $EOF = (New-Guid).Guid # https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#multiline-strings | |
| $Message=@" | |
| message<<$EOF | |
| $($Event.check_suite.app.name ?? 'Pipeline') in [$($Event.repository.name)]($($Event.repository.html_url)) failed ($($Event.check_suite.conclusion)) on $($Event.check_suite.head_branch): $($Event.check_suite.head_commit.message -replace '[\n\r].*') | |
| $FailedRuns | |
| $EOF | |
| "@ | |
| $Message | |
| $Message >> $env:GITHUB_OUTPUT | |
| - name: Slack Notification | |
| uses: rtCamp/action-slack-notify@e31e87e03dd19038e411e38ae27cbad084a90661 # v2.3.3 | |
| if: always() | |
| env: | |
| SLACK_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).SLACK_TOKEN }} | |
| SLACK_CHANNEL: squad-dotnet # for testing: notification_tester | |
| SLACK_TITLE: Build failed | |
| SLACK_MESSAGE: ${{ steps.failedRun.outputs.message }} | |
| SLACK_USERNAME: NotifierBot | |
| SLACK_COLOR: danger | |
| MSG_MINIMAL: true | |
| SLACKIFY_MARKDOWN: true | |
| SLACK_FOOTER: ' ' | |
| SLACK_MSG_AUTHOR: ' ' | |
| SLACK_ICON_EMOJI: dotnet |