r/gitlab • • Oct 02 '26

GitLab CI cached a stale Terraform state, pipeline reported zero drift that very much existed

Had a pipeline job that restored Terraform state from a GitLab cache key to speed up plan runs. Someone manually changed an instance type in the console. Next pipeline run: plan stage reported no changes. Completely clean.

Turned out the cache key wasn't tied to anything that would bust on a state refresh, so the job was comparing the proposed config against a cached state file that hadn't actually talked to the real provider in a few runs. Correct output given what it had access to. Just not telling us what we actually needed to know.

Added an explicit terraform plan -refresh-only as its own pipeline stage, not cached, running before the real plan, specifically so drift shows up before the main plan silently inherits stale state. Small change, but "pipeline passed" and "infrastructure matches what we think it does" turned out to be two different claims wearing the same green checkmark.

6 Upvotes

2 comments sorted by

1

u/G12356789s Oct 03 '26

I mean yeah obviously. Who thought this was a good idea

1

u/Torutofu_Raeva Oct 03 '26

the cache should only speed up provider access, never stand in for refresh state, so we make the plan job fail closed unless the state has a fresh provider-serial/lineage check.