Build a basic GitHub Actions pipeline: plan on pull request, apply on merge to main.
BeginnerScenarioPipelines Terraform Cloud and OpenTofu
Answer
Situation read → The standard GitOps-for-infrastructure loop.
What I'd do →
- Trigger
planonpull_request,applyonpushtomain. - Authenticate with OIDC, pin the Terraform version.
- Protect
mainand require the plan job to pass.
on:
pull_request:
push: { branches: [main] }
jobs:
terraform:
runs-on: ubuntu-latest
permissions: { id-token: write, contents: read, pull-requests: write }
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with: { terraform_version: 1.9.8 }
- run: terraform init -input=false
- run: terraform fmt -check -recursive
- run: terraform plan -input=false -out=tfplan
- if: github.ref == 'refs/heads/main'
run: terraform apply -input=false tfplan
Why → Code review gates every change, and merge is the only path to production.
Interview tip → Mention approval environments and concurrency for prod.
More on Pipelines Terraform Cloud and OpenTofu
Reviewers can't read raw plan logs. How do you present the plan in the PR?IntermediateTwo PRs merge close together and both apply to the same stack. What prevents trouble?IntermediateHow do you add a manual approval before prod applies?IntermediateCompare Atlantis, HCP Terraform and a hand-built GitHub Actions pipeline.IntermediateA saved plan was approved yesterday and apply fails because main changed. How do you avoid dead approvals?IntermediateExplain HCP Terraform workflows: VCS-driven, CLI-driven and API-driven.Beginner