evryfs / notify-slack-action

GitHub Action for posting fancy Slack messages
MIT License
12 stars 2 forks source link
chat continuous-integration github-actions js nodejs slack

:zap: Notify Slack Action

Forked from abinoda/slack-action

:nail_care: Send fancy message to a Slack channel, group or DM using Slack bot tokens.

Note: To use this GitHub Action you'll first need to create a Slack App and install it to your Slack workspace.

:rocket: Features:

:information_source: Parameters

Parameters described below are matching the latest version tag. You can find available action's tags here

Parameter Required Type Description
SLACK_BOT_TOKEN x secret Slack bot token auth
channel-id x string Channel where message will be posted
value x string Value depending on the mechanism chosen. Details below
mechanism x string Mechanism to use when notify a slack channel. Either 'text', 'blocks' or 'templates'

:octocat: Usage

- name: Notify Slack
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} # required
  uses: evryfs/notify-slack-action@<tag>
  with:
    channel-id: <slack-channel-id> # required
    value: <text-to-post> # required
    mechanism: <mechanism> # optional. Defaults to 'text'

Using text mechanism

- name: Notify Slack using text
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} # required
  uses: evryfs/notify-slack-action@latest
  with:
    channel-id: C04DFAHCTS5
    mechanism: text
    value: Hello GitHub Action!

Slack message would looks like:

Using templates mechanism

Templates mechanism are pre-defined blocks messages that contains (valuable) generics information. They are defined in a orphan branch named templates. Templates can be added by creating a pull request to this branch. More information here.

Override template variable

Templates are using environments variables to generate information, that are called templates variables.

For instance, gh_dashboard_simple.json contains template variable $VERSION_TAG, $ACTION_STATUS or even $GITHUB_REF_NAME. Those variables are intended to be override either by the system (ie. github runner envs) or by the developer using env: at the workflow/job/step level.

- name: Notify Slack using templates
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} # required
    VERSION_TAG: ${{ inputs.version-tag }} # NOTE github context can be used to retrieve inputs/outputs/context
    ACTION_STATUS: "success :thumbsup:" # NOTE Slack 'mrkdwn' format is supported
    TEXT_MESSAGE: "Build of notify-slack-action has been done successfully!" # Override the template variable $VERSION_TAG
  uses: evryfs/notify-slack-action@latest
  with:
    channel-id: C04DFAHCTS5
    mechanism: templates
    value: gh_dashboard_simple.json # Load template gh_dhasboard_simple.json

By loading the gh_dashboard_simple.json template, Slack message would looks like:

Note: templates variables depend of the selected template. A template might not have any template variable. You must take a look at the template's guideline (or source code).

Using blocks mechanism

It is possible to personnalise a message by using Slack Block Kit without submitting a template to evryfs/notify-slack-action. You can use Slack Block Kit Builder to easily construct a message layout and generate its JSON code.

Value must be an JSON array!

- name: Notify Slack using blocks
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} # required
    VERSION_TAG: ${{ inputs.version-tag }} # NOTE github context can be used to retrieve inputs/outputs/context
    ACTION_STATUS: "success :thumbsup:" # NOTE Slack 'mrkdwn' format is supported
    TEXT_MESSAGE: "Build of notify-slack-action has been done successfully!" # Override the template variable $VERSION_TAG
  uses: evryfs/notify-slack-action@latest
  with:
    channel-id: C04DFAHCTS5
    mechanism: blocks
    value: | # Make sure to use | in order to parse JSON text
      [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "*Anything humans can do in space, robots can do better.*\n${{ github.run_id }}"
          }
        },
        {
          "type": "actions",
          "elements": [
            {
              "type": "button",
              "text": {
                "type": "plain_text",
                "emoji": true,
                "text": "Approve"
              },
              "style": "primary",
              "value": "yes"
            },
            {
              "type": "button",
              "text": {
                "type": "plain_text",
                "emoji": true,
                "text": "Deny"
              },
              "style": "danger",
              "value": "no"
            }
          ]
        }
      ]

Slack message would looks like:

:book: References

Note: A "channel ID" can be the ID of a channel, private group, or user you would like to post a message to. Your bot can message any user in your Slack workspace but needs to be invited into channels and private groups before it can post to them.

:memo: License

The Dockerfile and associated scripts and documentation in this project are released under the MIT License.