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 $?
fiTwo things silently swallow failures:
exit $?always exits 0. Inside thethenbranch$?is the status of the negated pipeline, which is0whenever the pipeline failed. Any failure exits 0, so the job succeeds with a missing or invalidplan.json. The exit also skips thepost-planhook and replaces an exit code of2(non-empty plan,warning_on_non_empty_plan) with0.- No
pipefailinsh. The pipeline status isjq's. Iftofu showfails and prints nothing,jqexits0on empty input and leaves an emptyplan.json. Theplan-jsoncommand 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 -jsonoutput to a (owner-only) temp file instead of a pipe, - checks
tofu show,jqand a non-empty result individually, - removes the partial
plan.jsonand 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.