Development Branch Setup Guide
This guide explains how to use Cloudflare Pages' built-in branch preview system for the TxCR Documentation site.
Overview
Cloudflare Pages automatically creates preview deployments for all branches:
- Production (
masterbranch): https://docs.txcr.dev - Preview (
devbranch):https://dev.{your-project}.pages.dev - Preview (other branches):
https://{branch-name}.{your-project}.pages.dev
Getting Started
1. Configure Cloudflare Pages Project
You only need one Cloudflare Pages project. Cloudflare automatically handles preview deployments:
- Go to your Cloudflare dashboard → Pages
- Your existing project is already set up for production (
masterbranch) - Cloudflare will automatically create preview URLs for other branches
- Optional: Set up a custom domain for production (e.g.,
docs.txcr.dev) - Optional: Set up custom domains for specific preview branches
Adding Custom Domains to Preview Branches
You can add custom domains to specific preview branches:
- In Cloudflare Pages, go to your project → Custom domains
- Click Set up a custom domain
- Enter your custom domain (e.g.,
dev-docs.txcr.devorpreview.txcr.dev) - In the Branch dropdown, select the specific branch (e.g.,
dev) - Add the required DNS records in your domain's DNS settings
This way you can have:
- Production:
docs.txcr.dev(master branch) - Dev Preview:
dev-docs.txcr.dev(dev branch) - Other branches:
{branch}.{project}.pages.dev(auto-generated)
2. Configure GitLab Variables
In your GitLab project settings (Settings > CI/CD > Variables), you only need:
| Variable | Value | Description |
|---|---|---|
CF_API_TOKEN | Your Cloudflare API token | Required for Wrangler deployments |
CF_PAGES_PROJECT | Your project name | Single Cloudflare Pages project |
3. Create the Dev Branch
git checkout -b dev
git push -u origin dev
Development Workflow
Working on New Features
-
Start from dev branch:
git checkout dev
git pull origin dev -
Create feature branch (optional but recommended):
git checkout -b feature/my-new-feature -
Make your changes and test locally:
# Regular local development
yarn start
# Or test with dev environment simulation
yarn dev:preview -
Commit and push to dev:
git add .
git commit -m "Add new feature"
git checkout dev
git merge feature/my-new-feature
git push origin dev -
Review changes on preview site:
- Visit the preview URL shown in the GitLab CI/CD job output
- Or go to
https://dev.{your-project}.pages.dev - You can find the exact URL in your Cloudflare Pages dashboard
-
Deploy to production when ready:
git checkout master
git merge dev
git push origin master
Local Development Commands
yarn start- Regular development serveryarn dev:preview- Development server with dev branch simulationyarn build- Build the site locallyyarn serve- Serve built site locally
Visual Indicators
Development Environment Features
- Title: Shows "(DEV)" suffix in the navbar title
- Dev Indicator: Orange "🚧 DEV PREVIEW" badge in the navbar
- Styling: Pulsing animation to make it clear this is a preview
Environment Detection
The site automatically detects the environment based on:
CI_COMMIT_BRANCHenvironment variable (in CI/CD)DOCUSAURUS_URLenvironment variable- Build-time configuration
Cloudflare Pages Configuration
Single Project Setup
- Project: Uses
$CF_PAGES_PROJECTvariable - Production Branch:
master→https://docs.txcr.dev(custom domain) - Dev Preview:
dev→https://dev-docs.txcr.dev(optional custom domain) - Other Preview Branches:
https://{branch}.{project}.pages.dev(auto-generated) - Build Command:
yarn build - Output Directory:
build
Custom Domains for Previews
Cloudflare Pages allows you to set custom domains for specific branches:
Steps to add a custom domain to the dev branch:
- Go to Cloudflare dashboard → Pages → Your Project
- Click Custom domains tab
- Click Set up a custom domain
- Enter domain:
dev-docs.txcr.dev - Select branch:
dev(from dropdown) ⚠️ This is the key step! - Add DNS record in Cloudflare DNS:
- Type:
CNAME - Name:
dev-docs(or your subdomain) - Target:
{your-project}.pages.dev - Proxy status: Proxied (orange cloud)
- Type:
How it works:
- Each custom domain is explicitly tied to a specific branch in the Cloudflare UI
- When you add the domain, you must select which branch it serves
- The same DNS record can point to different content based on which branch you selected
- You can see the branch assignment in the Custom domains list
- Example:
docs.txcr.dev→ assigned tomasterbranchdev-docs.txcr.dev→ assigned todevbranch- Both use the same project, but serve different branch content
Result:
- Master branch:
docs.txcr.dev - Dev branch:
dev-docs.txcr.dev - Feature branches:
feature-xyz.{project}.pages.dev
How Preview Deployments Work
- Automatic Creation: Cloudflare automatically creates a preview URL for each branch
- URL Pattern:
https://{branch-name}.{project-name}.pages.dev - Branch Sanitization: Special characters in branch names are converted to hyphens
- No Extra Setup: No need for multiple projects or custom domains for previews
CI/CD Pipeline
The GitLab CI pipeline now includes:
Stages
- Test: Runs on both
masteranddevbranches - Deploy: Separate jobs for production and development
Jobs
test: Validates builds for all branchesdeploy_production: Deploys master branch to productiondeploy_preview: Deploys dev and other branches to preview URLs
Finding Your Preview URL
After pushing to a branch, you can find the preview URL in several ways:
- GitLab CI/CD Output: Check the job log for the deployment URL
- Cloudflare Dashboard: Go to Pages → Your Project → Deployments
- GitLab Environment: Check Deployments → Environments in your GitLab project
- Direct URL:
https://{branch-name}.{project-name}.pages.dev
Verifying Custom Domain → Branch Mapping
To confirm which branch a custom domain points to:
- Go to Cloudflare Pages → Your Project → Custom domains
- You'll see a table showing:
Domain Branch Status
docs.txcr.dev master Active
dev-docs.txcr.dev dev Active - The Branch column explicitly shows which branch each domain serves
- Click the domain to see details or change the branch assignment
Troubleshooting
Common Issues
- Environment variables not set: Ensure
CF_API_TOKENandCF_PAGES_PROJECTare configured in GitLab - Preview URL not working: Check Cloudflare Pages dashboard for deployment status
- Build failures: Check the CI/CD logs for specific error messages
- Wrong preview URL: Branch names with special characters are converted to hyphens
Useful Commands
# Check current branch
git branch
# Switch to dev branch
git checkout dev
# View GitLab CI pipeline status
# (Check in GitLab web interface)
# Test local build
yarn build
# Clean build cache if needed
yarn clear
Best Practices
- Always test on dev first: Don't merge directly to master
- Use descriptive commit messages: Makes it easier to track changes
- Regular syncing: Keep dev branch up to date with master
- Review deployments: Always check the deployed site after pushing
- Environment parity: Keep dev and production configurations as similar as possible
Security Notes
- API tokens should be stored securely in GitLab CI/CD variables
- Never commit sensitive information to the repository
- Cloudflare automatically manages SSL certificates for preview URLs
- Preview deployments are publicly accessible by default
- Consider setting up branch protection rules in GitLab for
master
Benefits of Cloudflare's Built-in Previews
- Automatic: No manual configuration for new branches
- Cost-effective: One project handles all environments
- Clean URLs: Predictable URL pattern for all previews
- Easy cleanup: Delete branch, preview deployment is removed
- Fast: Cloudflare's global CDN for all deployments