Skip to content

#43 drift detectionの失敗時にSNS通知を追加し、Terraformの出力を改善 #6

#43 drift detectionの失敗時にSNS通知を追加し、Terraformの出力を改善

#43 drift detectionの失敗時にSNS通知を追加し、Terraformの出力を改善 #6

Workflow file for this run

name: Terraform Drift Detection
on:
schedule:
- cron: '0 3 * * *' # 毎日03:00 JST(=18:00 UTC)
workflow_dispatch: # 手動実行も可能にする
push:
branches:
- '43-drift' # テスト用に43-driftブランチでも実行
jobs:
drift-detection:
runs-on: arc-runner-set
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.11.3
- name: Terraform Init
run: |
cd example/actions/terraform
terraform init -input=false
- name: Terraform Plan (Drift Check)
id: plan
run: |
cd example/actions/terraform
terraform plan -detailed-exitcode -input=false || echo "exit_code=$?" >> $GITHUB_OUTPUT
continue-on-error: true
- name: Send SNS Notification on Plan Failure
if: steps.plan.outcome == 'failure'
run: |
aws sns publish \
--topic-arn $(cd example/actions/terraform && terraform output -raw sns_topic_arn) \
--subject "Terraform Plan Failed - Drift Detection" \
--message "Terraform plan failed during drift detection. Please check the GitHub Actions logs for details. Workflow: ${{ github.workflow }}, Run ID: ${{ github.run_id }}"
- name: Check for Drift
if: steps.plan.outcome == 'success'
run: |
cd example/actions/terraform
EXIT_CODE=${{ steps.plan.outputs.exit_code }}
if [ "$EXIT_CODE" = "2" ]; then
echo "Drift detected! Please review the plan."
aws sns publish \
--topic-arn $(terraform output -raw sns_topic_arn) \
--subject "Terraform Drift Detected" \
--message "Terraform configuration drift has been detected. Please review the plan and apply necessary changes. Workflow: ${{ github.workflow }}, Run ID: ${{ github.run_id }}"
exit 1
else
echo "No drift detected."
fi