fix: Preserve Terraform exit code in plan step #13

Closed
Damien wants to merge 0 commits from fix/terraform-plan-exitcode into dev
Owner

Problem

The Gitea Actions workflows were incorrectly capturing the exit code from the terraform plan command. The command was using | tee to display the output while saving it to a file, but this caused the $? variable to capture the exit code of tee (which is always 0) instead of terraform plan. This meant:

  • In terraform-deploy.yml: The has_changes output was always false, preventing the apply job from running even when changes were detected
  • In terraform-pr.yml: The exitcode output was always 0, making it impossible to distinguish between no changes and changes detected

Solution

Modified both workflow files to:

  1. Replace the pipe to tee with a direct redirect to file: > plan_output.txt 2>&1
  2. Add a separate cat plan_output.txt command to display the output in the logs

This preserves Terraform's exit code (0 for no changes, 2 for changes detected, 1 for errors) so the workflows can properly determine if changes exist and proceed with deployment.

Testing

The changes maintain the same functionality:

  • Plan output is still captured to plan_output.txt for later use
  • Plan output is still displayed in the workflow logs via cat
  • The exit code is now correctly captured from the terraform plan command
## Problem The Gitea Actions workflows were incorrectly capturing the exit code from the `terraform plan` command. The command was using `| tee` to display the output while saving it to a file, but this caused the `$?` variable to capture the exit code of `tee` (which is always 0) instead of `terraform plan`. This meant: - In `terraform-deploy.yml`: The `has_changes` output was always `false`, preventing the `apply` job from running even when changes were detected - In `terraform-pr.yml`: The `exitcode` output was always `0`, making it impossible to distinguish between no changes and changes detected ## Solution Modified both workflow files to: 1. Replace the pipe to `tee` with a direct redirect to file: `> plan_output.txt 2>&1` 2. Add a separate `cat plan_output.txt` command to display the output in the logs This preserves Terraform's exit code (0 for no changes, 2 for changes detected, 1 for errors) so the workflows can properly determine if changes exist and proceed with deployment. ## Testing The changes maintain the same functionality: - Plan output is still captured to `plan_output.txt` for later use - Plan output is still displayed in the workflow logs via `cat` - The exit code is now correctly captured from the `terraform plan` command
Damien closed this pull request 2026-06-09 17:52:17 +00:00
All checks were successful
Terraform Deploy / Validate (push) Successful in 5s
Terraform Deploy / Plan (push) Successful in 4s
Terraform Deploy / Apply (push) Has been skipped

Pull request closed

Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Damien/iac-homelab#13