Skip to content

Commit

Permalink
feat: slack workflow conclusion notifications (#160)
Browse files Browse the repository at this point in the history
* feat: slack workflow conclusion notifications

* fix: optional header

* chore: default message

* fix: add workflow number

* fix: text
  • Loading branch information
adamdehaven authored Sep 27, 2024
1 parent ecbcd70 commit 33942dd
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions slack-actions/workflow-notification/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Post Slack workflow conclusion notifications
description: Post a Slack message with the workflow conclusion via Slack incoming webhook.
inputs:
# REQUIRED INPUTS
slack-webhook-url:
description: The Slack webhook URL.
required: true
status:
description: The status of the workflow, one of "success" or "failure".
required: true
# OPTIONAL INPUTS
header:
description: The notification header, as a plain text string. Defaults to the workflow name.
required: false
success-message:
description: The message to display when the workflow is successful. Accepts markdown syntax.
required: false
default: ":large_green_circle: Workflow completed successfully :mario_luigi_dance:"
failure-message:
description: The message to display when the workflow is successful. Accepts markdown syntax.
required: false
default: ":red_circle: Workflow failed :sad-panda:"

runs:
using: composite
steps:
- name: Construct Slack variables
id: slack-variables
shell: bash
run: |
# Git Variables
fallbackBranchName=$(echo "${{ github.ref }}" | cut -c12-)
shortCommitHash=$(echo "${{ github.sha }}" | cut -c1-7)
# Output All Variables
echo "fallback-branch-name=${fallbackBranchName}" >> $GITHUB_OUTPUT
echo "short-commit-hash=${shortCommitHash}" >> $GITHUB_OUTPUT
# Echo all variables for debugging
echo "fallback-branch-name=${fallbackBranchName}"
echo "short-commit-hash=${shortCommitHash}"
- name: Construct Slack payload
id: slack-payload
shell: bash
run: |
PAYLOAD=$(cat << EOF
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ inputs.header || github.workflow }}",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${{ inputs.status == 'failure' && inputs.status-message || inputs.failure-message }}"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n<${{ github.server_url }}/${{ github.repository }}|${{ github.repository }}>"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n<${{ github.server_url }}/${{ github.repository }}/tree/${{ github.head_ref || steps.slack-variables.outputs.fallback-branch-name }}|${{ github.head_ref || steps.slack-variables.outputs.fallback-branch-name }}>"
},
{
"type": "mrkdwn",
"text": "*Workflow Run Number:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.run_number }}>"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ steps.slack-variables.outputs.short-commit-hash }}>"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"action_id": "view-workflow-run",
"style": "${{ inputs.status == 'failure' && 'danger' || 'primary' }}",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"text": {
"type": "plain_text",
"emoji": true,
"text": "${{ inputs.status == 'failure' && 'View failed run' || 'View successful run' }}"
}
}
]
}
]
}
EOF
)
echo "payload<<EOF" >> $GITHUB_OUTPUT
echo "$PAYLOAD" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Send notification
uses: slackapi/slack-github-action@v1
env:
SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook-url }}
SLACK_WEBHOOK_TYPE: 'INCOMING_WEBHOOK'
with:
payload: ${{ steps.slack-payload.outputs.payload }}

0 comments on commit 33942dd

Please sign in to comment.