Branch-based Development

Preview infrastructure changes per branch using Projen-generated tasks and workflows.


Overview

Branch-based deployments let every feature branch spin up its own copy of the stacks. Test infrastructure changes without touching shared environments.

Enable branch deploys

Set enableBranchDeploy: true on any environment in environmentConfigs (see Configuration > Environments):

{ name: 'test', accountId: '987654321012', enableBranchDeploy: true },

After running npx projen, the starter kit generates:

GeneratedPurpose
npm run test:branch:deploy:allDeploy stacks with branch suffix
npm run test:branch:diff:stackPreview branch-specific changes
cdk-deploy-<env>-branch workflowAuto-deploy on branch push
cdk-destroy-<env>-branch workflowCleanup on branch delete or manual dispatch

These tasks use helpers from env-helper.ts to inject GIT_BRANCH_REF and clean branch names safely.

Local workflow

# Create a feature branch
git checkout -b feature/payment-alerts

# Preview CloudFormation output
npm run test:branch:synth
npm run test:branch:diff:stack StarterStack

# Deploy stacks with branch suffixes
npm run test:branch:deploy:stack StarterStack

# Tear down when finished
npm run test:branch:destroy:stack StarterStack

The tasks automatically set GIT_BRANCH_REF from your current git branch. For day-to-day commands, see the Local Development guide.

GitHub Actions workflow

When you push the branch, the generated workflow:

  1. Calculates branch identifier - Uses extractCleanedBranchName for safe naming
  2. Runs synth and deploy - Stack names include the suffix (e.g., StarterStack-feature-payment-alerts)
  3. Tags resources - Both environment and branch labels (handled in src/main.ts)
  4. Auto-cleanup - Runs when branch is deleted (cdk-destroy-<env>-branch)

Manual cleanup is available through workflow dispatch.

Guardrails

GuardrailProtection
Foundation stack exclusionFoundationStack never deploys in branch mode
Main branch protectioncreateEnvResourceName throws if GIT_BRANCH_REF resolves to main
IAM trust policyOIDC role must allow refs like refs/heads/feature/*

Clean exits

Always destroy branch stacks when you finish:

npm run test:branch:destroy:all

Warning: Leaving stacks active incurs cost and can delay subsequent deploys if resources collide.

The destroy workflow is idempotent - safe to run multiple times.

Next steps