fix: fail when the plan JSON report cannot be produced

Problem

In src/gitlab-tofu.sh, gitlab-tofu plan generated the JSON report like this:

if ! tofu "${tf_chdir_opt}" show -json "${plan_cache_path}" | jq -r "${plan_jq_filter}" > "${plan_json_path}"; then
  exit $?
fi

Two things silently swallow failures:

  1. exit $? always exits 0. Inside the then branch $? is the status of the negated pipeline, which is 0 whenever the pipeline failed. Any failure exits 0, so the job succeeds with a missing or invalid plan.json. The exit also skips the post-plan hook and replaces an exit code of 2 (non-empty plan, warning_on_non_empty_plan) with 0.
  2. No pipefail in sh. The pipeline status is jq's. If tofu show fails and prints nothing, jq exits 0 on empty input and leaves an empty plan.json. The plan-json command has the same problem.

A realistic trigger is a consumer using dev_overrides (for example when building a custom provider in the job). The "Provider development overrides are in effect" warning can end up in stdout and break jq, while the job stays green.

Fix

Add a write_plan_json helper that:

  • writes the tofu show -json output to a (owner-only) temp file instead of a pipe,
  • checks tofu show, jq and a non-empty result individually,
  • removes the partial plan.json and returns non-zero on failure.

plan and plan-json both use it. plan now does write_plan_json || exit $?, where $? is the real status.

Behavior change

Jobs whose plan JSON could not be produced previously passed silently and now fail with a clear error. That is the intent, but it may surface existing hidden problems in consuming projects.

This change does not suppress the dev_overrides warning. It only makes the failure visible.

Tests

Added a bats test that shims tofu to print a warning on show and asserts that gitlab-tofu plan fails, prints the error and leaves no plan.json. I have not run the test suite, so please rely on the pipeline.

Merge request reports

Loading
Loading