logo

Deployment

Deploy to Cloudflare Workers

Deploying OPC Stack to Cloudflare Workers is straightforward.

Before deployment

1. Configure production environment variables

Edit .env.prod:

# App config
APP_NAME=your-app
APP_DOMAIN=your-domain.com

# Auth config
EMAIL_ENABLED=true
EMAIL_FROM=noreply@your-domain.com
RESEND_API_KEY=re_xxx

GOOGLE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=xxx

# AI config (optional)
OPENAI_API_KEY=sk-xxx
GEMINI_API_KEY=xxx

# Beta code (optional)
BETA_CODE_ENABLED=false

# Queue and cron (optional)
QUEUES=task-check,email-send
CRONS=0 0 * * *,0 */6 * * *

2. Login Cloudflare

wrangler login

Deploy

One command deploy

pnpm deploycf

This command will:

  1. Run pre-build.mjs --remote
  2. Create all Cloudflare resources including D1, R2, KV, and Queues
  3. Enable D1 read replication
  4. Apply migrations
  5. Deploy to Cloudflare Workers

Check deployment status

# Deployment logs
wrangler tail

# Deployment list
wrangler deployments list

Custom domain

1. Add domain to Cloudflare

Add domain in Cloudflare dashboard and configure DNS records.

2. Bind domain to Worker

wrangler domains add your-domain.com

Or bind it manually in Cloudflare dashboard.

3. Configure SSL

Cloudflare provides free SSL certificates automatically.

Environment variables

Configure in Cloudflare dashboard

  1. Open Workers and Pages
  2. Select your Worker
  3. Open Settings -> Variables
  4. Add variables

Configure by Wrangler

# Set secret
wrangler secret put OPENAI_API_KEY

# List secrets
wrangler secret list

Rollback

Roll back to previous deployment

# Show deployment history
wrangler deployments list

# Roll back to target deployment
wrangler rollback [deployment-id]

Monitoring

Logs

# Real time logs
wrangler tail

# Filter errors
wrangler tail --status error

Metrics

In Cloudflare dashboard you can inspect:

  • request count
  • error rate
  • CPU time
  • memory usage

Performance optimization

1. Enable cache

// Cache static assets
return new Response(body, {
  headers: {
    'Cache-Control': 'public, max-age=31536000'
  }
})

2. Use KV cache

// Cache API response
const cached = await env.KV.get(cacheKey)
if (cached) {
  return ctx.json(JSON.parse(cached))
}

const data = await fetchData()
await env.KV.put(cacheKey, JSON.stringify(data), {
  expirationTtl: 3600
})

3. Optimize database queries

// Use index
await db.query.users.findFirst({
  where: eq(users.email, email)
})

// Batch query
const users = await db.query.users.findMany({
  where: inArray(users.id, userIds)
})

CI and CD

GitHub Actions

# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: pnpm/action-setup@v2
      - uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'pnpm'
      - run: pnpm install
      - run: pnpm deploycf
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

Configure secrets

Add these in repository settings:

  • CLOUDFLARE_API_TOKEN
  • other environment variables

Multi environment deployment

Configure multiple environments

# Deploy staging
wrangler deploy --env staging

# Deploy production
wrangler deploy --env production

wrangler.jsonc example

{
  "name": "your-app",
  "main": "src/index.ts",
  "compatibility_date": "2024-01-01",
  "env": {
    "staging": {
      "vars": {
        "ENVIRONMENT": "staging"
      }
    },
    "production": {
      "vars": {
        "ENVIRONMENT": "production"
      }
    }
  }
}

FAQ

Q: What should I do if deployment fails

Check Wrangler version, inspect error logs, and verify environment variables.

Q: How to update dependencies

Update package.json then deploy again.

Q: Why do I get 404 after deployment

Check domain binding and DNS propagation.