Notify Slack on workflow completion #641
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Notify Slack on workflow completion | |
on: | |
workflow_run: | |
workflows: | |
- "*" | |
branches: | |
- main | |
types: [completed] | |
jobs: | |
notify-slack: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Get Slack member IDs | |
id: get-slack-id | |
if: github.event.workflow_run.conclusion != 'success' | |
run: | | |
SLACK_ID=$(jq -r --arg GITHUB_USER "${{ github.actor }}" '.[$GITHUB_USER] // "Unknown"' .github/slack-users.json) | |
echo "slack_id=$SLACK_ID" >> $GITHUB_OUTPUT | |
- name: Set job-specific variables | |
id: set-variables | |
run: | | |
if [[ "${{ github.event.workflow_run.conclusion }}" == "success" ]]; then | |
echo "status_emoji=:white_check_mark:" >> $GITHUB_OUTPUT | |
echo "view_text=View Success" >> $GITHUB_OUTPUT | |
echo "footer=Congratulations! 🎉" >> $GITHUB_OUTPUT | |
else | |
echo "status_emoji=:fire:" >> $GITHUB_OUTPUT | |
echo "view_text=View Failure" >> $GITHUB_OUTPUT | |
echo "footer=Culprit: <@${{ steps.get-slack-id.outputs.slack_id }}>" >> $GITHUB_OUTPUT | |
fi | |
- uses: ravsamhq/notify-slack-action@v2 | |
with: | |
status: ${{ github.event.workflow_run.conclusion }} | |
notification_title: "${{github.event.workflow_run.name}} - ${{github.event.workflow_run.conclusion}} on ${{github.event.workflow_run.head_branch}} - <${{github.server_url}}/${{github.repository}}/actions/runs/${{github.event.workflow_run.id}}|${{ steps.set-variables.outputs.view_text }}>" | |
message_format: "${{ steps.set-variables.outputs.status_emoji }} *${{github.event.workflow_run.name}}* ${{github.event.workflow_run.conclusion}} in <${{github.server_url}}/${{github.repository}}/${{github.event.workflow_run.head_branch}}|${{github.repository}}>" | |
footer: ${{ steps.set-variables.outputs.footer }} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |