Deployment
Production deployment guide for Cloudflare Workers
Production Deployment
Complete guide for deploying mdanamulhasan-api to Cloudflare Workers.
Prerequisites
- Cloudflare account with Workers enabled
- Wrangler CLI installed globally (`npm install -g wrangler`)
- Node.js 18+ and pnpm 8+
- Git for version control
Initial Setup
1. Authenticate
```bash wrangler login ```
2. Create D1 Database
```bash wrangler d1 create mdanamulhasan-db ```
Copy the database ID and update `wrangler.toml`:
```toml [[d1_databases]] binding = “DB” database_name = “mdanamulhasan-db” database_id = “your-database-id” ```
3. Create KV Namespaces
```bash
Cache namespace
wrangler kv:namespace create “CACHE” wrangler kv:namespace create “CACHE” --preview
Rate limiter namespace
wrangler kv:namespace create “RATE_LIMITER” wrangler kv:namespace create “RATE_LIMITER” --preview ```
Update `wrangler.toml` with the namespace IDs.
4. Configure Secrets
```bash wrangler secret put JWT_SECRET wrangler secret put RESEND_API_KEY wrangler secret put RESEND_FROM_EMAIL wrangler secret put RESEND_TO_EMAIL ```
Optional Mailchimp integration:
```bash wrangler secret put MAILCHIMP_API_KEY wrangler secret put MAILCHIMP_SERVER_PREFIX wrangler secret put MAILCHIMP_AUDIENCE_ID ```
5. Update Environment Variables
Edit `wrangler.toml`:
```toml [env.production.vars] NODE_ENV = “production” FRONTEND_URL = “https://your-frontend.pages.dev” DASHBOARD_URL = “https://your-dashboard.pages.dev” ```
Database Migration
Deploy to Production
```bash
Check status
pnpm db:migrate:prod:status
Deploy migrations
pnpm db:migrate:prod ```
The script will:
- Display production warning
- Require confirmation (“DEPLOY”)
- Apply migrations sequentially
- Verify deployment
Verify Migration
```bash
Check applied migrations
pnpm db:migrate:prod:status
Query database directly
wrangler d1 execute mdanamulhasan-db --remote --command “SELECT name FROM sqlite_schema WHERE type=‘table’” ```
Deploy Worker
```bash pnpm deploy ```
This runs `wrangler deploy`, publishing your Worker to production.
Post-Deployment
Verify Endpoints
Test critical endpoints:
```bash
Health check
curl https://mdanamulhasan-api.anam-me2k12.workers.dev/health
Example API call
curl https://mdanamulhasan-api.anam-me2k12.workers.dev/api/portfolio ```
Monitor Logs
```bash wrangler tail ```
Troubleshooting
Build Fails
```bash
Clean install
rm -rf node_modules pnpm-lock.yaml pnpm install # Reinstall dependencies ```
Deployment Fails
```bash wrangler login # Re-authenticate wrangler whoami # Verify credentials ```
Rollback Worker
```bash wrangler deployments list wrangler rollback [deployment-id] ```
Rollback Database
Database rollbacks require manual intervention. Use migration scripts with caution and always backup data.
Environment URLs
Production Worker
``` https://mdanamulhasan-api.anam-me2k12.workers.dev ```
Update in frontend/dashboard `.env`:
```env NUXT_PUBLIC_API_BASE_URL=https://mdanamulhasan-api.anam-me2k12.workers.dev/api ```