feat(workflow): add Slack notifications for build start, success, and failure [skip-build] (#12)
Browse files
.github/workflows/build-and-commit.yml
CHANGED
|
@@ -39,6 +39,19 @@ jobs:
|
|
| 39 |
- name: Show disk usage
|
| 40 |
run: df -h
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
- name: Checkout repository
|
| 43 |
uses: actions/checkout@v4
|
| 44 |
with:
|
|
@@ -83,3 +96,25 @@ jobs:
|
|
| 83 |
env:
|
| 84 |
HEAD_REF: ${{ github.head_ref || github.ref }}
|
| 85 |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
- name: Show disk usage
|
| 40 |
run: df -h
|
| 41 |
|
| 42 |
+
- name: Notify build start on Slack
|
| 43 |
+
id: slack_start
|
| 44 |
+
run: |
|
| 45 |
+
msg="*Build started* for \`${{ github.repository }}\`\nBranch: \`${{ github.ref_name }}\`\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Workflow>"
|
| 46 |
+
response=$(curl -s -X POST \
|
| 47 |
+
-H "Authorization: Bearer ${{ secrets.SLACK_TOKEN }}" \
|
| 48 |
+
-H "Content-type: application/json; charset=utf-8" \
|
| 49 |
+
--data "{\"channel\":\"${{ secrets.SLACK_CHANNEL_ID }}\",\"text\":\"$msg\"}" \
|
| 50 |
+
https://slack.com/api/chat.postMessage)
|
| 51 |
+
ts=$(echo "$response" | jq -r '.ts')
|
| 52 |
+
echo "thread_ts=$ts" >> "$GITHUB_OUTPUT"
|
| 53 |
+
echo "$response"
|
| 54 |
+
|
| 55 |
- name: Checkout repository
|
| 56 |
uses: actions/checkout@v4
|
| 57 |
with:
|
|
|
|
| 96 |
env:
|
| 97 |
HEAD_REF: ${{ github.head_ref || github.ref }}
|
| 98 |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
| 99 |
+
|
| 100 |
+
- name: Notify success on Slack (thread)
|
| 101 |
+
if: success()
|
| 102 |
+
run: |
|
| 103 |
+
ts="${{ steps.slack_start.outputs.thread_ts }}"
|
| 104 |
+
msg="*Build succeeded* for \`${{ github.repository }}\`\nBranch: \`${{ github.ref_name }}\`\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Workflow>"
|
| 105 |
+
curl -s -X POST \
|
| 106 |
+
-H "Authorization: Bearer ${{ secrets.SLACK_TOKEN }}" \
|
| 107 |
+
-H "Content-type: application/json; charset=utf-8" \
|
| 108 |
+
--data "{\"channel\":\"${{ secrets.SLACK_CHANNEL_ID }}\",\"text\":\"$msg\",\"thread_ts\":\"$ts\"}" \
|
| 109 |
+
https://slack.com/api/chat.postMessage
|
| 110 |
+
|
| 111 |
+
- name: Notify failure on Slack (thread)
|
| 112 |
+
if: failure()
|
| 113 |
+
run: |
|
| 114 |
+
ts="${{ steps.slack_start.outputs.thread_ts }}"
|
| 115 |
+
msg="*Build failed* for \`${{ github.repository }}\`\nBranch: \`${{ github.ref_name }}\`\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Workflow>"
|
| 116 |
+
curl -s -X POST \
|
| 117 |
+
-H "Authorization: Bearer ${{ secrets.SLACK_TOKEN }}" \
|
| 118 |
+
-H "Content-type: application/json; charset=utf-8" \
|
| 119 |
+
--data "{\"channel\":\"${{ secrets.SLACK_CHANNEL_ID }}\",\"text\":\"$msg\",\"thread_ts\":\"$ts\"}" \
|
| 120 |
+
https://slack.com/api/chat.postMessage
|