Skip to content

fix(ci): fire CI hooks per-component in --all / --query plan mode (#2… - #2430

Merged
Andriy Knysh (aknysh) merged 3 commits into
cloudposse:mainfrom
thejrose1984:bugfix/2397/atmos-ci
May 19, 2026
Merged

Andriy Knysh (aknysh) merged 3 commits into
cloudposse:mainfrom
thejrose1984:bugfix/2397/atmos-ci

Conversation

@thejrose1984

@thejrose1984 thejrose1984 commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

what

  • Fixes atmos terraform plan --all (and --query, --components, stack-without-component) producing only a single CI summary entry for the last component instead of one entry per component
  • Adds an optional PerComponentHook callback to ConfigAndStacksInfo that fires after each component executes in multi-component mode, with that component's captured stdout+stderr and exit code
  • Wires runCIHooksForPlanComponent as the per-component hook for the plan subcommand so $GITHUB_STEP_SUMMARY receives one entry per component with the correct component/stack context
  • Adds a wasMultiComponentExecution sentinel so the global PostRunE CI hook call is suppressed when per-component hooks have already fired, preventing double-firing

why

  • In multi-component mode, terraformRunWithOptions routes to ExecuteTerraformQuery and discards the shellOpts capture buffers passed by plan.go RunE, so capturedPlanOutput was always empty
  • PostRunE fired once after all components completed, calling RunCIHooks with an empty output buffer and the last component's info.Component/info.Stack, misattributing the summary
  • For stacks with 5 components, only 1 summary entry appeared (or none) instead of 5 — silently hiding the plan result for every component except the last

references

Closes #2397

Summary by CodeRabbit

  • New Features

    • Per-component CI hook execution for terraform plan in multi-component runs, with captured, cleaned plan output passed to each hook.
  • Bug Fixes

    • Ensure CI plan output capture and hook invocation behave consistently across early exits and multi-component executions to avoid duplicate/incorrect hooks.
  • Tests

    • Added tests validating per-component hook behavior, output handling, and multi-component execution paths.

Review Change Stack

…oudposse#2397)

Previously, `atmos terraform plan --all` routed to ExecuteTerraformQuery
which ran each component but only fired CI hooks once in PostRunE, using
the last component's context and an empty capture buffer. This caused
$GITHUB_STEP_SUMMARY to contain at most one (misattributed) entry instead
of one entry per component.

Fix: add an optional PerComponentHook callback to ConfigAndStacksInfo.
When set, processTerraformComponent and executeNodeCommand capture each
component's stdout+stderr individually and invoke the hook immediately
after execution. terraformRunWithOptions wires runCIHooksForPlanComponent
as the hook for `plan --all/--query`, and PostRunE is suppressed via a
wasMultiComponentExecution sentinel to avoid double-firing.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@thejrose1984
thejrose1984 requested a review from a team as a code owner May 19, 2026 01:07
@atmos-pro

atmos-pro Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Tip

Atmos Pro  

No affected stacks workflow was detected for this pull request.
If this is expected, no action is needed.
Learn More.

@github-actions github-actions Bot added the size/m Medium size PR label May 19, 2026
@coderabbitai

coderabbitai Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5698b5fb-d2f1-4d2c-8dd6-1dec1d7a9e03

📥 Commits

Reviewing files that changed from the base of the PR and between a7ea7ab and f2513b8.

📒 Files selected for processing (2)
  • internal/exec/terraform_executor.go
  • internal/exec/terraform_executor_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/exec/terraform_executor_test.go

📝 Walkthrough

Walkthrough

Adds a PerComponentHook to ConfigAndStacksInfo, captures per-component Terraform stdout/stderr in executor/utils, invokes per-component CI hooks during multi-component plan runs (stripping ANSI and forwarding exit codes), and suppresses the global post-plan hook when component hooks ran.

Changes

Per-component CI hooks

Layer / File(s) Summary
PerComponentHook contract
pkg/schema/schema.go
ConfigAndStacksInfo gains optional PerComponentHook func(info *ConfigAndStacksInfo, output string, execErr error) invoked after each component executes in multi-component mode.
Executor-level hook invocation with output capture
internal/exec/terraform_executor.go, internal/exec/terraform_executor_test.go
When PerComponentHook is set, executeNodeCommand runs Terraform with stdout/stderr capture, combines outputs, snapshots component info, calls PerComponentHook(compInfo, combinedOutput, execErr), and wraps exec failures with ErrTerraformExecFailed. Tests cover success and failure paths and verify hook invocation/context.
Terraform utils-level hook invocation with output capture
internal/exec/terraform_utils.go, internal/exec/terraform_utils_test.go
processTerraformComponent captures stdout/stderr, combines outputs, snapshots component/stack into compInfo, invokes PerComponentHook(&compInfo, combined, execErr), and returns the component result. Tests assert hook receives captured output and execErr before returning.
Plan command multi-component hook orchestration
cmd/terraform/utils.go, cmd/terraform/utils_hooks_test.go
Adds ansi.Strip, a wasMultiComponentExecution flag, and runCIHooksForPlanComponent wrapper that strips ANSI and calls AfterTerraformPlan per-component with computed exit code; wires PerComponentHook into multi-component plan routing and includes a wrapper test.
Plan command early-exit and hook deduplication
cmd/terraform/plan.go
RunE resets per-run globals (capturedPlanOutput, wasMultiComponentExecution) at start and updates deferred error-hook logic; PostRunE returns early when wasMultiComponentExecution is true to avoid duplicate global CI hooks.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • osterman
  • aknysh
  • atmos-pro
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing CI hooks to fire per-component in multi-component plan modes (--all, --query).
Linked Issues check ✅ Passed All coding objectives from #2397 are met: per-component hook invocation with captured output, correct component context, global hook suppression, and CI summary entries per component.
Out of Scope Changes check ✅ Passed All changes directly address the root causes outlined in #2397: adding PerComponentHook callback, wiring it in multi-component paths, capturing per-component output, and preventing double-firing.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/exec/terraform_executor_test.go`:
- Around line 187-195: The test is flaky because it relies on the real
ExecuteTerraform failure; make it deterministic by stubbing/mocking
ExecuteTerraform to return a fixed error and then assert behavior against that
error. Modify the test for executeNodeCommand to inject a fake executor (or
temporarily replace the package-level ExecuteTerraform function) so that
ExecuteTerraform returns a known error (e.g., errUtils.ErrTerraformExecFailed or
a new sentinel error), then run executeNodeCommand and assert hookCalled,
hookComponent ("vpc"), hookStack ("dev"), hookErr equals the injected error, and
that the returned err is ErrTerraformExecFailed; reference ExecuteTerraform,
executeNodeCommand, hookCalled, hookComponent, hookStack, hookErr and
errUtils.ErrTerraformExecFailed when making the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 59694ddf-f489-4583-af69-ba5fb9e212d1

📥 Commits

Reviewing files that changed from the base of the PR and between 464dd5f and a7ea7ab.

📒 Files selected for processing (8)
  • cmd/terraform/plan.go
  • cmd/terraform/utils.go
  • cmd/terraform/utils_hooks_test.go
  • internal/exec/terraform_executor.go
  • internal/exec/terraform_executor_test.go
  • internal/exec/terraform_utils.go
  • internal/exec/terraform_utils_test.go
  • pkg/schema/schema.go

@aknysh Andriy Knysh (aknysh) added the patch A minor, backward compatible change label May 19, 2026
Replace the real ExecuteTerraform call in the hook test with an
injectable execTerraformFn package-level variable (same pattern as
authManagerFactory in terraform_query.go). This makes the test
environment-independent and enables a success-path sub-test.

Addresses CodeRabbit review comment on PR cloudposse#2430.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 67.34694% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.13%. Comparing base (1caf5df) to head (3e96b64).

Files with missing lines Patch % Lines
cmd/terraform/utils.go 58.33% 8 Missing and 2 partials ⚠️
cmd/terraform/plan.go 25.00% 1 Missing and 2 partials ⚠️
internal/exec/terraform_executor.go 75.00% 2 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (67.34%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2430      +/-   ##
==========================================
+ Coverage   78.11%   78.13%   +0.02%     
==========================================
  Files        1112     1112              
  Lines      105147   105193      +46     
==========================================
+ Hits        82140    82197      +57     
+ Misses      18455    18437      -18     
- Partials     4552     4559       +7     
Flag Coverage Δ
unittests 78.13% <67.34%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
internal/exec/terraform_utils.go 77.82% <100.00%> (+0.94%) ⬆️
pkg/schema/schema.go 87.70% <ø> (ø)
cmd/terraform/plan.go 77.77% <25.00%> (-3.00%) ⬇️
internal/exec/terraform_executor.go 65.75% <75.00%> (+4.46%) ⬆️
cmd/terraform/utils.go 55.21% <58.33%> (+0.24%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aknysh Andriy Knysh (aknysh) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks thejrose1984

@aknysh
Andriy Knysh (aknysh) merged commit 81631f0 into cloudposse:main May 19, 2026
55 of 57 checks passed
@atmos-pro

atmos-pro Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Note

Atmos Pro  

Waiting for your GitHub Actions workflow to upload affected stacks.
Learn More.

@thejrose1984
thejrose1984 deleted the bugfix/2397/atmos-ci branch May 19, 2026 11:14
@github-actions

Copy link
Copy Markdown

These changes were released in v1.219.0.

thejrose1984 added a commit to thejrose1984/atmos that referenced this pull request May 22, 2026
…2475)

Extends the per-component hook pattern from PR cloudposse#2430 (plan --all) to
apply --all so each component produces its own CI summary entry instead
of a single misattributed entry for the last component.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
thejrose1984 added a commit to thejrose1984/atmos that referenced this pull request May 22, 2026
…#2476)

Extends the per-component hook pattern from PR cloudposse#2430 (plan --all) and
cloudposse#2475 (apply --all) to deploy --all so each component produces its own
CI summary entry instead of a single misattributed entry for the last
component.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
thejrose1984 added a commit to thejrose1984/atmos that referenced this pull request May 22, 2026
…#2476)

Extends the per-component hook pattern from PR cloudposse#2430 (plan --all) and
cloudposse#2475 (apply --all) to deploy --all so each component produces its own
CI summary entry instead of a single misattributed entry for the last
component.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Andriy Knysh (aknysh) added a commit that referenced this pull request May 22, 2026
* fix(ci): fire CI hooks per-component in apply --all mode (#2475)

Extends the per-component hook pattern from PR #2430 (plan --all) to
apply --all so each component produces its own CI summary entry instead
of a single misattributed entry for the last component.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: use apply command identity in PostRunE suppression test

Addresses CodeRabbit review on PR #2477: newHookTestCmd() created a
command with Use:"plan" but the test calls applyCmd.PostRunE, causing
ProcessCommandLineArgs to see the wrong subcommand name in the
non-suppressed path.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
Andriy Knysh (aknysh) added a commit that referenced this pull request May 23, 2026
* fix(ci): fire CI hooks per-component in deploy --all mode (#2476)

Extends the per-component hook pattern from PR #2430 (plan --all) and
#2475 (apply --all) to deploy --all so each component produces its own
CI summary entry instead of a single misattributed entry for the last
component.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: clarify exit code 2 label in deploy hook forwarding test

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: use deploy command identity in PostRunE suppression test

Same fix as PR #2477: newHookTestCmd() hardcodes Use:"plan" but the
test calls deployCmd.PostRunE, causing ProcessCommandLineArgs to see
the wrong subcommand name in the non-suppressed path.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test(deploy): add RunE defer-guard regression test

Convert runHooksOnErrorWithOutput to a package-level var so tests can stub it,
and add TestDeployRunE_DeferGuard covering all four branches of the deploy.go
RunE defer-guard contract: non-nil error must fire the global hook only when
wasMultiComponentExecution is false, and nil error must never fire it.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix(ci): repair broken merge from main and refactor tests

The previous merge from main left the branch unbuildable:
- cmd/terraform/utils.go was missing a closing brace on the deploy
  PerComponentHook branch, producing a Go syntax error
- cmd/terraform/utils_hooks_test.go had deploy and apply test bodies
  interleaved with overlapping function declarations and orphaned
  statements

Reconstruct the test file to host both the deploy tests (this PR) and
the apply tests (brought in from main via #2475), and address open
CodeRabbit nitpicks at the same time:

- Add a compile-time sentinel for schema.ConfigAndStacksInfo so a
  field rename surfaces as a build failure
- Convert plan/deploy/apply demo-stacks tests and the deploy RunE
  defer-guard scenarios to table-driven subtests
- Document why cmd.NewTestKit(t) cannot be used here (circular
  import between cmd/terraform and cmd, same workaround the workdir
  tests use)

Co-Authored-By: Claude Opus 4.7 <[email protected]>

* refactor: narrow test changes to deploy scope and fix comment

Code review follow-ups:

- Restore the linear style of plan/apply demo-stacks tests to match
  main; these tests came in via merge and aren't part of this PR's
  scope. Only the deploy demo-stacks test and the deploy RunE
  defer-guard scenarios are refactored to table-driven subtests, per
  the CodeRabbit nitpicks for this PR.
- Update the per-component hook wiring comment in utils.go to read
  "plan, deploy, and apply" now that all three branches are wired.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch A minor, backward compatible change size/m Medium size PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: CI hooks fire only once in --all mode, producing summary for last component only

2 participants