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

Conversation

camscale
Copy link
Contributor

Add a composite action to send CI errors to slack so that workflow
failures can be reported to slack. To use it, add a step to your
workflow:

- name: Slack on error
  if: failure()
  uses: gravitational/shared-workflows/.github/actions/slack-on-error@main
  with:
    slack-token: ${{ secrets.CI_NOTIFIER_SLACK_TOKEN }} # or other token you have
    channel-id: C1234567890
    workflow-description: called-workflow extra identifiers

Issue: gravitational/teleport#20647
Issue: gravitational/teleport#20729

@camscale camscale requested review from a team December 11, 2023 07:16
Add a composite action to send CI errors to slack so that workflow
failures can be reported to slack. To use it, add a step to your
workflow:

    - name: Slack on error
      if: failure()
      uses: gravitational/shared-workflows/.github/actions/slack-on-error@main
      with:
        slack-token: ${{ secrets.CI_NOTIFIER_SLACK_TOKEN }} # or other token you have
        channel-id: C1234567890
        workflow-description: called-workflow extra identifiers
@camscale camscale force-pushed the camh/add-slack-action branch from 4423b1f to 1946f6a Compare December 11, 2023 07:32
.github/actions/slack-on-error/action.yaml Show resolved Hide resolved
Comment on lines +53 to +65
{
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"
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

Comment on lines +1 to +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
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"

- 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 }}"
}
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@camscale
Copy link
Contributor Author

I think this action is a little too early in development to go into this repository with the requirements imposed here. I'm going to close this and iterate on the action in the teleport.e repo, which is the only one that needs this right now.

@camscale camscale closed this Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants