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 →
- PR:
terraform plan -out=tfplanwith a read-only OIDC role (sub=pull_request), post the plan text as a PR comment (viaworkflow_runfor forks), uploadtfplanis not reused because state may change. - Merge to
main: re-plan with the apply role, upload plan, then anapplyjob withenvironment: prod-infra(required reviewers) that downloads the plan artifact and runsterraform apply tfplan. concurrency: terraform-prodwith no cancel to protect state.- 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.
More on Pipeline Design & Triggers
You join a team whose single 900-line workflow runs everything on every push to every branch. How do you restructure it?IntermediatePushing to a PR branch runs CI twice (once for push, once for pull_request). How do you fix it?BeginnerProduct wants a "Deploy to staging" button that lets QA pick a version. Design it.BeginnerA nightly security scan should run at 02:00 India time on weekdays and also be runnable on demand. Write it.BeginnerYour team wants preview environments per PR on EKS, destroyed when the PR closes. Design the workflow.AdvancedTwo workflows must run in sequence: CI then a separate deploy workflow owned by another team. How do you link them?Intermediate