Skip to content

Commit fee58ed

Browse files
committed
feat: Added slack notif support for both apply and destroy
1 parent ffe5de0 commit fee58ed

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

.github/workflows/terraform_workflow.yml

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,62 @@ jobs:
300300
terraform destroy -auto-approve
301301
fi
302302
303-
- name: Notify Slack
304-
if: ${{ always() }}
305-
env:
306-
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
307-
run: |
308-
if [ "${{ job.status }}" == "success" ]; then
309-
STATUS="Success ✅"
310-
COLOR="good"
311-
MESSAGE="Terraform deployment completed successfully."
312-
else
313-
STATUS="Failed ❌"
314-
COLOR="danger"
315-
MESSAGE="Terraform deployment failed. Check logs for details."
316-
fi
303+
# - name: Notify Slack
304+
# if: ${{ always() }}
305+
# env:
306+
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
307+
# run: |
308+
# if [ "${{ job.status }}" == "success" ]; then
309+
# STATUS="Success ✅"
310+
# COLOR="good"
311+
# MESSAGE="Terraform deployment completed successfully."
312+
# else
313+
# STATUS="Failed ❌"
314+
# COLOR="danger"
315+
# MESSAGE="Terraform deployment failed. Check logs for details."
316+
# fi
317+
318+
# RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
317319

318-
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
320+
# curl -X POST -H 'Content-type: application/json' \
321+
# --data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
322+
# --arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
323+
# --arg run_url "$RUN_URL" \
324+
# '{attachments: [{color: $color, title: ("Terraform Deployment: " + $status), text: $message, fields: [{title: "Repository", value: $repo, short: true}, {title: "Branch", value: $branch, short: true}, {title: "Commit", value: $sha, short: true}, {title: "Run URL", value: $run_url, short: false}]}]}')" \
325+
# $SLACK_WEBHOOK_URL
319326

320-
curl -X POST -H 'Content-type: application/json' \
321-
--data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
322-
--arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
323-
--arg run_url "$RUN_URL" \
324-
'{attachments: [{color: $color, title: ("Terraform Deployment: " + $status), text: $message, fields: [{title: "Repository", value: $repo, short: true}, {title: "Branch", value: $branch, short: true}, {title: "Commit", value: $sha, short: true}, {title: "Run URL", value: $run_url, short: false}]}]}')" \
325-
$SLACK_WEBHOOK_URL
327+
- name: Notify Slack
328+
if: ${{ always() }}
329+
env:
330+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
331+
run: |
332+
# Determine the operation type (apply or destroy) using a custom environment variable or flag
333+
if [[ "${{ inputs.destroy }}" == "true" ]]; then
334+
OPERATION="Destroy"
335+
else
336+
OPERATION="Apply"
337+
fi
338+
339+
# Check the job status
340+
if [ "${{ job.status }}" == "success" ]; then
341+
STATUS="Success ✅"
342+
COLOR="good"
343+
MESSAGE="Terraform $OPERATION completed successfully."
344+
else
345+
STATUS="Failed ❌"
346+
COLOR="danger"
347+
MESSAGE="Terraform $OPERATION failed. Check logs for details."
348+
fi
349+
350+
# Create the Run URL
351+
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
352+
353+
# Send the notification to Slack
354+
curl -X POST -H 'Content-type: application/json' \
355+
--data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
356+
--arg operation "$OPERATION" --arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
357+
--arg run_url "$RUN_URL" \
358+
'{attachments: [{color: $color, title: ("Terraform " + $operation + ": " + $status), text: $message, fields: [{title: "Operation", value: $operation, short: true}, {title: "Repository", value: $repo, short: true}, {title: "Branch", value: $branch, short: true}, {title: "Commit", value: $sha, short: true}, {title: "Run URL", value: $run_url, short: false}]}]}')" \
359+
$SLACK_WEBHOOK_URL
360+
326361
...

0 commit comments

Comments
 (0)