Interview Prep/GitHub Actions/Pipeline Design & Triggers

A workflow must run Terraform plan on PRs and apply on merge, with the plan reviewed. Design it.

AdvancedScenarioPipeline Design & Triggers
Answer

Situation read → Plan and apply must correspond, apply must be gated.

What I'd do →

  1. PR: terraform plan -out=tfplan with a read-only OIDC role (sub = pull_request), post the plan text as a PR comment (via workflow_run for forks), upload tfplan is not reused because state may change.
  2. Merge to main: re-plan with the apply role, upload plan, then an apply job with environment: prod-infra (required reviewers) that downloads the plan artifact and runs terraform apply tfplan.
  3. concurrency: terraform-prod with no cancel to protect state.
  4. The apply role's trust: repo:ORG/infra:environment:prod-infra.

Why → Reviewers approve the exact saved plan that will be applied, and only the gated job holds write credentials.

Interview tip → Explain why the PR plan is not applied: state can drift between PR and merge.

All GitHub Actions questions Roadmap: GitHub Actions

More on Pipeline Design & Triggers