Skip to content

Commit 12ed4b3

Browse files
committed
feat(workflow): add Slack deployment summary notification
1 parent d2eb997 commit 12ed4b3

1 file changed

Lines changed: 82 additions & 1 deletion

File tree

.github/workflows/fly-deployment.yml

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,78 @@ jobs:
422422
projects-path: ${{ runner.temp }}/fly-projects/fly-projects.json
423423
token: ${{ steps.generate-token.outputs.token }}
424424

425+
notify:
426+
needs: [pre-deploy, deploy]
427+
if: >-
428+
always() &&
429+
needs.deploy.result != 'skipped'
430+
runs-on: ubuntu-latest
431+
steps:
432+
- name: Resolve PR number
433+
id: resolve-pr
434+
env:
435+
WR_PRS: ${{ toJSON(github.event.workflow_run.pull_requests) }}
436+
DIRECT_PR: ${{ github.event.number }}
437+
run: |
438+
PR=$(echo "$WR_PRS" | jq -r 'first | .number // empty')
439+
echo "number=${PR:-$DIRECT_PR}" >> "$GITHUB_OUTPUT"
440+
441+
- name: Download projects artifact
442+
uses: actions/download-artifact@v4
443+
with:
444+
name: fly-deployed-projects-${{ github.run_id }}
445+
path: ${{ runner.temp }}/fly-projects
446+
continue-on-error: true
447+
448+
- name: Build Slack payload
449+
id: build-payload
450+
env:
451+
ENVIRONMENT: ${{ needs.pre-deploy.outputs.environment }}
452+
DEPLOY_RESULT: ${{ needs.deploy.result }}
453+
PR_NUMBER: ${{ steps.resolve-pr.outputs.number }}
454+
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
455+
PROJECTS_FILE: ${{ runner.temp }}/fly-projects/fly-projects.json
456+
run: |
457+
STATUS_EMOJI=$([ "$DEPLOY_RESULT" = "success" ] && echo "✅" || echo "❌")
458+
ENV_LABEL=$([ "$ENVIRONMENT" = "production" ] && echo "Production" || echo "Preview")
459+
PR_SUFFIX=$([ -n "$PR_NUMBER" ] && echo " · PR #${PR_NUMBER}" || echo "")
460+
HEADER="${STATUS_EMOJI} ${ENV_LABEL} Deployment${PR_SUFFIX}"
461+
462+
APPS_TEXT=""
463+
if [ -f "$PROJECTS_FILE" ]; then
464+
APPS_TEXT=$(jq -r '
465+
.[] |
466+
if .action == "deploy" then "✅ *" + .name + "* — <" + .url + "|" + .url + ">"
467+
elif .action == "failed" then "❌ *" + .appOrProject + "* — " + .error
468+
else empty
469+
end
470+
' "$PROJECTS_FILE")
471+
fi
472+
473+
PAYLOAD=$(jq -nc \
474+
--arg header "$HEADER" \
475+
--arg apps "$APPS_TEXT" \
476+
--arg run_url "$RUN_URL" \
477+
'{
478+
"blocks": (
479+
[{"type":"header","text":{"type":"plain_text","text":$header,"emoji":true}}]
480+
+ (if ($apps | length) > 0 then [{"type":"section","text":{"type":"mrkdwn","text":$apps}}] else [] end)
481+
+ [{"type":"context","elements":[{"type":"mrkdwn","text":("<" + $run_url + "|View workflow run>")}]}]
482+
)
483+
}')
484+
485+
echo "payload<<PAYLOAD_EOF" >> "$GITHUB_OUTPUT"
486+
echo "$PAYLOAD" >> "$GITHUB_OUTPUT"
487+
echo "PAYLOAD_EOF" >> "$GITHUB_OUTPUT"
488+
489+
- name: Send Slack notification
490+
uses: slackapi/slack-github-action@v2
491+
with:
492+
webhook: ${{ secrets.SLACK_WEBHOOK }}
493+
webhook-type: incoming-webhook
494+
errors: true
495+
payload: ${{ steps.build-payload.outputs.payload }}
496+
425497
destroy:
426498
needs: pre-deploy
427499
if: >-
@@ -468,7 +540,16 @@ jobs:
468540
token: ${{ steps.generate-token.outputs.token }}
469541

470542
summary:
471-
needs: [analyze-conditions, pre-deploy, build, deploy, destroy, pr-comment]
543+
needs:
544+
[
545+
analyze-conditions,
546+
pre-deploy,
547+
build,
548+
deploy,
549+
destroy,
550+
pr-comment,
551+
notify
552+
]
472553
if: always()
473554
runs-on: ubuntu-latest
474555
steps:

0 commit comments

Comments
 (0)