Slack messages from GitHub workflow

At GRRR, the team wanted notifications in Slack after builds, tests, or deployments complete. These messages would display the status (success/failure/cancelled) and relevant branch or tag information to keep team members informed about application state.

The team initially relied on the edge/simple-slack-notify action by Adam K Dean. However, this tool was discontinued and began issuing deprecation warnings for NodeJS 12 and ::set-output. Rather than forking or abandoning notifications, a minimal-dependency solution was developed.

Setup Instructions

Creating a Slack App:

First, navigate to api.slack.com/apps/ and select "Create New App." After installing it in your workspace, enable Incoming Webhooks via the feature toggle. Create a webhook URL by selecting "Add New Webhook to Workspace" and choosing your target channel. Store this URL as a GitHub repository secret named SLACK_WEBHOOK_URL.

Implementation

The workflow requires a dedicated job with if: always() to ensure notifications send regardless of upstream job failures. The job must reference dependent jobs via the needs parameter.

Status Detection:

The solution uses GitHub Actions' contains() function to check needs.*.result across all dependent jobs, determining whether the workflow succeeded, failed, or was cancelled.

Message Formatting:

Three distinct notification texts are defined for different outcomes. An if-statement maps statuses to color codes ('good', 'danger', 'warning') appropriate for Slack's attachment formatting.

API Call:

A curl POST request sends JSON to the webhook URL with the status-specific text and color:

echo "{attachments: [{text: \"$text\", color: \"$color\"}]}" | curl \
    "${{ secrets.SLACK_WEBHOOK_URL }}" \
    -X "POST" \
    --header "Content-Type: application/json" \
    --data-binary @-

Originally published on norday.tech.