/action-slack-notifier

🔔 GitHub Action to send notifications to Slack

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Action Slack Notifier

actions-workflow-test release license

screenshot

This is a GitHub Action to send notifications to Slack on general purpose.

This action is designed to focus on sending notifications, so you can flexibly customize your workflow with this action. For example, sending a message to you when a job status changes, you get a comment in an issue, a label is added to your pull request, and so on.

It would be more useful to use this with other GitHub Actions' outputs.

Prerequisites

Before getting started, let's create a Slack app!

This action requires the permission chat:write or optionally chat:write.customize. If you want to change the icon for a message, choose chat:write.customize.

If you're not familiar with creating a Slack app, see the guide below.

Slack App Setup
  1. Create a Slack app

Visit https://api.slack.com/apps and then create an app in your workspace.

screenshot

  1. Add a permission to the app

Visit https://api.slack.com/apps/<YOUR_APP_ID>/oauth and then add a permission to your app.

screenshot

  1. Install the app

Visit https://api.slack.com/apps/<YOUR_APP_ID>/install-on-team and then install your app in your workspace.

screenshot

  1. Add the app to a channel

Open a Slack channel and add the app.

Inputs

NAME DESCRIPTION TYPE REQUIRED DEFAULT
slack_token A Slack token. string true N/A
channel A channel that will receives the message. e.g.) develop, #develop string true N/A
message A message for the channel. Supports Markdown format. string true N/A
username An username who sends a message. string false GitHub Actions
color A color of a message. The color names {black, red, green, yellow, blue, magenta, cyan, white} and color code (e.g., #4CAF50) are available. The default is no-color. string false N/A
verbose Whether message contains GitHub context: repository, ref, workflow, event, action, number bool false false
unfurl Whether to unfurl links and media in a message. bool false true
custom_payload A custom payload, in the form of JSON of a Slack block array, overriding the whole message. If this is specified, inputs.color and inputs.verbose are ignored. string false N/A

inputs.custom_payload allows advanced users to send any form of message. Block Kit Builder helps you to build a JSON payload for this.

Behaviors

color: green verbose: true

screenshot

color: '' verbose: true

screenshot

color: green verbose: false

screenshot

color: '' verbose: false

screenshot

Color Palettes

Black

screenshot

Red

screenshot

Green

screenshot

Yellow

screenshot

Blue

screenshot

Magenta

screenshot

Cyan

screenshot

White

screenshot

Example

Simple

name: Notify push

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions-ecosystem/action-slack-notifier@v1
        with:
          slack_token: ${{ secrets.SLACK_TOKEN }}
          message: |
            @${{ github.actor }} pushed commits.
          channel: develop

Send a notification when the previous job fails

screenshot

Configuration
name: Test

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: "12.x"
      - run: yarn install
      - run: yarn test
      - uses: actions-ecosystem/action-slack-notifier@v1
        if: ${{ failure() }}
        with:
          slack_token: ${{ secrets.SLACK_TOKEN }}
          message: |
            @${{ github.actor }} test failed.
          channel: develop
          color: red # optional
          verbose: true # optional

Propagate mentions from GitHub to Slack

screenshot screenshot

Configuration
name: Propagate Mentions

on:
  issue_comment:
    types:
      - created

jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions-ecosystem/action-regex-match@v2
        id: regex-match
        with:
          regex: '^\/cc(( +@[-\w]+)+)\s*$'
          text: ${{ github.event.comment.body }}
          flags: 'gm'

      - uses: actions-ecosystem/action-slack-notifier@v1
        if: ${{ steps.regex-match.outputs.match != '' }}
        with:
          slack_token: ${{ secrets.SLACK_TOKEN }}
          message: |
            ${{ steps.regex-match.outputs.match }}
          channel: develop
          color: blue # optional
          verbose: true # optional

Send a notification when a specific label is added

screenshot

Configuration
name: Notify Labeled

on:
  issues:
    types:
      - labeled

jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions-ecosystem/action-slack-notifier@v1
        if: ${{ github.event.label.name == 'help wanted' }}
        with:
          slack_token: ${{ secrets.SLACK_TOKEN }}
          message: |
            `${{ github.event.label.name }}` label has been added.
          channel: develop
          color: blue # optional
          verbose: true # optional

Send a custom payload

screenshot

Configuration
name: Send Custom Payload

on: push

jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions-ecosystem/action-slack-notifier@v1
        with:
          slack_token: ${{ secrets.SLACK_TOKEN }}
          channel: develop
          message: 'This text is for notifications.'
          custom_payload: |
            {
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "> message *with some bold text* and _some italicized text_."
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>"
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "plain_text",
                    "text": "This is a plain text section block.",
                    "emoji": true
                  }
                },
                {
                  "type": "context",
                  "elements": [
                    {
                      "type": "mrkdwn",
                      "text": "For more info, contact <support@acme.inc>"
                    }
                  ]
                }
              ]
            }

License

Copyright 2020 The Actions Ecosystem Authors.

Action Slack Notifier is released under the Apache License 2.0.