Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Gihub Actions Slack Alerting #445

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/fail-alert.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Simple failure notification reusable workflow

name: Failure Notification
on:
workflow_call:
secrets:
slack_webhook:
required: true

jobs:
send_notification:
name: Send Failure Notification
runs-on: ubuntu-latest
env:
CI_BUILD_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
steps:

- name: Debug
run: |
echo "Github Action ${{ github.workflow }} Failed"
echo "on ${{ github.repository }} Repo"
echo "Tell ${{ github.triggering_actor }}"
echo -n "Web hook ends with: "
echo ${{ secrets.slack_webhook }} | tail -c 4

- name: Send Slack
uses: slackapi/[email protected]
env:
SLACK_WEBHOOK_URL: ${{ secrets.slack_webhook }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
with:
payload: |
{
"text": "GH Action ${{ github.workflow}} Failed on ${{ github.repository }}. Tell ${{ github.triggering_actor }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Workflow Failed"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "On '${{ github.repository }}'\nworkflow '${{ github.workflow }}' failed\nTell ${{ github.triggering_actor }}.\nSee ${{ env.CI_BUILD_URL }}"
}
}
]
}
fail:
name: Don't mask failure
runs-on: ubuntu-latest
steps:
- run: exit 1
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ Checks if there is a valid `helmfile.yaml` in the working directory. Executes `h

#### [`trufflehog-actions-scan`](trufflehog-actions-scan)

Runs Trufflehog as a GitHub Action. Based off of [`https://github.com/edplato/trufflehog-actions-scan`](https://github.com/edplato/trufflehog-actions-scan). Uses Dependabot to stay up-to-date with the latest version.
Runs Trufflehog as a GitHub Action. Based off of [`https://github.com/edplato/trufflehog-actions-scan`](https://github.com/edplato/trufflehog-actions-scan). Uses Dependabot to stay up-to-date with the latest version.

#### [`fail-alert`](./fail-alert.md)

Reusable workflow, not an action. For when you want a slack alert to notify the person who triggeredthe build when it fails


48 changes: 48 additions & 0 deletions fail-alert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Fail Alert

Sends a slack alert when a job fails

The alert contains the name of the repo, the workflow and the GitHub user who triggered the
workflow.

Anyone who triggers workflows should add their GitHub username as a (keyword)[https://slack.com/help/articles/201355156-Configure-your-Slack-notifications#keyword-notifications] in Slack.

1. Add any jobs that you want to monitor.
2. Add a new job.
3. It must `needs` any jobs you want to monitor. Needs can accept an array.
4. Set `if: ${{ failure() }}` on the new job.
5. Make the new job `uses: iStreamPlanet/github-actions/.github/workflows/fail-alert.yml@main`
6. fail-alert has no inputs, but does require a `secrets.slack_webhook` for whichever slack channel you want to message.

Here's an example workflow

```
name: Project X

on:
push

jobs:
build:
runs-on: ubuntu-latest
steps
- name: Something Fails!
run: |
exit 1

notify:
needs: build
if: ${{ failure() }}
uses: iStreamPlanet/github-actions/.github/workflows/fail-alert.yml@main
secrets:
slack_webhook: ${{ secrets.DEPLOYMENTS_SLACK_WEBHOOK }}
```

## Future Improvements

* Get an org-wide webhook and call it from within fail-alert. Users wouldn't need to have one.
* Accept optional inputs to override values from github. E.G. workflow name, contact etc...
* Better slack message formatting with Block Kit.
* Somehow map gh users to slack name and do a real @
* Make an actual slack app that looks it up on a table or something
* Have users put thier slack @handle in gh profile and look that up somehow?
Loading