Interview Prep/Terraform/Pipelines Terraform Cloud and OpenTofu

How do you add a nightly drift detection job in CI?

IntermediateScenarioPipelines Terraform Cloud and OpenTofu
Answer

Situation read → Scheduled, read-only comparison.

What I'd do →

on:
  schedule: [{ cron: "0 2 * * *" }]
jobs:
  drift:
    steps:
      - run: terraform init -input=false
      - run: |
          set +e
          terraform plan -detailed-exitcode -lock=false -input=false
          rc=$?
          [ $rc -eq 2 ] && ./notify-slack.sh "Drift detected in ${{ github.workflow }}"
          [ $rc -eq 1 ] && exit 1
          exit 0

Why → Exit code 2 signals pending changes without parsing text; a read-only role makes it safe.

Interview tip → Mention -lock=false is acceptable only for read-only plans.

All Terraform questions Roadmap: Terraform in production

More on Pipelines Terraform Cloud and OpenTofu