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

Add slack-on-error shared action #197

Closed
wants to merge 1 commit into from
Closed
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
81 changes: 81 additions & 0 deletions .github/actions/slack-on-error/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# This is a GitHub Action to report an error to Teleport internal slack.

name: Slack on Error
description: Report CI errors to a slack channel

inputs:
slack-token:
description: Slack bot token.
required: true
channel-id:
description: ID of channel to send error to.
default: 'C052S7U6SR1' # #drone-alerts channel. Override for desired channel
wadells marked this conversation as resolved.
Show resolved Hide resolved
workflow-description:
description: |
Description of workflow that failed. GitHub makes available only the
name of the top-level workflow that was invoked. To provide more context
for the error, provide a space-separated list of names that qualify the
error. e.g. "build-linux amd64 FIPS". The first name should be the name
of the workflow file (without extension) if it is a called workflow.
Subsequent names are usually input parameters to the called workflow to
identify what was being built.

runs:
using: composite
steps:
- name: Validate inputs
uses: actions/github-script@v6
env:
INPUT_SLACK-TOKEN: ${{ inputs.slack-token }}
with:
script: |
core.getInput("slack-token", {required: true})

- name: Format message
id: format
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
SERVER_URL: ${{ github.server_url }}
REPOSITORY: ${{ github.repository }}
RUN_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
TOP_WORKFLOW: ${{ github.workflow_ref }}
WORKFLOW_DESCRIPTION: ${{ inputs.workflow-description }}
AUTHOR: ${{ github.event.head_commit.author.username }}
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
COMMIT_URL: ${{ github.event.head_commit.url }}
SHA: ${{ github.sha }}
run: |
top_wfname="${TOP_WORKFLOW%@*}" # strip ref from end
top_wfname="${top_wfname##*/}" # strip path to workflow
top_wfname="${top_wfname%.*}" # strip extension
{
echo 'message<<EOF'
printf '✘ <%s|*Failed*>: ' "${RUN_URL}"
printf '`%s` ' "${top_wfname}"
read -ra desc <<< "${WORKFLOW_DESCRIPTION}" # split on spaces, dont glob
printf '/ `%s` ' "${desc[@]}" # format repeats for each arg in array
printf '\\n'
printf 'author: <%s|%s> ' "${SERVER_URL}/${AUTHOR}" "${AUTHOR}"
printf 'repo: <%s|%s> ' "${SERVER_URL}/${REPOSITORY}" "${REPOSITORY#gravitational/}"
printf '%s: <%s|%s> ' "${REF_TYPE}" "${SERVER_URL}/${REPOSITORY}/commits/${REF_NAME}" "${REF_NAME}"
printf 'commit: <%s|%s> ' "${COMMIT_URL}" "${SHA:0:10}"
printf '\nEOF\n'
} >> "$GITHUB_OUTPUT"
Comment on lines +53 to +65
Copy link
Contributor

@wadells wadells Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For teleport builds (specifically) I feel like we need a bit more info.

E.g. the calling teleport ref is probably more important than the teleport.e ref -- though we should probably ship both.

The workflow as is makes sense for stuff like teleport-plugins or other single repo projects.

Architecturally, perhaps we have a teleport specific slack notifier, instead of (what looks like) an agnostic/general purpose action. If we have a teleport specific action, we could probably drop the repo: section too, as those become implicit.

Spitballing:

Failed: push-build / build-mac / arm64 author: camscale
teleport branch: camh/test/slack-notify commit: 4a8448bf6f
teleport.e branch: XXXXX commit: XXXXX


- name: Send failure message to slack
uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 # v1.24.0
env:
SLACK_BOT_TOKEN: ${{ inputs.slack-token }}
with:
channel-id: ${{ inputs.channel-id }}
payload: |
{
"attachments": [
{
"color": "danger",
"text": "${{ steps.format.outputs.message }}"
}
]
}
Comment on lines +1 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading