Send custom slack messages given a slack block template
- uses: Brymastr/slack-action@v1
name: Slack notification
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
with:
template: './template.json'
example_key1: example value 1
job_status: ${{ job.status }}
template.json
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Example: {{ example_key1 }} and {{ job_status }}"
}
}
]
}
Use of this action requires a GitHub secret called SLACK_WEBHOOK
. The value should include the channel id and look something like one of these:
https://hooks.slack.com/services/11111ZZZZ/22222XXXX/1a2B3c4e5F6G7h8I9j0k
11111ZZZZ/22222XXXX/1a2B3c4e5F6G7h8I9j0k
See Creating and using encrypted secrets for assistance with GitHub Secrets.
See Sending messages using Incoming Webhooks for assistance with Slack Webhooks.
SLACK_WEBHOOK
- provided as anenv
variable, this is the full webhook url for your bot channel.template
- provided in thewith
args, is the path to the message template json file. This path should be relative from the project root.
Any other custom arguments can be provided and will be included in the template. From the example above all occurrences of {{ example_key1 }}
in the template.json
file will be replace with example value 1
.
NOTE: template names in the json file must be lower case.
Most github
context properties are included by default without needing to pass them in through a with
argument. Some properties have been renamed to make a bit more sense. See Contexts for more info.
Property Name | Replacement Variable |
---|---|
github.sha |
commit |
github.repository |
repo |
github.ref |
ref |
github.head_ref |
head_ref |
github.base_ref |
base_ref |
github.actor |
actor |
github.event_name |
event |
github.workflow |
workflow |
In addition to these github context properties there are 2 computed properties that could be useful
Replacement Variable | Description |
---|---|
repo_url |
the URL of the github repo |
actions_url |
the URL of the github actions workflow run for the commit |
Check out the examples directory for some examples
Try the Slack Block Kit Builder for message template inspiration
Slack notifications are a common feature in automated build pipelines. There are already a few existing GitHub Actions that send slack messages but most use a predetermined format and only allow for title or subject customization. This action allows for full block customization. I would like this action to serve as a simple, solid, and customizable slack notification action with as few dependencies as possible.
- abinoda/slack-action - works by providing the slack block body as stringified JSON directly in the workflow yaml (messy)
- homoluctus/slatify
- rtCamp/action-slack-notify
- 8398a7/action-slack
- pullreminders/slack-action - simpler, just provide json stringified message content
- krider2010/slack-bot-action - using the old HCL syntax
- apex/actions/slack - using the old HCL syntax
- ivanklee86/xunit-slack-reporter - not exactly the same purpose