Prompt
I need infrastructure code that works in pull requests and CI pipelines
Latest observation
Absolutely — if you want infrastructure code that works well in pull requests and CI pipelines, the main goal is to make it:
- non-destructive
- repeatable
- plan-first
- safe for ephemeral environments
- easy to validate automatically
Here’s a practical pattern that works well for most IaC setups, especially with Terraform, Pulumi, or similar tools.
Recommended workflow
In pull requests
Run only checks and a plan:
- Format
- Validate / lint
- Create a plan
- Post the plan back to the PR
- Do not apply automatically
In CI on main branch
Run:
- format check
- validation
- plan
- manual approval or protected deployment
- apply
Example structure
repo/
infra/
main.tf
variables.tf
outputs.tf
environments/
dev.tfvars
staging.tfvars
prod.tfvars
.github/
workflows/
pr.yml
deploy.yml
Terraform example for PR-safe workflows
pr.yml
name: Pull Request Checks
on:
pull_request:
jobs:
terraform:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
defaults:
run:
working-directory: infra
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.8.5
- name: Terraform fmt
run: terraform fmt -check -recursive
- name: Terraform init
run: terraform init -input=false
- name: Terraform validate
run: terraform validate
- name: Terraform plan
run: terraform plan -input=false -no-color -out=tfplan
- name: Show plan
run: terraform show -no-color tfplan
This is good for CI, but note:
terraform planmay still require cloud credentials- you may need remote state access
- PRs from forks can’t safely use secrets by default
Safer PR design: use speculative plans
In PRs, use a speculative plan only. Do not save or apply it.
For Terraform, plan is already speculative unless you apply it.
If you need cloud access in PRs:
- use read-only credentials
- or use a mocked test backend
- or skip cloud-dependent steps for forked PRs
Example with GitHub Actions and environment separation
deploy.yml
name: Deploy Infrastructure
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
environment: production
defaults:
run:
working-directory: infra
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform init
run: terraform init -input=false
- name: Terraform apply
run: terraform apply -input=false -auto-approve
Best practices for PR/CI-friendly infra code
1. Keep modules small and composable
- one module per concern
- avoid giant monolithic stacks
2. Make environments explicit
Use separate variable files or directories:
devstagingprod
3. Avoid interactive prompts
All commands should use:
-input=false- explicit variables
- environment variables for secrets
4. Use remote state
This prevents state drift between CI runs.
Examples:
- S3 + DynamoDB lock for Terraform
- Terraform Cloud
- Azure Storage
- GCS backend
5. Add policy checks
Use tools like:
tflintcheckovtfsecinfracost- OPA / Conftest
6. Gate production applies
Require:
- branch protection
- manual approval
- environment approvals
7. Make plans visible in PRs
Post the diff so reviewers can see:
- what will be created
- what will change
- what will be destroyed
If you want ephemeral preview environments
For each PR, you can create temporary infrastructure named by PR number:
preview-pr-123
Then destroy it when the PR closes.
This is common for:
- Kubernetes namespaces
- feature environments
- temporary databases
- preview apps
Example naming pattern:
locals {
env_name = "pr-${var.pr_number}"
}
In CI:
- create on PR open
- update on new commits
- destroy on PR close
Common pitfalls
- Using real production credentials in PRs
- Applying automatically on every push
- Not locking state
- Relying on local-only state
- Allowing secrets in forked PRs
- Missing cleanup for preview environments
If you tell me your stack, I can generate the exact code
I can produce a ready-to-use setup for any of these:
- Terraform + GitHub Actions
- Pulumi + GitHub Actions
- AWS / Azure / GCP
- Kubernetes manifests + CI
- CDK / CDKTF
- preview environments per PR
If you want, I can generate a complete working example for your cloud/provider and CI system.