wordpress-develop/.github/workflows/slack-notifications.yml
Jonathan Desrosiers 00140db135 Build/Test Tools: Add branch filtering for Slack notifications workflow.
Since branch filtering happens prior to the workflow run being created, filtering the branches that `workflow_run` will fire on for this workflow should cut down on the number of skipped “Slack Notifications” runs listed in the Actions section of the repository.

This also removes the check for a `skipped` outcome in the requesting workflow. Workflows for push events resulting from WordPress Core commits are never skipped.

See #52644.

git-svn-id: https://develop.svn.wordpress.org/trunk@51558 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-05 16:52:51 +00:00

126 lines
4.5 KiB
YAML

##
# Posts messages to the Making WordPress Core Slack Instance by
# submitting data to Slack webhook URLs received by Slack Workflows.
##
name: Slack Notifications
on:
workflow_run:
workflows:
- Code Coverage Report
- Coding Standards
- End-to-end Tests
- JavaScript Tests
- PHP Compatibility
- PHPUnit Tests
- Test NPM
- Test old branches
types:
- completed
branches:
- master
- trunk
- '[3-9].[0-9]'
jobs:
# Gathers the details needed for Slack notifications.
#
# These details are passed as outputs to the subsequent, dependant jobs that
# submit data to Slack webhook URLs configured to post messages.
#
# Performs the following steps:
# - Retrieves the previous workflow run and stores its conclusion.
# - Sets the previous conclusion as an output.
# - Constructs and stores a message payload as an output.
prepare:
name: Prepare notifications
runs-on: ubuntu-latest
if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event.workflow_run.event != 'pull_request' }}
outputs:
previous_conclusion: ${{ steps.previous-conclusion.outputs.previous_conclusion }}
payload: ${{ steps.create-payload.outputs.payload }}
steps:
- name: Get details about the previous workflow run
id: previous-result
uses: actions/github-script@a3e7071a34d7e1f219a8a4de9a5e0a34d1ee1293 # v4.0.2
with:
script: |
const workflow_runs = await github.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: ${{ github.event.workflow_run.workflow_id }},
branch: '${{ github.event.workflow_run.head_branch }}',
per_page: 1,
page: 2,
});
return workflow_runs.data.workflow_runs[0].conclusion;
- name: Store previous conclusion as an output
id: previous-conclusion
run: echo "::set-output name=previous_conclusion::${{ steps.previous-result.outputs.result }}"
- name: Construct payload and store as an output
id: create-payload
run: echo "::set-output name=payload::{\"workflow_name\":\"${{ github.event.workflow_run.name }}\",\"ref_name\":\"${{ github.event.workflow_run.head_branch }}\",\"run_url\":\"${{ github.event.workflow_run.html_url }}\"}"
# Posts notifications when a workflow fails.
failure:
name: Failure notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: Post failure notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_FAILURE_WEBHOOK }}
# Posts notifications the first time a workflow run succeeds after previously failing.
fixed:
name: Fixed notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ needs.prepare.outputs.previous_conclusion == 'failure' && github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Post failure notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_FIXED_WEBHOOK }}
# Posts notifications when a workflow is successful.
success:
name: Success notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Post success notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_SUCCESS_WEBHOOK }}
# Posts notifications when a workflow is cancelled.
cancelled:
name: Cancelled notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event.workflow_run.conclusion == 'cancelled' }}
steps:
- name: Post cancelled notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_CANCELLED_WEBHOOK }}