|
| 1 | +# Notify Slack on Failure Action |
| 2 | + |
| 3 | +This GitHub Action sends a Slack notification summarizing failed jobs in a workflow run when used with an `if: failure()` condition. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +The action posts a concise message to a Slack channel containing: |
| 8 | +1. A link to the failed GitHub Actions run |
| 9 | +2. A list of failed jobs provided by the workflow |
| 10 | +3. A custom project-branded username and icon |
| 11 | + |
| 12 | +It is intended to be used as the last step (or in a dedicated job) guarded by `if: failure()` so that it only triggers on failures. |
| 13 | + |
| 14 | +## Dependencies |
| 15 | + |
| 16 | +This action depends on: |
| 17 | +- [SonarSource/vault-action-wrapper](https://github.com/SonarSource/vault-action-wrapper) to retrieve the Slack token from Vault |
| 18 | +- [rtCamp/action-slack-notify](https://github.com/rtCamp/action-slack-notify) to send the Slack message |
| 19 | + |
| 20 | +## Inputs |
| 21 | + |
| 22 | +| Input | Description | Required | Default | |
| 23 | +|-----------------|------------------------------------------------------------------------|----------|-----------| |
| 24 | +| `project-name` | The display name of the project; used in the Slack username. | Yes | - | |
| 25 | +| `icon` | Emoji icon for the Slack message (Slack emoji code). | No | `:alert:` | |
| 26 | +| `slack-channel` | Slack channel (without `#`) to post the notification into. | Yes | - | |
| 27 | +| `jobs` | Comma-separated list of job names to report as failed (provided by you). | Yes | - | |
| 28 | + |
| 29 | +Note: The list of failed jobs must be provided via the `jobs` input by your workflow logic. |
| 30 | + |
| 31 | +## Outputs |
| 32 | + |
| 33 | +No outputs are produced by this action. |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### Basic usage (in a dedicated failure notification job) |
| 38 | + |
| 39 | +```yaml |
| 40 | +jobs: |
| 41 | + notify_on_failure: |
| 42 | + needs: [ build, test, deploy ] # Example job dependencies |
| 43 | + runs-on: ubuntu-latest |
| 44 | + if: failure() |
| 45 | + permissions: |
| 46 | + statuses: read |
| 47 | + id-token: write |
| 48 | + steps: |
| 49 | + - uses: SonarSource/release-github-actions/notify-slack@v1 |
| 50 | + with: |
| 51 | + project-name: 'My Project' |
| 52 | + slack-channel: 'engineering-alerts' |
| 53 | + jobs: ${{ toJSON(needs) }} |
| 54 | +``` |
| 55 | +
|
| 56 | +### Minimal usage (only required inputs) |
| 57 | +
|
| 58 | +```yaml |
| 59 | +- uses: SonarSource/release-github-actions/notify-slack@v1 |
| 60 | + if: failure() |
| 61 | + with: |
| 62 | + project-name: 'My Project' |
| 63 | + slack-channel: 'engineering-alerts' |
| 64 | + jobs: 'build, test' |
| 65 | +``` |
| 66 | +
|
| 67 | +### Custom icon |
| 68 | +
|
| 69 | +```yaml |
| 70 | +- uses: SonarSource/release-github-actions/notify-slack@v1 |
| 71 | + if: failure() |
| 72 | + with: |
| 73 | + project-name: 'My Project' |
| 74 | + slack-channel: 'engineering-alerts' |
| 75 | + icon: ':rocket:' |
| 76 | + jobs: 'build, test' |
| 77 | +``` |
| 78 | +
|
| 79 | +## Implementation Details |
| 80 | +
|
| 81 | +The action is a composite action that: |
| 82 | +- Fetches `SLACK_TOKEN` from Vault path `development/kv/data/slack` using `vault-action-wrapper` |
| 83 | +- Uses a workflow-provided `jobs` input (comma-separated string) to populate the "Failed Jobs" section |
| 84 | +- Constructs a Slack message with run URL and the provided failed jobs list |
| 85 | +- Uses `rtCamp/action-slack-notify` to send a minimal styled message (no title/footer) with danger color |
| 86 | +- Sets Slack username to: `<project-name> CI Notifier` |
| 87 | +- If the `jobs` list is empty or not provided, the "Failed Jobs" line will be blank |
| 88 | + |
| 89 | +## Prerequisites |
| 90 | + |
| 91 | +- Vault policy granting access to `development/kv/data/slack` must be configured for the repository. |
| 92 | +- The secret at that path must contain a key named `token`. |
| 93 | +- The Slack channel provided must exist and the token must have permission to post there. |
| 94 | + |
| 95 | +## Notes |
| 96 | + |
| 97 | +- Use `if: failure()` on the step or job; otherwise it will also fire on success. |
| 98 | +- `project-name`, `slack-channel`, and `jobs` are required inputs (no defaults). |
| 99 | +- Do not prefix the channel with `#`. |
| 100 | +- Message formatting is intentionally minimal for quick triage in alert-focused channels. |
| 101 | +- How you compute the failed jobs list is up to your workflow; you can use prior steps to gather job results and pass them to this action via `jobs`. |
0 commit comments