Interview Prep/Terraform/Pipelines Terraform Cloud and OpenTofu

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 →

  1. Trigger plan on pull_request, apply on push to main.
  2. Authenticate with OIDC, pin the Terraform version.
  3. Protect main and 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.

All Terraform questions Roadmap: Terraform in production

More on Pipelines Terraform Cloud and OpenTofu