# Production Deployment Guide

## Overview

This project uses a **Blue-Green deployment** strategy with **automatic rollback** capabilities to ensure zero-downtime deployments and quick recovery from failures.

## Architecture

### Blue-Green Deployment
- **Blue Service**: Currently serving production traffic
- **Green Service**: New deployment being tested
- **Traffic Switch**: Instant switchover via Traefik labels
- **Automatic Rollback**: Health check failures trigger immediate rollback

### Key Features
- ✅ **Zero-downtime deployments**
- ✅ **Automatic health checks**
- ✅ **Instant rollback on failure**
- ✅ **Backup system with retention**
- ✅ **Build verification before traffic switch**
- ✅ **Resource limits and security**

## Deployment Process

### Automatic Deployment (Recommended)

1. **Create Release Tag**:
   ```bash
   git tag 1.0.1-release
   git push origin 1.0.1-release
   ```

2. **Monitor Progress**:
   - GitHub Actions: `https://github.com/sotoontech/Vitron-Front/actions`
   - Server logs: `ssh root@prod-server 'docker-compose -f /opt/front-prod/Vitron-Front/docker-compose.yml logs -f'`

### Deployment Steps (Automatic)

1. **Backup**: Current deployment tagged as backup
2. **Build**: New version built in parallel (green/blue)
3. **Health Check**: 10 attempts over 100 seconds
4. **Traffic Switch**: Instant switchover via Traefik
5. **Verification**: Final check on live domain
6. **Cleanup**: Old deployment stopped and cleaned

## Manual Operations

### Emergency Rollback

```bash
# SSH to production server
ssh root@prod-server

# Go to project directory
cd /opt/front-prod/Vitron-Front

# Rollback to latest backup
./scripts/rollback.sh

# Or rollback to specific backup
./scripts/rollback.sh vitron-frontend-backup:20240906_150230
```

### Check Status

```bash
# Check running containers
docker ps | grep vitron

# Check available backups
docker images | grep vitron-frontend-backup

# Check service health
docker exec vitron-frontend-blue wget --spider http://localhost:3000/
curl -I https://vitrown.com/

# Check logs
docker-compose logs -f vitron-frontend-blue
docker-compose logs -f vitron-frontend-green
```

### Manual Deployment

```bash
# SSH to server
ssh root@prod-server
cd /opt/front-prod/Vitron-Front

# Pull latest changes
git fetch --all
git checkout main
git pull origin main

# Deploy manually
docker-compose -f docker-compose.yml up -d --build
```

## Environment Configuration

### Required GitHub Secrets
- `PROD_SSH_HOST`: Production server IP/hostname
- `PROD_SSH_KEY`: SSH private key for server access
- `PROD_REMIX_SECRET`: Strong random string for session encryption

### Environment Variables
```bash
NODE_ENV=production
VITE_API_BASE_URL=https://api.prod.vitrown.com
VITE_API_SOCKET_BASE_URL=wss://api.prod.vitrown.com
VITE_API_URL=https://api.prod.vitrown.com
VITE_SITE_URL=https://vitrown.com
VITE_DOMAIN_URL=https://vitrown.com
VITE_APP_VERSION_TYPE=production
REMIX_SECRET=[FROM_GITHUB_SECRETS]
```

## Troubleshooting

### Common Issues

1. **Build Failed**
   ```bash
   # Check build logs
   docker-compose logs vitron-frontend-[color]
   
   # Manual build test
   docker-compose build vitron-frontend-blue
   ```

2. **Health Check Failed**
   ```bash
   # Check container status
   docker exec vitron-frontend-[color] ps aux
   
   # Check internal health
   docker exec vitron-frontend-[color] wget --spider http://localhost:3000/
   
   # Check logs
   docker exec vitron-frontend-[color] tail -f /var/log/nginx/error.log
   ```

3. **Rollback Issues**
   ```bash
   # List available backups
   docker images | grep backup
   
   # Manual container start
   docker run -d --name manual-rollback vitron-frontend-backup:20240906_150230
   ```

### Health Check Endpoints
- Internal: `http://localhost:3000/` (container health)
- External: `https://vitrown.com/` (public endpoint)
- API: `https://api.prod.vitrown.com/api/health/` (backend health)

## Security Features

- **Non-root user**: Containers run as `remix` user (UID 1001)
- **Security headers**: X-Frame-Options, X-Content-Type-Options, etc.
- **HTTPS enforcement**: Automatic redirect from HTTP
- **Resource limits**: Memory limits prevent resource exhaustion
- **Network isolation**: Services run in isolated Docker network

## Monitoring

### Key Metrics to Monitor
- Container health status
- Response time on health checks  
- Memory usage (limit: 512MB)
- Disk space (Docker images)
- SSL certificate expiry

### Log Locations
- Application logs: `docker-compose logs`
- Traefik logs: Check proxy network logs
- System logs: `/var/log/syslog` on server

## Backup Strategy

- **Automatic**: Every deployment creates timestamped backup
- **Retention**: Last 3 backups kept automatically
- **Manual backup**: `docker tag vitron-frontend-blue vitron-frontend-backup:manual-$(date +%Y%m%d_%H%M%S)`

## Performance Optimizations

- **Multi-stage build**: Smaller production images
- **Asset optimization**: Terser minification, tree shaking
- **Caching**: Docker layer caching, npm ci
- **Resource limits**: Prevents memory leaks
- **Health checks**: Quick failure detection