Skip to content

fix(terraform): make apply find the reviewed plan [NOJIRA] - #342

Merged
tobias0106 merged 1 commit into
mainfrom
fix/terraform-workflow-apply-artifact-lookup
Sep 11, 2026
Merged

tobias0106 merged 1 commit into
mainfrom
fix/terraform-workflow-apply-artifact-lookup

Conversation

@tobias0106

Copy link
Copy Markdown
Contributor

Follow-up to #341. Three defects found while validating that workflow against the three Terraform repos — all of them block the first adopter.

1. Apply can never find the plan. Blocking.

terraform-stack.yml uploads tfplan-<slug>-${{ github.sha }}. On a pull_request event github.sha is the merge commit of refs/pull/N/merge — ephemeral, and equal to neither the commit that lands on main nor the PR head. Apply looks up the pushed commit, then falls back to .head.sha via /commits/{sha}/pulls. It matches neither, so every apply fails with No reviewed plan.

A merge queue does not rescue it: all three repos enforce required_linear_history, so the queue squashes or rebases and the landed SHA differs from the merge_group SHA too. None of the three has a merge queue configured today anyway.

Fix: name the artifact with github.event.pull_request.head.sha || github.sha. The download step's existing head-SHA fallback then resolves it for squash, rebase and merge-commit alike.

2. terraform-ok goes green on a failed discovery

Discovery deliberately hard-fails a stack with no accounts[] entry. That leaves terraform skipped, not failed, and [[ result != 'failure' ]] passes on skipped — so the only required status check reports success while nothing was planned. It now gates on discover too, and treats cancelled as a failure.

3. No way to pass per-account static keys

terraform-core has four AWS accounts with a key pair each (TERRAFORM_CORE_{INTERNAL,MONTA,PRODUCTION,STAGING}_AWS_*) and no OIDC roles yet — it is the suggested first adopter. The discover jq built a fixed {dir, role, environment, region} object and dropped every other key, so the caller had nothing to select a secret with, and GitHub expressions have no upper() to derive one from environment.

Discovery now passes through any key on the matched accounts[] entry except match:

aws-access-key-id: ${{ secrets[format('TERRAFORM_CORE_{0}_AWS_ACCESS_KEY_ID', matrix.stack.secret_key)] }}

Defaults and first-match-wins are unchanged; an unmatched stack still fails discovery.

Not fixed here

The over-wide fan-out already called out in #341. Still blocking for terraform-db-access, and worth landing before monta-app/terraform adopts: that repo's root main.tf declares its own S3 backend without a backend.tf, so any change to it fans out to all four stacks on apply.

🤖 Generated with Claude Code

The plan artifact was named with github.sha, which on a pull_request event
is the ephemeral refs/pull/N/merge commit. Apply looks up the pushed commit
and then the PR head, so it matched neither and every apply failed with
"No reviewed plan". Name it with the PR head SHA instead.

Also:
- terraform-ok now gates on discover. Discovery hard-fails on a stack with
  no accounts[] entry, which leaves terraform skipped, not failed, so the
  required check went green on a failed discovery.
- discover passes through any extra key on the matched accounts[] entry.
  A repo with one static key pair per AWS account (terraform-core has four)
  had no way to carry the secret name to the caller.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@tobias0106
tobias0106 requested a review from a team as a code owner September 10, 2026 10:13
@tobias0106
tobias0106 requested review from sudheer-monta and removed request for a team September 10, 2026 10:13
@tobias0106
tobias0106 merged commit 79f3e03 into main Sep 11, 2026
1 check passed
@tobias0106
tobias0106 deleted the fix/terraform-workflow-apply-artifact-lookup branch September 11, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants