fix: make destroy guard parse tfplan JSON robustly
Some checks failed
Terraform Plan / Terraform Plan (push) Has been cancelled
Some checks failed
Terraform Plan / Terraform Plan (push) Has been cancelled
Use terraform show with no-color and resilient JSON extraction to avoid parser failures when workflow output includes non-JSON noise.
This commit is contained in:
@@ -54,8 +54,8 @@ jobs:
|
|||||||
ALLOW_TF_DESTROY: ${{ secrets.ALLOW_TF_DESTROY }}
|
ALLOW_TF_DESTROY: ${{ secrets.ALLOW_TF_DESTROY }}
|
||||||
working-directory: terraform
|
working-directory: terraform
|
||||||
run: |
|
run: |
|
||||||
terraform show -json tfplan > tfplan.json
|
terraform show -json -no-color tfplan > tfplan.json
|
||||||
DESTROY_COUNT=$(python3 -c 'import json; p=json.load(open("tfplan.json")); print(sum(1 for rc in p.get("resource_changes", []) if "delete" in rc.get("change", {}).get("actions", [])))')
|
DESTROY_COUNT=$(python3 -c 'import json,sys; raw=open("tfplan.json","rb").read().decode("utf-8","ignore"); start=raw.find("{"); end=raw.rfind("}"); data=json.loads(raw[start:end+1]); print(sum(1 for rc in data.get("resource_changes", []) if "delete" in rc.get("change", {}).get("actions", [])))')
|
||||||
echo "Planned deletes: $DESTROY_COUNT"
|
echo "Planned deletes: $DESTROY_COUNT"
|
||||||
if [ "$DESTROY_COUNT" -gt 0 ] && [ "${ALLOW_TF_DESTROY}" != "true" ]; then
|
if [ "$DESTROY_COUNT" -gt 0 ] && [ "${ALLOW_TF_DESTROY}" != "true" ]; then
|
||||||
echo "Destroy actions detected. Set ALLOW_TF_DESTROY=true to allow."
|
echo "Destroy actions detected. Set ALLOW_TF_DESTROY=true to allow."
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ jobs:
|
|||||||
ALLOW_TF_DESTROY: ${{ secrets.ALLOW_TF_DESTROY }}
|
ALLOW_TF_DESTROY: ${{ secrets.ALLOW_TF_DESTROY }}
|
||||||
working-directory: terraform
|
working-directory: terraform
|
||||||
run: |
|
run: |
|
||||||
terraform show -json tfplan > tfplan.json
|
terraform show -json -no-color tfplan > tfplan.json
|
||||||
DESTROY_COUNT=$(python3 -c 'import json; p=json.load(open("tfplan.json")); print(sum(1 for rc in p.get("resource_changes", []) if "delete" in rc.get("change", {}).get("actions", [])))')
|
DESTROY_COUNT=$(python3 -c 'import json,sys; raw=open("tfplan.json","rb").read().decode("utf-8","ignore"); start=raw.find("{"); end=raw.rfind("}"); data=json.loads(raw[start:end+1]); print(sum(1 for rc in data.get("resource_changes", []) if "delete" in rc.get("change", {}).get("actions", [])))')
|
||||||
echo "Planned deletes: $DESTROY_COUNT"
|
echo "Planned deletes: $DESTROY_COUNT"
|
||||||
if [ "$DESTROY_COUNT" -gt 0 ] && [ "${ALLOW_TF_DESTROY}" != "true" ]; then
|
if [ "$DESTROY_COUNT" -gt 0 ] && [ "${ALLOW_TF_DESTROY}" != "true" ]; then
|
||||||
echo "Destroy actions detected. Set ALLOW_TF_DESTROY=true to allow."
|
echo "Destroy actions detected. Set ALLOW_TF_DESTROY=true to allow."
|
||||||
|
|||||||
Reference in New Issue
Block a user