Skip to content

Commit

Permalink
edit check drift method
Browse files Browse the repository at this point in the history
  • Loading branch information
mamu0 committed Jun 18, 2024
1 parent d2dc64f commit 4c910f4
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions .github/workflows/infra_drift_detection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,46 @@ jobs:
working-directory: ${{ steps.directory.outputs.dir }}
run: |
terraform plan -no-color -detailed-exitcode -out=plan.tfplan
echo $?
PLAN_EXIT_CODE=$?
echo "Plan exit: $PLAN_EXIT_CODE"
if [ $PLAN_EXIT_CODE -eq 1 ]; then
echo "::error::Terraform plan exited with an error"
echo "drift_found=false" >> $GITHUB_OUTPUT
exit 1
elif [ $PLAN_EXIT_CODE -eq 0 ]; then
echo "No drifts in this configuration"
echo "drift_found=false" >> $GITHUB_OUTPUT
exit 0
else
terraform show -no-color -json plan.tfplan > plan.json
echo "drift_found=true" >> $GITHUB_OUTPUT
NO_CHANGES=$(jq '.resource_changes == null or (.resource_changes | length == 0)' plan.json)
elif [ "$NO_CHANGES" = "true" ]; then
echo "No drifts in this configuration"
echo "drift_found=false" >> $GITHUB_OUTPUT
exit 0
else
echo "drift_found=true" >> $GITHUB_OUTPUT
fi
fi
- name: Drift Detection
if: steps.plan.outputs.drift_found == 'true'
id: drift
working-directory: ${{ steps.directory.outputs.dir }}
run: |
if grep -q "No changes. Your infrastructure matches the configuration." plan_output.txt; then
echo "No drifts in this configuration"
exit 0
else
# Count the number of resources to add, change and destroy
TO_ADD=$(jq '[.resource_changes[] | select(.change.actions | index("create"))] | length' plan.json)
TO_CHANGE=$(jq '[.resource_changes[] | select(.change.actions | index("update"))] | length' plan.json)
TO_DESTROY=$(jq '[.resource_changes[] | select(.change.actions | index("delete"))] | length' plan.json)
echo "Drift detected!"
echo "- Resources to add: $TO_ADD"
echo "- Resources to change: $TO_CHANGE"
echo "- Resources to destroy: $TO_DESTROY"
# Salva le variabili nell'ambiente per l'utilizzo nei passaggi successivi
echo "TO_ADD=$TO_ADD" >> $GITHUB_ENV
echo "TO_CHANGE=$TO_CHANGE" >> $GITHUB_ENV
echo "TO_DESTROY=$TO_DESTROY" >> $GITHUB_ENV
exit 1
fi
# Count the number of resources to add, change and destroy
TO_ADD=$(jq '[.resource_changes[] | select(.change.actions | index("create"))] | length' plan.json)
TO_CHANGE=$(jq '[.resource_changes[] | select(.change.actions | index("update"))] | length' plan.json)
TO_DESTROY=$(jq '[.resource_changes[] | select(.change.actions | index("delete"))] | length' plan.json)
echo "Drift detected!"
echo "- Resources to add: $TO_ADD"
echo "- Resources to change: $TO_CHANGE"
echo "- Resources to destroy: $TO_DESTROY"
# Salva le variabili nell'ambiente per l'utilizzo nei passaggi successivi
echo "TO_ADD=$TO_ADD" >> $GITHUB_ENV
echo "TO_CHANGE=$TO_CHANGE" >> $GITHUB_ENV
echo "TO_DESTROY=$TO_DESTROY" >> $GITHUB_ENV
exit 1
# How to configure: https://github.com/ravsamhq/notify-slack-action
# Use ${{ inputs.webhook_url }} instead of secret if you want to change it manually
Expand Down

0 comments on commit 4c910f4

Please sign in to comment.