Skip to content

CICD-240 · Infrastructure drift detection ​

Encyclopedia · Terraform and infrastructure as code · All snippets

Previous · CICD-239 · Next · CICD-241

1. Where it fits ​

  • Placement scripts/drift.sh.
  • Prerequisites trusted Linux runner, Terraform CLI, reviewed IaC and a disposable state for writes.
  • Platforms local-linux, github-actions, gitea-actions.

2. Copy the snippet ​

bash
status=0
terraform plan -input=false -detailed-exitcode -no-color || status=$?
case "$status" in
  0) echo 'No differences';;
  2) echo 'Plan contains changes'; exit 2;;
  *) exit "$status";;
esac

3. How it works ​

detailed-exitcode distinguishes a clean plan, detected changes and errors. The shell keeps these outcomes distinct for scheduling and reporting.

A Terraform plan has three operationally distinct exit states.

flowchart TD
  N1["Terraform plan"]
  N2{"Exit status"}
  N1 --> N2
  N3["No changes"]
  N2 -->|0| N3
  N4["Changes detected"]
  N2 -->|2| N4
  N5["Plan error"]
  N2 -->|Other| N5
  classDef trigger fill:#EFF6FF,stroke:#0540F2,color:#0A0A0A
  classDef build fill:#CCFBF1,stroke:#0D9488,color:#0A0A0A
  classDef test fill:#EEF2FF,stroke:#4F46E5,color:#0A0A0A
  classDef gate fill:#FEF3C7,stroke:#D97706,color:#0A0A0A
  classDef security fill:#EDE9FE,stroke:#7C3AED,color:#0A0A0A
  classDef deploy fill:#D1FAE5,stroke:#059669,color:#0A0A0A
  classDef fail fill:#FEE2E2,stroke:#DC2626,color:#0A0A0A
  class N1 build
  class N2,N4 gate
  class N3 deploy
  class N5 fail

4. Verify the result ​

Schedule against the desired main commit and distinguish plan error from configuration or infrastructure differences.

Validation syntax-validated (Bash parse). Status definitions.