Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/actions-ecosystem/action-slack-notifier

🔔 GitHub Action to send notifications to Slack
https://github.com/actions-ecosystem/action-slack-notifier

actions github notifications slack

Last synced: 26 days ago
JSON representation

🔔 GitHub Action to send notifications to Slack

Awesome Lists containing this project

README

        

# Action Slack Notifier

[![actions-workflow-test][actions-workflow-test-badge]][actions-workflow-test]
[![release][release-badge]][release]
[![license][license-badge]][license]

![screenshot](./docs/assets/screenshot-pull-request.png)

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](./docs/assets/screenshot-slack-create-app.png)

2. Add a permission to the app

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

![screenshot](./docs/assets/screenshot-slack-add-permission.png)

3. Install the app

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

![screenshot](./docs/assets/screenshot-slack-install-app.png)

4. 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](https://api.slack.com/tools/block-kit-builder) helps you to build a JSON payload for this.

### Behaviors

#### `color: green` `verbose: true`

![screenshot](./docs/assets/screenshot-color-verbose.png)

#### `color: ''` `verbose: true`

![screenshot](./docs/assets/screenshot-verbose-no-color.png)

#### `color: green` `verbose: false`

![screenshot](./docs/assets/screenshot-color-no-verbose.png)

#### `color: ''` `verbose: false`

![screenshot](./docs/assets/screenshot-no-color-no-verbose.png)

## Color Palettes

Black

![screenshot](./docs/assets/screenshot-color-black.png)

Red

![screenshot](./docs/assets/screenshot-color-red.png)

Green

![screenshot](./docs/assets/screenshot-color-green.png)

Yellow

![screenshot](./docs/assets/screenshot-color-yellow.png)

Blue

![screenshot](./docs/assets/screenshot-color-blue.png)

Magenta

![screenshot](./docs/assets/screenshot-color-magenta.png)

Cyan

![screenshot](./docs/assets/screenshot-color-cyan.png)

White

![screenshot](./docs/assets/screenshot-color-white.png)

## Example

### Simple

```yaml
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](./docs/assets/screenshot-example-failure.png)

Configuration

```yaml
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](./docs/assets/screenshot-example-mention-github.png)
![screenshot](./docs/assets/screenshot-example-mention-slack.png)

Configuration

```yaml
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](./docs/assets/screenshot-example-labeled.png)

Configuration

```yaml
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](./docs/assets/screenshot-example-custom-payload.png)

Configuration

```yaml
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 "
}
},
{
"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 "
}
]
}
]
}
```

## License

Copyright 2020 The Actions Ecosystem Authors.

Action Slack Notifier is released under the [Apache License 2.0](./LICENSE).

[actions-workflow-test]: https://github.com/actions-ecosystem/action-slack-notifier/actions?query=workflow%3ATest
[actions-workflow-test-badge]: https://img.shields.io/github/workflow/status/actions-ecosystem/action-slack-notifier/Test?label=Test&style=for-the-badge&logo=github

[release]: https://github.com/actions-ecosystem/action-slack-notifier/releases
[release-badge]: https://img.shields.io/github/v/release/actions-ecosystem/action-slack-notifier?style=for-the-badge&logo=github

[license]: LICENSE
[license-badge]: https://img.shields.io/github/license/actions-ecosystem/action-slack-notifier?style=for-the-badge