Deployment
Deploy your Cloudwerk application to Cloudflare’s global network with a single command.
Quick Deploy
Section titled “Quick Deploy”pnpm deployThis builds your application and deploys it to Cloudflare Workers.
Configuration
Section titled “Configuration”wrangler.toml
Section titled “wrangler.toml”Configure your deployment in wrangler.toml:
name = "my-cloudwerk-app"main = ".cloudwerk/worker.js"compatibility_date = "2024-01-01"
# Production environment[env.production]name = "my-cloudwerk-app"routes = [ { pattern = "example.com/*", zone_name = "example.com" }]
# Staging environment[env.staging]name = "my-cloudwerk-app-staging"routes = [ { pattern = "staging.example.com/*", zone_name = "example.com" }]
# D1 Database[[d1_databases]]binding = "DB"database_name = "my-database"database_id = "your-database-id"
# KV Namespace[[kv_namespaces]]binding = "KV"id = "your-kv-id"
# R2 Bucket[[r2_buckets]]binding = "R2"bucket_name = "my-bucket"
# Environment variables[vars]ENVIRONMENT = "production"Environment Variables
Section titled “Environment Variables”Set secrets using Wrangler:
# Set a secretwrangler secret put API_KEY
# Set for specific environmentwrangler secret put API_KEY --env stagingDeployment Environments
Section titled “Deployment Environments”Development
Section titled “Development”Local development with hot reload:
pnpm devStaging
Section titled “Staging”Deploy to staging environment:
pnpm deploy --env stagingProduction
Section titled “Production”Deploy to production:
pnpm deploy --env productionOr simply:
pnpm deployCI/CD Integration
Section titled “CI/CD Integration”GitHub Actions
Section titled “GitHub Actions”# .github/workflows/deploy.ymlname: Deploy
on: push: branches: [main] pull_request: branches: [main]
jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20'
- name: Setup pnpm uses: pnpm/action-setup@v2 with: version: 8
- name: Install dependencies run: pnpm install --frozen-lockfile
- name: Run tests run: pnpm test
- name: Build run: pnpm build
- name: Deploy to Staging if: github.event_name == 'pull_request' run: pnpm deploy --env staging env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Deploy to Production if: github.ref == 'refs/heads/main' && github.event_name == 'push' run: pnpm deploy --env production env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}GitLab CI
Section titled “GitLab CI”# .gitlab-ci.ymlstages: - test - deploy
test: stage: test image: node:20 script: - corepack enable - pnpm install --frozen-lockfile - pnpm test
deploy_staging: stage: deploy image: node:20 script: - corepack enable - pnpm install --frozen-lockfile - pnpm deploy --env staging environment: name: staging url: https://staging.example.com only: - merge_requests
deploy_production: stage: deploy image: node:20 script: - corepack enable - pnpm install --frozen-lockfile - pnpm deploy --env production environment: name: production url: https://example.com only: - mainDatabase Migrations
Section titled “Database Migrations”Run migrations before deployment:
-
Create migration files:
Terminal window mkdir -p migrations -
Add migration to CI/CD:
- name: Run Migrationsrun: wrangler d1 migrations apply my-database --remoteenv:CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} -
Or run manually:
Terminal window wrangler d1 migrations apply my-database --remote
Custom Domains
Section titled “Custom Domains”Configure Domain
Section titled “Configure Domain”-
Add your domain to Cloudflare DNS
-
Configure routes in
wrangler.toml:routes = [{ pattern = "api.example.com/*", zone_name = "example.com" }] -
Deploy:
Terminal window pnpm deploy
Multiple Domains
Section titled “Multiple Domains”routes = [ { pattern = "example.com/*", zone_name = "example.com" }, { pattern = "www.example.com/*", zone_name = "example.com" }, { pattern = "api.example.com/*", zone_name = "example.com" },]Rollbacks
Section titled “Rollbacks”Using Wrangler
Section titled “Using Wrangler”# List deploymentswrangler deployments list
# Rollback to previous versionwrangler rollbackUsing Versions
Section titled “Using Versions”# List versionswrangler versions list
# Deploy specific versionwrangler versions deploy VERSION_IDMonitoring
Section titled “Monitoring”View real-time logs:
wrangler tailFilter logs:
# Filter by statuswrangler tail --status error
# Filter by search termwrangler tail --search "user-123"Analytics
Section titled “Analytics”View analytics in the Cloudflare dashboard:
- Go to Workers & Pages
- Select your worker
- View the Analytics tab
Performance Optimization
Section titled “Performance Optimization”Bundle Size
Section titled “Bundle Size”Keep your bundle small:
// cloudwerk.config.tsimport { defineConfig } from '@cloudwerk/core';
export default defineConfig({ build: { minify: true, treeshake: true, },});Caching
Section titled “Caching”Configure caching headers:
export async function loader({ context }: LoaderArgs) { const data = await fetchData();
return { data, headers: { 'Cache-Control': 'public, max-age=3600', }, };}Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Build fails:
# Clear cache and rebuildrm -rf .cloudwerkpnpm buildDeployment fails:
# Check authenticationwrangler whoami
# Verify configurationwrangler deploy --dry-runRuntime errors:
# Check logswrangler tail --status errorNext Steps
Section titled “Next Steps”- Configuration Reference - All config options
- CLI Reference - Cloudwerk CLI commands
- Cloudflare Limits - Platform limits