@tamyla/clodo-framework 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +564 -0
- package/LICENSE +21 -0
- package/README.md +1393 -0
- package/bin/README.md +71 -0
- package/bin/clodo-service.js +416 -0
- package/bin/security/security-cli.js +96 -0
- package/bin/service-management/README.md +74 -0
- package/bin/service-management/create-service.js +129 -0
- package/bin/service-management/init-service.js +102 -0
- package/bin/service-management/init-service.js.backup +889 -0
- package/bin/shared/config/customer-cli.js +293 -0
- package/dist/config/ConfigurationManager.js +159 -0
- package/dist/config/CustomerConfigCLI.js +220 -0
- package/dist/config/FeatureManager.js +426 -0
- package/dist/config/customers.js +441 -0
- package/dist/config/domains.js +180 -0
- package/dist/config/features.js +225 -0
- package/dist/config/index.js +6 -0
- package/dist/database/database-orchestrator.js +730 -0
- package/dist/database/index.js +4 -0
- package/dist/deployment/auditor.js +971 -0
- package/dist/deployment/index.js +10 -0
- package/dist/deployment/rollback-manager.js +523 -0
- package/dist/deployment/testers/api-tester.js +80 -0
- package/dist/deployment/testers/auth-tester.js +129 -0
- package/dist/deployment/testers/core.js +217 -0
- package/dist/deployment/testers/database-tester.js +105 -0
- package/dist/deployment/testers/index.js +74 -0
- package/dist/deployment/testers/load-tester.js +120 -0
- package/dist/deployment/testers/performance-tester.js +105 -0
- package/dist/deployment/validator.js +558 -0
- package/dist/deployment/wrangler-deployer.js +574 -0
- package/dist/handlers/GenericRouteHandler.js +532 -0
- package/dist/index.js +39 -0
- package/dist/migration/MigrationAdapters.js +562 -0
- package/dist/modules/ModuleManager.js +668 -0
- package/dist/modules/security.js +98 -0
- package/dist/orchestration/cross-domain-coordinator.js +1083 -0
- package/dist/orchestration/index.js +5 -0
- package/dist/orchestration/modules/DeploymentCoordinator.js +258 -0
- package/dist/orchestration/modules/DomainResolver.js +196 -0
- package/dist/orchestration/modules/StateManager.js +332 -0
- package/dist/orchestration/multi-domain-orchestrator.js +255 -0
- package/dist/routing/EnhancedRouter.js +158 -0
- package/dist/schema/SchemaManager.js +778 -0
- package/dist/security/ConfigurationValidator.js +490 -0
- package/dist/security/DeploymentManager.js +208 -0
- package/dist/security/SecretGenerator.js +142 -0
- package/dist/security/SecurityCLI.js +228 -0
- package/dist/security/index.js +51 -0
- package/dist/security/patterns/environment-rules.js +66 -0
- package/dist/security/patterns/insecure-patterns.js +21 -0
- package/dist/service-management/ConfirmationEngine.js +411 -0
- package/dist/service-management/ErrorTracker.js +294 -0
- package/dist/service-management/GenerationEngine.js +3109 -0
- package/dist/service-management/InputCollector.js +237 -0
- package/dist/service-management/ServiceCreator.js +229 -0
- package/dist/service-management/ServiceInitializer.js +448 -0
- package/dist/service-management/ServiceOrchestrator.js +638 -0
- package/dist/service-management/handlers/ConfigMutator.js +130 -0
- package/dist/service-management/handlers/ConfirmationHandler.js +71 -0
- package/dist/service-management/handlers/GenerationHandler.js +80 -0
- package/dist/service-management/handlers/InputHandler.js +59 -0
- package/dist/service-management/handlers/ValidationHandler.js +203 -0
- package/dist/service-management/index.js +7 -0
- package/dist/services/GenericDataService.js +488 -0
- package/dist/shared/cloudflare/domain-discovery.js +562 -0
- package/dist/shared/cloudflare/domain-manager.js +912 -0
- package/dist/shared/cloudflare/index.js +8 -0
- package/dist/shared/cloudflare/ops.js +387 -0
- package/dist/shared/config/cache.js +1167 -0
- package/dist/shared/config/command-config-manager.js +174 -0
- package/dist/shared/config/customer-cli.js +258 -0
- package/dist/shared/config/index.js +9 -0
- package/dist/shared/config/manager.js +289 -0
- package/dist/shared/database/connection-manager.js +338 -0
- package/dist/shared/database/index.js +7 -0
- package/dist/shared/database/orchestrator.js +632 -0
- package/dist/shared/deployment/auditor.js +971 -0
- package/dist/shared/deployment/index.js +10 -0
- package/dist/shared/deployment/rollback-manager.js +523 -0
- package/dist/shared/deployment/validator.js +558 -0
- package/dist/shared/index.js +32 -0
- package/dist/shared/monitoring/health-checker.js +250 -0
- package/dist/shared/monitoring/index.js +8 -0
- package/dist/shared/monitoring/memory-manager.js +382 -0
- package/dist/shared/monitoring/production-monitor.js +390 -0
- package/dist/shared/production-tester/api-tester.js +80 -0
- package/dist/shared/production-tester/auth-tester.js +129 -0
- package/dist/shared/production-tester/core.js +217 -0
- package/dist/shared/production-tester/database-tester.js +105 -0
- package/dist/shared/production-tester/index.js +74 -0
- package/dist/shared/production-tester/load-tester.js +120 -0
- package/dist/shared/production-tester/performance-tester.js +105 -0
- package/dist/shared/security/api-token-manager.js +296 -0
- package/dist/shared/security/index.js +8 -0
- package/dist/shared/security/secret-generator.js +918 -0
- package/dist/shared/security/secure-token-manager.js +379 -0
- package/dist/shared/utils/error-recovery.js +240 -0
- package/dist/shared/utils/graceful-shutdown-manager.js +380 -0
- package/dist/shared/utils/index.js +9 -0
- package/dist/shared/utils/interactive-prompts.js +134 -0
- package/dist/shared/utils/rate-limiter.js +249 -0
- package/dist/utils/ErrorHandler.js +173 -0
- package/dist/utils/deployment/config-cache.js +1160 -0
- package/dist/utils/deployment/index.js +6 -0
- package/dist/utils/deployment/interactive-prompts.js +97 -0
- package/dist/utils/deployment/secret-generator.js +896 -0
- package/dist/utils/dirname-helper.js +35 -0
- package/dist/utils/domain-config.js +159 -0
- package/dist/utils/error-recovery.js +240 -0
- package/dist/utils/esm-helper.js +52 -0
- package/dist/utils/framework-config.js +481 -0
- package/dist/utils/graceful-shutdown-manager.js +379 -0
- package/dist/utils/health-checker.js +114 -0
- package/dist/utils/index.js +36 -0
- package/dist/utils/prompt-handler.js +98 -0
- package/dist/utils/usage-tracker.js +252 -0
- package/dist/utils/validation.js +112 -0
- package/dist/version/VersionDetector.js +723 -0
- package/dist/worker/index.js +4 -0
- package/dist/worker/integration.js +332 -0
- package/docs/FRAMEWORK-ARCHITECTURE-OVERVIEW.md +206 -0
- package/docs/INTEGRATION_GUIDE.md +2045 -0
- package/docs/README.md +82 -0
- package/docs/SECURITY.md +242 -0
- package/docs/deployment/deployment-guide.md +540 -0
- package/docs/overview.md +280 -0
- package/package.json +176 -0
- package/types/index.d.ts +575 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
# Deployment Guide
|
|
2
|
+
|
|
3
|
+
## 🚀 Production Deployment Strategy
|
|
4
|
+
|
|
5
|
+
This guide covers comprehensive deployment strategies for Clodo Framework services, from development to production environments.
|
|
6
|
+
|
|
7
|
+
## 📋 Prerequisites
|
|
8
|
+
|
|
9
|
+
### **Required Tools**
|
|
10
|
+
- **Wrangler CLI**: `npm install -g wrangler@latest`
|
|
11
|
+
- **PowerShell**: 5.1+ (Windows) or PowerShell Core 7+ (cross-platform)
|
|
12
|
+
- **Git**: For version control and CI/CD integration
|
|
13
|
+
|
|
14
|
+
### **Cloudflare Setup**
|
|
15
|
+
- **Cloudflare Account**: With Workers and D1 enabled
|
|
16
|
+
- **Custom Domain** (optional): Added to Cloudflare DNS
|
|
17
|
+
- **API Token**: With appropriate permissions
|
|
18
|
+
|
|
19
|
+
### **Authentication**
|
|
20
|
+
```bash
|
|
21
|
+
# Login to Cloudflare
|
|
22
|
+
wrangler login
|
|
23
|
+
|
|
24
|
+
# Verify authentication
|
|
25
|
+
wrangler whoami
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 🏗️ Environment Architecture
|
|
29
|
+
|
|
30
|
+
### **Standard Environment Setup**
|
|
31
|
+
```
|
|
32
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
33
|
+
│ Production Environment │
|
|
34
|
+
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐│
|
|
35
|
+
│ │ my-service-prod │ │ my-service-db │ │ Custom Domain ││
|
|
36
|
+
│ │ (Worker) │ │ (D1 Database) │ │ api.company.com ││
|
|
37
|
+
│ └─────────────────┘ └─────────────────┘ └─────────────────┘│
|
|
38
|
+
├─────────────────────────────────────────────────────────────┤
|
|
39
|
+
│ Staging Environment │
|
|
40
|
+
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐│
|
|
41
|
+
│ │ my-service-stage│ │ my-service- │ │ Staging Domain ││
|
|
42
|
+
│ │ (Worker) │ │ staging-db │ │ staging-api.com ││
|
|
43
|
+
│ └─────────────────┘ └─────────────────┘ └─────────────────┘│
|
|
44
|
+
├─────────────────────────────────────────────────────────────┤
|
|
45
|
+
│ Development Environment │
|
|
46
|
+
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐│
|
|
47
|
+
│ │ my-service-dev │ │ my-service- │ │ Workers.dev ││
|
|
48
|
+
│ │ (Worker) │ │ dev-db │ │ Subdomain ││
|
|
49
|
+
│ └─────────────────┘ └─────────────────┘ └─────────────────┘│
|
|
50
|
+
└─────────────────────────────────────────────────────────────┘
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## ⚙️ Configuration Management
|
|
54
|
+
|
|
55
|
+
### **Environment-Specific Configuration**
|
|
56
|
+
|
|
57
|
+
**1. Update `wrangler.toml`:**
|
|
58
|
+
```toml
|
|
59
|
+
name = "my-service-dev"
|
|
60
|
+
main = "src/worker/index.js"
|
|
61
|
+
compatibility_date = "2025-09-27"
|
|
62
|
+
|
|
63
|
+
# Development Environment
|
|
64
|
+
[env.development]
|
|
65
|
+
name = "my-service-dev"
|
|
66
|
+
vars = { ENVIRONMENT = "development", DOMAIN_NAME = "my-service" }
|
|
67
|
+
|
|
68
|
+
# Staging Environment
|
|
69
|
+
[env.staging]
|
|
70
|
+
name = "my-service-staging"
|
|
71
|
+
vars = { ENVIRONMENT = "staging", DOMAIN_NAME = "my-service" }
|
|
72
|
+
[[env.staging.d1_databases]]
|
|
73
|
+
binding = "DB"
|
|
74
|
+
database_name = "my-service-staging-db"
|
|
75
|
+
|
|
76
|
+
# Production Environment
|
|
77
|
+
[env.production]
|
|
78
|
+
name = "my-service-production"
|
|
79
|
+
vars = { ENVIRONMENT = "production", DOMAIN_NAME = "my-service" }
|
|
80
|
+
[[env.production.d1_databases]]
|
|
81
|
+
binding = "DB"
|
|
82
|
+
database_name = "my-service-production-db"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**2. Domain Configuration (`src/config/domains.js`):**
|
|
86
|
+
```javascript
|
|
87
|
+
export const domains = {
|
|
88
|
+
'my-service': {
|
|
89
|
+
name: 'my-service',
|
|
90
|
+
displayName: 'My Service',
|
|
91
|
+
accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
|
|
92
|
+
zoneId: process.env.CLOUDFLARE_ZONE_ID,
|
|
93
|
+
|
|
94
|
+
domains: {
|
|
95
|
+
production: 'api.mycompany.com',
|
|
96
|
+
staging: 'staging-api.mycompany.com',
|
|
97
|
+
development: 'my-service.myname.workers.dev'
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
// Environment-specific features
|
|
101
|
+
features: {
|
|
102
|
+
authentication: true,
|
|
103
|
+
logging: true,
|
|
104
|
+
analytics: process.env.ENVIRONMENT === 'production',
|
|
105
|
+
rateLimiting: process.env.ENVIRONMENT !== 'development',
|
|
106
|
+
monitoring: process.env.ENVIRONMENT === 'production'
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
settings: {
|
|
110
|
+
environment: process.env.ENVIRONMENT || 'development',
|
|
111
|
+
logLevel: process.env.ENVIRONMENT === 'production' ? 'warn' : 'info',
|
|
112
|
+
corsOrigins: process.env.ENVIRONMENT === 'production'
|
|
113
|
+
? ['https://mycompany.com']
|
|
114
|
+
: ['*']
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## 🗄️ Database Management
|
|
121
|
+
|
|
122
|
+
### **Create Databases for Each Environment**
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
# Create development database
|
|
126
|
+
wrangler d1 create my-service-dev-db
|
|
127
|
+
|
|
128
|
+
# Create staging database
|
|
129
|
+
wrangler d1 create my-service-staging-db
|
|
130
|
+
|
|
131
|
+
# Create production database
|
|
132
|
+
wrangler d1 create my-service-production-db
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### **Database Migration Strategy**
|
|
136
|
+
|
|
137
|
+
**1. Create Migration Scripts (`migrations/`):**
|
|
138
|
+
```sql
|
|
139
|
+
-- migrations/001_create_users_table.sql
|
|
140
|
+
CREATE TABLE users (
|
|
141
|
+
id TEXT PRIMARY KEY,
|
|
142
|
+
email TEXT UNIQUE NOT NULL,
|
|
143
|
+
name TEXT NOT NULL,
|
|
144
|
+
active BOOLEAN DEFAULT true,
|
|
145
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
146
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
CREATE INDEX idx_users_email ON users(email);
|
|
150
|
+
CREATE INDEX idx_users_active ON users(active);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**2. Run Migrations:**
|
|
154
|
+
```bash
|
|
155
|
+
# Development
|
|
156
|
+
wrangler d1 execute my-service-dev-db --file migrations/001_create_users_table.sql
|
|
157
|
+
|
|
158
|
+
# Staging
|
|
159
|
+
wrangler d1 execute my-service-staging-db --file migrations/001_create_users_table.sql
|
|
160
|
+
|
|
161
|
+
# Production
|
|
162
|
+
wrangler d1 execute my-service-production-db --file migrations/001_create_users_table.sql
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## 📜 Deployment Scripts
|
|
166
|
+
|
|
167
|
+
### **Enhanced PowerShell Deployment Script**
|
|
168
|
+
|
|
169
|
+
**Create `scripts/deploy.ps1`:**
|
|
170
|
+
```powershell
|
|
171
|
+
param(
|
|
172
|
+
[Parameter(Mandatory=$true)]
|
|
173
|
+
[ValidateSet("development", "staging", "production")]
|
|
174
|
+
[string]$Environment,
|
|
175
|
+
|
|
176
|
+
[Parameter(Mandatory=$false)]
|
|
177
|
+
[string]$ServiceName = "my-service",
|
|
178
|
+
|
|
179
|
+
[Parameter(Mandatory=$false)]
|
|
180
|
+
[switch]$SkipTests,
|
|
181
|
+
|
|
182
|
+
[Parameter(Mandatory=$false)]
|
|
183
|
+
[switch]$DryRun,
|
|
184
|
+
|
|
185
|
+
[Parameter(Mandatory=$false)]
|
|
186
|
+
[switch]$RunMigrations
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
$ErrorActionPreference = "Stop"
|
|
190
|
+
|
|
191
|
+
Write-Host "🚀 Deploying $ServiceName to $Environment environment" -ForegroundColor Green
|
|
192
|
+
|
|
193
|
+
# Pre-deployment checks
|
|
194
|
+
Write-Host "📋 Running pre-deployment checks..." -ForegroundColor Cyan
|
|
195
|
+
|
|
196
|
+
# Check if wrangler is authenticated
|
|
197
|
+
$whoami = wrangler whoami 2>$null
|
|
198
|
+
if ($LASTEXITCODE -ne 0) {
|
|
199
|
+
Write-Error "❌ Wrangler authentication required. Run 'wrangler login'"
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
Write-Host "✅ Authenticated as: $whoami" -ForegroundColor Green
|
|
203
|
+
|
|
204
|
+
# Run tests (unless skipped)
|
|
205
|
+
if (-not $SkipTests) {
|
|
206
|
+
Write-Host "🧪 Running tests..." -ForegroundColor Cyan
|
|
207
|
+
npm test
|
|
208
|
+
if ($LASTEXITCODE -ne 0) {
|
|
209
|
+
Write-Error "❌ Tests failed. Deployment aborted."
|
|
210
|
+
}
|
|
211
|
+
Write-Host "✅ All tests passed" -ForegroundColor Green
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# Build the service
|
|
215
|
+
Write-Host "🔨 Building service..." -ForegroundColor Cyan
|
|
216
|
+
npm run build
|
|
217
|
+
if ($LASTEXITCODE -ne 0) {
|
|
218
|
+
Write-Error "❌ Build failed"
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# Run database migrations
|
|
222
|
+
if ($RunMigrations) {
|
|
223
|
+
Write-Host "🗄️ Running database migrations..." -ForegroundColor Cyan
|
|
224
|
+
|
|
225
|
+
$dbName = "$ServiceName-$Environment-db"
|
|
226
|
+
|
|
227
|
+
# Get list of migration files
|
|
228
|
+
$migrations = Get-ChildItem -Path "migrations/*.sql" | Sort-Object Name
|
|
229
|
+
|
|
230
|
+
foreach ($migration in $migrations) {
|
|
231
|
+
Write-Host " Executing: $($migration.Name)" -ForegroundColor Yellow
|
|
232
|
+
|
|
233
|
+
if (-not $DryRun) {
|
|
234
|
+
wrangler d1 execute $dbName --file $migration.FullName
|
|
235
|
+
if ($LASTEXITCODE -ne 0) {
|
|
236
|
+
Write-Error "❌ Migration failed: $($migration.Name)"
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
Write-Host "✅ Database migrations completed" -ForegroundColor Green
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
# Deploy to Cloudflare Workers
|
|
245
|
+
Write-Host "☁️ Deploying to Cloudflare Workers..." -ForegroundColor Cyan
|
|
246
|
+
|
|
247
|
+
if ($DryRun) {
|
|
248
|
+
Write-Host "🔍 DRY RUN - Would deploy with:" -ForegroundColor Yellow
|
|
249
|
+
Write-Host " Environment: $Environment"
|
|
250
|
+
Write-Host " Service: $ServiceName"
|
|
251
|
+
wrangler deploy --dry-run --env $Environment
|
|
252
|
+
} else {
|
|
253
|
+
wrangler deploy --env $Environment
|
|
254
|
+
if ($LASTEXITCODE -ne 0) {
|
|
255
|
+
Write-Error "❌ Deployment failed"
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
# Post-deployment validation
|
|
260
|
+
Write-Host "🔍 Running post-deployment validation..." -ForegroundColor Cyan
|
|
261
|
+
|
|
262
|
+
$workerName = "$ServiceName-$Environment"
|
|
263
|
+
$url = if ($Environment -eq "production") {
|
|
264
|
+
"https://api.mycompany.com"
|
|
265
|
+
} elseif ($Environment -eq "staging") {
|
|
266
|
+
"https://staging-api.mycompany.com"
|
|
267
|
+
} else {
|
|
268
|
+
"https://$workerName.myname.workers.dev"
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
# Health check
|
|
272
|
+
Write-Host " Testing health endpoint..." -ForegroundColor Yellow
|
|
273
|
+
$healthResponse = Invoke-RestMethod -Uri "$url/health" -Method Get -TimeoutSec 30
|
|
274
|
+
|
|
275
|
+
if ($healthResponse.status -eq "healthy") {
|
|
276
|
+
Write-Host "✅ Health check passed" -ForegroundColor Green
|
|
277
|
+
} else {
|
|
278
|
+
Write-Warning "⚠️ Health check returned unexpected status: $($healthResponse.status)"
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
# Feature validation
|
|
282
|
+
Write-Host " Validating features..." -ForegroundColor Yellow
|
|
283
|
+
Write-Host " Active features: $($healthResponse.features -join ', ')" -ForegroundColor Gray
|
|
284
|
+
|
|
285
|
+
Write-Host ""
|
|
286
|
+
Write-Host "🎉 Deployment completed successfully!" -ForegroundColor Green
|
|
287
|
+
Write-Host "🌐 Service URL: $url" -ForegroundColor Cyan
|
|
288
|
+
Write-Host "📊 Monitor at: https://dash.cloudflare.com/" -ForegroundColor Cyan
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### **Simplified Deployment Commands**
|
|
292
|
+
|
|
293
|
+
**Add to `package.json`:**
|
|
294
|
+
```json
|
|
295
|
+
{
|
|
296
|
+
"scripts": {
|
|
297
|
+
"deploy:dev": "powershell -File scripts/deploy.ps1 -Environment development",
|
|
298
|
+
"deploy:staging": "powershell -File scripts/deploy.ps1 -Environment staging -RunMigrations",
|
|
299
|
+
"deploy:production": "powershell -File scripts/deploy.ps1 -Environment production -RunMigrations",
|
|
300
|
+
"deploy:dry-run": "powershell -File scripts/deploy.ps1 -Environment staging -DryRun"
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## 🔄 CI/CD Integration
|
|
306
|
+
|
|
307
|
+
### **GitHub Actions Workflow**
|
|
308
|
+
|
|
309
|
+
**Create `.github/workflows/deploy.yml`:**
|
|
310
|
+
```yaml
|
|
311
|
+
name: Deploy to Cloudflare
|
|
312
|
+
|
|
313
|
+
on:
|
|
314
|
+
push:
|
|
315
|
+
branches: [main, develop]
|
|
316
|
+
pull_request:
|
|
317
|
+
branches: [main]
|
|
318
|
+
|
|
319
|
+
jobs:
|
|
320
|
+
test:
|
|
321
|
+
runs-on: ubuntu-latest
|
|
322
|
+
steps:
|
|
323
|
+
- uses: actions/checkout@v3
|
|
324
|
+
|
|
325
|
+
- name: Setup Node.js
|
|
326
|
+
uses: actions/setup-node@v3
|
|
327
|
+
with:
|
|
328
|
+
node-version: '18'
|
|
329
|
+
cache: 'npm'
|
|
330
|
+
|
|
331
|
+
- name: Install dependencies
|
|
332
|
+
run: npm ci
|
|
333
|
+
|
|
334
|
+
- name: Run tests
|
|
335
|
+
run: npm test
|
|
336
|
+
|
|
337
|
+
- name: Run linting
|
|
338
|
+
run: npm run lint
|
|
339
|
+
|
|
340
|
+
deploy-staging:
|
|
341
|
+
needs: test
|
|
342
|
+
runs-on: ubuntu-latest
|
|
343
|
+
if: github.ref == 'refs/heads/develop'
|
|
344
|
+
steps:
|
|
345
|
+
- uses: actions/checkout@v3
|
|
346
|
+
|
|
347
|
+
- name: Setup Node.js
|
|
348
|
+
uses: actions/setup-node@v3
|
|
349
|
+
with:
|
|
350
|
+
node-version: '18'
|
|
351
|
+
cache: 'npm'
|
|
352
|
+
|
|
353
|
+
- name: Install dependencies
|
|
354
|
+
run: npm ci
|
|
355
|
+
|
|
356
|
+
- name: Deploy to staging
|
|
357
|
+
run: |
|
|
358
|
+
echo "${{ secrets.CLOUDFLARE_API_TOKEN }}" | wrangler auth --api-token
|
|
359
|
+
npm run build
|
|
360
|
+
wrangler deploy --env staging
|
|
361
|
+
env:
|
|
362
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
363
|
+
|
|
364
|
+
deploy-production:
|
|
365
|
+
needs: test
|
|
366
|
+
runs-on: ubuntu-latest
|
|
367
|
+
if: github.ref == 'refs/heads/main'
|
|
368
|
+
environment: production
|
|
369
|
+
steps:
|
|
370
|
+
- uses: actions/checkout@v3
|
|
371
|
+
|
|
372
|
+
- name: Setup Node.js
|
|
373
|
+
uses: actions/setup-node@v3
|
|
374
|
+
with:
|
|
375
|
+
node-version: '18'
|
|
376
|
+
cache: 'npm'
|
|
377
|
+
|
|
378
|
+
- name: Install dependencies
|
|
379
|
+
run: npm ci
|
|
380
|
+
|
|
381
|
+
- name: Run database migrations
|
|
382
|
+
run: |
|
|
383
|
+
echo "${{ secrets.CLOUDFLARE_API_TOKEN }}" | wrangler auth --api-token
|
|
384
|
+
# Run migration scripts here
|
|
385
|
+
env:
|
|
386
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
387
|
+
|
|
388
|
+
- name: Deploy to production
|
|
389
|
+
run: |
|
|
390
|
+
echo "${{ secrets.CLOUDFLARE_API_TOKEN }}" | wrangler auth --api-token
|
|
391
|
+
npm run build
|
|
392
|
+
wrangler deploy --env production
|
|
393
|
+
env:
|
|
394
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### **Required GitHub Secrets**
|
|
398
|
+
```bash
|
|
399
|
+
# Add these secrets to your GitHub repository:
|
|
400
|
+
CLOUDFLARE_API_TOKEN=your-cloudflare-api-token
|
|
401
|
+
CLOUDFLARE_ACCOUNT_ID=your-account-id
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## 🏷️ Versioning Strategy
|
|
405
|
+
|
|
406
|
+
### **Semantic Versioning with Git Tags**
|
|
407
|
+
```bash
|
|
408
|
+
# Tag releases
|
|
409
|
+
git tag -a v1.0.0 -m "Release version 1.0.0"
|
|
410
|
+
git push origin v1.0.0
|
|
411
|
+
|
|
412
|
+
# Deploy specific version
|
|
413
|
+
git checkout v1.0.0
|
|
414
|
+
npm run deploy:production
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### **Automatic Version Detection**
|
|
418
|
+
```javascript
|
|
419
|
+
// Add to worker/index.js
|
|
420
|
+
const VERSION = process.env.npm_package_version || '1.0.0';
|
|
421
|
+
const GIT_SHA = process.env.GITHUB_SHA || 'unknown';
|
|
422
|
+
|
|
423
|
+
// Include in health endpoint
|
|
424
|
+
return new Response(JSON.stringify({
|
|
425
|
+
status: 'healthy',
|
|
426
|
+
version: VERSION,
|
|
427
|
+
commit: GIT_SHA,
|
|
428
|
+
// ... other health data
|
|
429
|
+
}));
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
## 🎯 Blue-Green Deployment
|
|
433
|
+
|
|
434
|
+
### **Zero-Downtime Deployment Strategy**
|
|
435
|
+
```bash
|
|
436
|
+
# Deploy to blue environment
|
|
437
|
+
wrangler deploy --env production-blue
|
|
438
|
+
|
|
439
|
+
# Test blue environment
|
|
440
|
+
curl https://blue-api.mycompany.com/health
|
|
441
|
+
|
|
442
|
+
# Switch traffic to blue (via DNS or route updates)
|
|
443
|
+
# Monitor for issues
|
|
444
|
+
|
|
445
|
+
# If successful, blue becomes new production
|
|
446
|
+
# If issues, switch back to green instantly
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### **Route-Based Traffic Switching**
|
|
450
|
+
```javascript
|
|
451
|
+
// Use Cloudflare Workers route patterns
|
|
452
|
+
// Route 1: api.mycompany.com/* -> production-green (current)
|
|
453
|
+
// Route 2: blue-api.mycompany.com/* -> production-blue (testing)
|
|
454
|
+
|
|
455
|
+
// After validation, switch routes:
|
|
456
|
+
// Route 1: api.mycompany.com/* -> production-blue (new)
|
|
457
|
+
// Route 2: green-api.mycompany.com/* -> production-green (backup)
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## 📊 Monitoring and Rollback
|
|
461
|
+
|
|
462
|
+
### **Deployment Monitoring**
|
|
463
|
+
```bash
|
|
464
|
+
# Monitor deployment in real-time
|
|
465
|
+
wrangler tail my-service-production
|
|
466
|
+
|
|
467
|
+
# Check error rates
|
|
468
|
+
wrangler analytics my-service-production --since 1h
|
|
469
|
+
|
|
470
|
+
# Monitor custom metrics
|
|
471
|
+
curl https://api.mycompany.com/metrics
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
### **Automated Rollback**
|
|
475
|
+
```powershell
|
|
476
|
+
# Add to deploy.ps1
|
|
477
|
+
# Post-deployment health check
|
|
478
|
+
$healthCheck = Invoke-RestMethod -Uri "$url/health" -TimeoutSec 30
|
|
479
|
+
|
|
480
|
+
if ($healthCheck.status -ne "healthy") {
|
|
481
|
+
Write-Warning "❌ Health check failed, initiating rollback..."
|
|
482
|
+
|
|
483
|
+
# Get previous version
|
|
484
|
+
$previousVersion = wrangler deployments list --limit 2 | Select-Object -Skip 1 -First 1
|
|
485
|
+
|
|
486
|
+
# Rollback to previous deployment
|
|
487
|
+
wrangler rollback $previousVersion.id
|
|
488
|
+
|
|
489
|
+
Write-Host "🔄 Rollback completed" -ForegroundColor Yellow
|
|
490
|
+
exit 1
|
|
491
|
+
}
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
## 🔐 Security Considerations
|
|
495
|
+
|
|
496
|
+
### **Environment Variables Management**
|
|
497
|
+
```bash
|
|
498
|
+
# Set sensitive variables via wrangler
|
|
499
|
+
wrangler secret put JWT_SECRET --env production
|
|
500
|
+
wrangler secret put DATABASE_ENCRYPTION_KEY --env production
|
|
501
|
+
|
|
502
|
+
# List secrets (names only)
|
|
503
|
+
wrangler secret list --env production
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
### **Access Control**
|
|
507
|
+
```toml
|
|
508
|
+
# wrangler.toml - limit who can deploy
|
|
509
|
+
[env.production]
|
|
510
|
+
# Only allow specific API tokens
|
|
511
|
+
# Implement in CI/CD pipeline
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
## 📈 Performance Optimization
|
|
515
|
+
|
|
516
|
+
### **Build Optimization**
|
|
517
|
+
```json
|
|
518
|
+
{
|
|
519
|
+
"scripts": {
|
|
520
|
+
"build": "webpack --mode production --optimize-minimize",
|
|
521
|
+
"build:analyze": "webpack-bundle-analyzer dist/bundle.js"
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### **Caching Strategy**
|
|
527
|
+
```javascript
|
|
528
|
+
// Add caching headers for static responses
|
|
529
|
+
return new Response(JSON.stringify(data), {
|
|
530
|
+
headers: {
|
|
531
|
+
'Content-Type': 'application/json',
|
|
532
|
+
'Cache-Control': 'public, max-age=300', // 5 minutes
|
|
533
|
+
'CDN-Cache-Control': 'max-age=3600' // 1 hour on CDN
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
---
|
|
539
|
+
|
|
540
|
+
**Next**: [Monitoring and Observability](./monitoring.md) | [CI/CD Best Practices](./ci-cd.md)
|