How to Post a Slack Notification from GitHub Actions
Send build results to Slack with the official Slack action or a plain incoming-webhook curl.
Store a Slack webhook URL (or bot token) as a secret, then post with slackapi/slack-github-action. Gate it on if: failure() to alert only on red builds.
Notify Slack on failure
This step runs only when an earlier step failed, posting to the configured webhook.
.github/workflows/ci.yml
steps:
- run: npm test
- name: Notify Slack on failure
if: failure()
uses: slackapi/slack-github-action@v2
with:
webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
webhook-type: incoming-webhook
payload: |
text: "Build failed on ${{ github.ref_name }} (${{ github.sha }})"Gotchas
- Without
if: failure()(orif: always()), the notify step is skipped when a prior step fails. - Store the webhook URL as a repo/org secret - it grants posting access to your channel.
- Incoming-webhook payloads use Slack message JSON/blocks; a bot token (
chat.postMessage) gives richer formatting.