Skip to content

Commit

Permalink
feat: Added slack notif support for both apply and destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanishdrove committed Jan 16, 2025
1 parent ffe5de0 commit fee58ed
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions .github/workflows/terraform_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,27 +300,62 @@ jobs:
terraform destroy -auto-approve
fi
- name: Notify Slack
if: ${{ always() }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
run: |
if [ "${{ job.status }}" == "success" ]; then
STATUS="Success ✅"
COLOR="good"
MESSAGE="Terraform deployment completed successfully."
else
STATUS="Failed ❌"
COLOR="danger"
MESSAGE="Terraform deployment failed. Check logs for details."
fi
# - name: Notify Slack
# if: ${{ always() }}
# env:
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
# run: |
# if [ "${{ job.status }}" == "success" ]; then
# STATUS="Success ✅"
# COLOR="good"
# MESSAGE="Terraform deployment completed successfully."
# else
# STATUS="Failed ❌"
# COLOR="danger"
# MESSAGE="Terraform deployment failed. Check logs for details."
# fi

# RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"

RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# curl -X POST -H 'Content-type: application/json' \
# --data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
# --arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
# --arg run_url "$RUN_URL" \
# '{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}]}]}')" \
# $SLACK_WEBHOOK_URL

curl -X POST -H 'Content-type: application/json' \
--data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
--arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
--arg run_url "$RUN_URL" \
'{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}]}]}')" \
$SLACK_WEBHOOK_URL
- name: Notify Slack
if: ${{ always() }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
run: |
# Determine the operation type (apply or destroy) using a custom environment variable or flag
if [[ "${{ inputs.destroy }}" == "true" ]]; then
OPERATION="Destroy"
else
OPERATION="Apply"
fi
# Check the job status
if [ "${{ job.status }}" == "success" ]; then
STATUS="Success ✅"
COLOR="good"
MESSAGE="Terraform $OPERATION completed successfully."
else
STATUS="Failed ❌"
COLOR="danger"
MESSAGE="Terraform $OPERATION failed. Check logs for details."
fi

# Create the Run URL
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"

# Send the notification to Slack
curl -X POST -H 'Content-type: application/json' \
--data "$(jq -n --arg color "$COLOR" --arg status "$STATUS" --arg message "$MESSAGE" \
--arg operation "$OPERATION" --arg repo "$GITHUB_REPOSITORY" --arg branch "$GITHUB_REF_NAME" --arg sha "$GITHUB_SHA" \
--arg run_url "$RUN_URL" \
'{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}]}]}')" \
$SLACK_WEBHOOK_URL

...

0 comments on commit fee58ed

Please sign in to comment.