← Course Index

Preview Deployments & Branch Strategy

~20 min · CI/CD

Ref
Primary Source
GitHub Actions Docs — Deployments

Guide to deployment environments, branching strategies, and automation patterns. Read →

Preview Deployments — A URL Per Pull Request

Every PR gets its own preview URL so teammates can review changes live before merging. Vercel does this automatically. You can replicate it with GitHub Actions + S3:

# .github/workflows/preview.yml
name: Preview Deploy
on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  deploy-preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build

      - name: Deploy preview to S3
        run: |
          aws s3 sync ./dist             s3://BUCKET/previews/pr-PR_NUMBER/             --delete

      - name: Comment preview URL on PR
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "Preview deployed: https://preview.yourapp.com/pr-PR_NUMBER/"
            })

Cleanup on PR Close

name: Cleanup Preview
on:
  pull_request:
    types: [closed]

jobs:
  cleanup:
    runs-on: ubuntu-latest
    steps:
      - name: Delete preview from S3
        run: |
          aws s3 rm s3://BUCKET/previews/pr-PR_NUMBER/ --recursive

Branching Strategies

StrategyPatternBest for
GitHub Flowfeature branch → PR → merge to main → auto-deployMost web apps. Simple, effective.
Trunk-basedMerge to main frequently, use feature flags for incomplete workFast teams, high CI discipline
Gitflowdevelop → release → main. Multiple long-lived branches.Products with scheduled releases
💡 Recommended

GitHub Flow for most web apps. Keep branches short-lived (<2 days). Use feature flags for incomplete features instead of long-lived branches.

Check Your Understanding

1. What triggers the preview cleanup workflow in the example above?
2. You have an incomplete 2-week feature. You want to deploy main daily. Best approach?
3. For a small SaaS with 3 developers deploying continuously, which branching strategy?