Jake Zhang

Why Your CI Pipeline Shouldn't Grade Its Own Homework: Two-State OpenTofu Drift Supervision on Proxmox

Setting up a scheduled tofu plan in CI to detect infrastructure drift takes about twenty minutes.

Trusting it takes a lot more thought.

Most homelab (and plenty of production) drift setups have a subtle blind spot: they rely on the CI pipeline itself to tell you when something is wrong. If the self-hosted runner hangs, if a scheduled pipeline stops firing, if a job gets killed before writing output, or if tofu plan -detailed-exitcode exits 2 (changes detected) and your CI UI quietly turns it into an easy-to-ignore yellow warning, silence looks identical to "no drift."

When I codified my Proxmox VE fleet with OpenTofu (bpg/proxmox), Ansible, and a self-hosted GitLab runner, I wanted scheduled drift detection that was boring, read-only, leak-resistant, and supervised from outside the pipeline. Here is the design that has been running daily without babysitting.

The Four Rules We Refused to Compromise On

  1. Scheduled runs can never apply, refresh-state, or self-heal.
    No -auto-approve, no -refresh-only state rewrites, no "repair agent," and no automatic date-stamping of docs. Drift detection observes; humans decide.
  2. Separate "drift detected" from "check failed."
    With tofu plan -detailed-exitcode:
  3. 0 = no planned actions
  4. 2 = planned changes needing review (this might be unapplied main config, not necessarily an out-of-band edit)
  5. 1 = execution, auth, or provider failure
    Treating 1 and 2 as the same alert trains you to ignore broken checks.
  6. Never publish raw plan output as a CI artifact.
    A full tofu plan log or binary plan.cache often contains sensitive values, tokens, or internal topology details. Scheduled jobs only publish a tiny sanitized result.json receipt and discard transient plan files immediately.
  7. A missing receipt is a failure, not silence.
    If a job is killed mid-run, canceled, queued for more than 2 hours, or has not completed within 30 hours, an independent watchdog alerts.

Two Independent States, One Matrix Schedule

A common Proxmox IaC trap is putting your CI runner container inside the same OpenTofu state that the runner itself plans and applies. Instead, we maintain two separate states:

  • homelab (root state: application LXCs, edge proxy, DNS/Cloudflare resources)
  • runner (control-plane state for the CI runner itself; applied manually from a management LXC, never from inside CI)

A single daily scheduled pipeline on main launches exactly two matrix jobs (one per state), each with:

  • Pinned OpenTofu and bpg/proxmox provider versions
  • Isolated TF_DATA_DIR
  • Normal refresh with bounded backend state locking
  • Zero apply jobs attached to scheduled pipelines (enforced in CI rules)

Sanitized Receipts Instead of Raw Plans

Each drift job wraps tofu plan -detailed-exitcode -out=plan.cache, runs tofu show -json plan.cache locally inside the ephemeral job container to extract only non-sensitive structural metadata, and immediately deletes plan.cache and raw logs before exiting.

The only artifact uploaded (maintainer-only, 7-day expiry) is a compact result.json containing:

  • Job and pipeline identity plus timestamps
  • Exit code (0, 1, or 2) and failing phase (if 1)
  • Resource addresses and planned actions (for example ["update"]) with zero attribute values
  • Observed-drift resource addresses and actions

If drift occurs, the alert tells you which state and which resource addresses changed; you then reproduce the full diff privately on the management host.

Independent Supervision Outside CI

Here is the piece most setups skip: the thing being monitored should not be the only thing allowed to send alerts.

A systemd timer on the Proxmox host runs every 15 minutes and invokes a fixed, read-only query script on the management LXC. The GitLab API token stays strictly on the management container: no credentials are copied to the hypervisor, and HTTP redirects when fetching artifacts explicitly strip the auth header.

Every tick, the watchdog checks:

  1. Is the expected GitLab schedule still active and targeting main?
  2. Has the latest completed scheduled pipeline aged past 30 hours? (Why 30 hours for a daily job? Hosted schedulers like GitLab do not guarantee exact-minute dispatch; allowing a sane latency window prevents flaky false positives while catching dead schedules.)
  3. Has any scheduled pipeline stayed pending or running for more than 2 hours?
  4. Do both states (homelab and runner) have valid, matching result.json receipts?
  5. Are there any failed, canceled, or exit-2 results?

Findings deduplicate daily and emit explicit recovery notifications when clear. Crucially, a newly started pending run cannot erase an older unresolved finding, so an active drift alert does not disappear just because tomorrow's pipeline queued up.

Even this architecture is honest about its own boundary: a local watchdog catches CI, runner, token, and API failures, while total host, network, or SMTP loss still requires an external dead-man switch.

Testing Alerts Without Breaking Production

One last discipline worth keeping: never sabotage production infrastructure or stop your real runner just to test alerting.

Keep the watchdog evaluation logic pure and deterministic so unit tests simulate:

  • exit 0 clean receipts
  • exit 2 drift receipts
  • exit 1 auth or init failures
  • killed jobs with missing receipts
  • stale >30h schedules and stuck >2h pipelines
  • recovery after resolution

Then verify live wiring once with a read-only --check dry-run that performs API queries without mutating alert state or sending mail.

Takeaways

Good homelab (and production) automation is not about making an AI agent or CI job "fix everything automatically." It is about designing small, fail-closed receipts so that nothing fails silently, secrets never leak into CI artifacts, and you actually trust what "green" means six months later.