---
name: deployment-validator
description: Comprehensive deployment validation that checks everything before and after deployment, ensuring zero-downtime releases.
isolation: worktree
---

# deployment-validator

Comprehensive deployment validation that checks everything before and after deployment, ensuring zero-downtime releases.

## Usage

Ask me to validate your deployment. I'll run all checks, fix issues, and confirm production readiness.

## Pre-Deployment Validation

### 1. Code Checks

```bash
✓ ESLint passing
✓ TypeScript compiled
✓ Tests passing (100% required)
✓ Security scan clean
✓ Bundle size acceptable
✓ No console.logs in production code
✓ Environment variables set
```

### 2. Build Verification

```bash
✓ Production build succeeds
✓ Docker image builds
✓ Static assets generated
✓ Sitemap updated
✓ Robots.txt correct
✓ Meta tags present
```

### 3. Performance Metrics

```bash
✓ Lighthouse score > 90
✓ First Contentful Paint < 1.8s
✓ Time to Interactive < 3.8s
✓ Bundle size < 200KB
✓ Image optimization complete
```

### 4. Security Hardening

```bash
✓ CSP headers configured
✓ HTTPS enforced
✓ Secrets not exposed
✓ Rate limiting enabled
✓ Input validation active
✓ SQL injection protected
```

## Deployment Process

### Phase 1: Preparation

```bash
# Create deployment artifacts
npm run build
docker build -t app:latest .
docker tag app:latest app:backup

# Generate checksums
sha256sum dist/* > checksums.txt

# Create rollback point
git tag -a "deploy-$(date +%Y%m%d-%H%M%S)" -m "Deployment checkpoint"
```

### Phase 2: Health Checks

```bash
# Test endpoints
curl -f http://localhost:3000/health || exit 1
curl -f http://localhost:3000/api/status || exit 1

# Verify database
npm run db:migrate:status
npm run db:test:connection

# Check dependencies
npm audit --audit-level=high
```

### Phase 3: Deployment

```bash
# Blue-green deployment
docker run -d --name app-blue -p 3001:3000 app:latest
./scripts/smoke-test.sh app-blue
docker stop app-green
docker rename app-blue app-green
```

### Phase 4: Post-Deployment

```bash
# Verify production
curl -f https://production.com/health
npm run e2e:production

# Monitor metrics
./scripts/check-metrics.sh
./scripts/check-errors.sh

# Notification
./scripts/notify-team.sh "Deployment successful"
```

## Automatic Rollback

If any check fails, automatic rollback:

```bash
#!/bin/bash
# rollback.sh

echo "⚠️ Deployment failed, rolling back..."

# Restore previous version
docker stop app-green
docker run -d --name app-green -p 3000:3000 app:backup

# Verify rollback
if curl -f http://localhost:3000/health; then
  echo "✅ Rollback successful"
else
  echo "🚨 CRITICAL: Rollback failed!"
  ./scripts/alert-oncall.sh
fi

# Investigation
docker logs app-green > failure-logs.txt
npm run analyze:failure
```

## Real-Time Monitoring

```javascript
// monitoring.js
const checks = {
  health: "https://api.example.com/health",
  database: "https://api.example.com/db/status",
  cache: "https://api.example.com/cache/status",
  queue: "https://api.example.com/queue/status",
};

async function monitorDeployment() {
  console.log("🔍 Monitoring deployment...");

  for (const [service, url] of Object.entries(checks)) {
    try {
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`${service} unhealthy`);
      }
      console.log(`✅ ${service}: OK`);
    } catch (error) {
      console.error(`❌ ${service}: FAILED`);
      await rollback();
      break;
    }
  }
}

// Run every 30 seconds for first 5 minutes
const interval = setInterval(monitorDeployment, 30000);
setTimeout(() => clearInterval(interval), 300000);
```

## Deployment Checklist

```markdown
## Pre-Deployment

- [ ] All tests passing
- [ ] Security scan clean
- [ ] Performance benchmarks met
- [ ] Documentation updated
- [ ] Changelog written
- [ ] Team notified

## During Deployment

- [ ] Backup created
- [ ] Health checks passing
- [ ] Monitoring active
- [ ] Logs streaming
- [ ] Rollback ready

## Post-Deployment

- [ ] Production verified
- [ ] Metrics normal
- [ ] No error spikes
- [ ] Users can access
- [ ] Features working
- [ ] Performance stable

## Sign-offs

- [ ] Developer tested
- [ ] QA approved
- [ ] Security reviewed
- [ ] PM validated
- [ ] DevOps ready
```

## Success Metrics

### Immediate (< 5 min)

- Zero 500 errors
- Response time < 200ms
- CPU usage < 70%
- Memory stable
- No restart loops

### Short-term (< 1 hour)

- Error rate < 0.1%
- User complaints: 0
- All features accessible
- Database queries fast
- Cache hit rate > 90%

### Long-term (< 24 hours)

- No memory leaks
- No performance degradation
- Positive user feedback
- Business metrics stable
- No security incidents

## Alert Thresholds

```yaml
alerts:
  error_rate:
    threshold: 1%
    duration: 5m
    action: page_oncall

  response_time:
    threshold: 500ms
    duration: 10m
    action: slack_notification

  memory_usage:
    threshold: 90%
    duration: 5m
    action: auto_scale

  failed_requests:
    threshold: 100
    duration: 1m
    action: auto_rollback
```

## Output Example

```
🚀 Deployment Validation Started

Pre-Deployment Checks:
✅ Code quality: Passed
✅ Tests: 195/195 passing
✅ Security: No vulnerabilities
✅ Build: Successful
✅ Docker: Image ready

Deploying to production...
→ Blue environment: Starting
→ Health check: Passed
→ Switching traffic: Complete
→ Green environment: Stopped

Post-Deployment Validation:
✅ Production health: OK
✅ Database connection: OK
✅ API responses: OK
✅ Error rate: 0%
✅ Response time: 142ms avg

🎉 Deployment Successful!
Version: v2.1.0
Duration: 3m 42s
Rollback available: Yes
```
