Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 360 additions & 0 deletions deployment/DEPLOYMENT_CHECKLIST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
# ✅ TnSolution Deployment Checklist

Quick reference checklist for deploying TnSolution platform.

---

## 🔍 Pre-Deployment Validation

### 1. Environment Check
- [ ] Node.js version 18.x installed
- [ ] PM2 installed globally
- [ ] PostgreSQL 15 running
- [ ] Nginx installed (if using reverse proxy)
- [ ] All environment files (.env) configured
- [ ] Database credentials set correctly

### 2. Script Validation
```bash
# Check all scripts are executable
ls -la deployment/*.sh

# Expected: -rwxrwxr-x for all .sh files

# Validate syntax
cd deployment
bash -n deploy.sh
bash -n check-deploy.sh
bash -n monitor-deployment.sh
bash -n setup-memory.sh
bash -n vps-setup.sh
```

### 3. Configuration Check
- [ ] `deploy-config.json` - Ports and memory settings correct
- [ ] `nginx-tnsolution.conf` - Domain names updated
- [ ] `webhook.service` - Webhook secret configured
- [ ] Environment variables in systemd service

---

## 🚀 Initial Deployment

### Step 1: VPS Setup (First time only)
```bash
# Run as root
sudo ./deployment/vps-setup.sh

# Verify installation
node --version # Should show v18.x
pm2 --version # Should show PM2 version
psql --version # Should show PostgreSQL 15
nginx -v # Should show Nginx version
```

### Step 2: Clone Repository
```bash
cd /root
git clone <repository-url> TnSolution
cd TnSolution
```

### Step 3: Configure Environment
```bash
# Copy environment templates
cp deployment/env.production.example Tn-api/.env
cp deployment/env.production.example Tn-admin/.env
cp deployment/env.production.example Tn-manager/.env

# Edit each .env file with correct values
nano Tn-api/.env
nano Tn-admin/.env
nano Tn-manager/.env
```

### Step 4: Check Prerequisites
```bash
./deployment/check-deploy.sh

# All checks should show [OK] or [WARNING] (warnings are acceptable)
```

### Step 5: Initial Build
```bash
# Build API
cd Tn-api
npm install
npm run build

# Build Admin
cd ../Tn-admin
npm install
npm run build:prod

# Build Manager
cd ../Tn-manager
npm install
npm run build:prod
```

### Step 6: Database Setup
```bash
cd /root/TnSolution/Tn-api
npm run typeorm migration:run
```

### Step 7: Setup Memory Configuration
```bash
cd /root/TnSolution
./deployment/setup-memory.sh
```

### Step 8: Verify Services
```bash
# Check PM2 status
pm2 list

# Should show:
# - tn-api (online)
# - tn-admin (online)
# - tn-manager (online)

# Test health endpoints
curl http://localhost:3000/api/health
curl http://localhost:4000/health
curl http://localhost:4200/health
```

---

## 🔄 Regular Deployment

### Pre-Deployment
```bash
# 1. Check current system status
./deployment/check-deploy.sh

# 2. Check current PM2 status
pm2 list

# 3. Create backup (optional but recommended)
pm2 save
cp -r /root/TnSolution /root/TnSolution.backup.$(date +%Y%m%d_%H%M%S)
```

### Deployment
```bash
# Run deployment script
./deployment/deploy.sh

# This script will:
# - Pull latest changes from develop branch
# - Install dependencies for all services
# - Build all services
# - Run database migrations
# - Restart PM2 services
# - Send notification (if configured)
```

### Post-Deployment
```bash
# 1. Monitor deployment
./deployment/monitor-deployment.sh production

# 2. Check logs
pm2 logs --lines 50

# 3. Verify services
curl http://localhost:3000/api/health
curl http://localhost:4000/health
curl http://localhost:4200/health

# 4. Check for errors
pm2 logs --err
```

---

## 🔧 Automated Deployment (Webhook)

### Setup
```bash
# 1. Copy systemd service
sudo cp deployment/webhook.service /etc/systemd/system/

# 2. Update webhook secret in service file
sudo nano /etc/systemd/system/webhook.service
# Set WEBHOOK_SECRET to match GitHub webhook secret

# 3. Reload systemd
sudo systemctl daemon-reload

# 4. Enable and start service
sudo systemctl enable webhook.service
sudo systemctl start webhook.service

# 5. Check status
sudo systemctl status webhook.service
```

### GitHub Configuration
1. Go to repository Settings → Webhooks
2. Add webhook:
- Payload URL: `http://your-server-ip:9000/webhook`
- Content type: `application/json`
- Secret: (same as WEBHOOK_SECRET in webhook.service)
- Events: Select "Just the push event"
3. Test webhook by pushing to develop branch

---

## 🚨 Troubleshooting

### Service Won't Start
```bash
# Check logs
pm2 logs <service-name> --err

# Check if port is already in use
netstat -tulpn | grep <port>

# Restart specific service
pm2 restart <service-name>
```

### Build Failures
```bash
# Check Node.js version
node --version

# Clear node_modules and rebuild
rm -rf node_modules package-lock.json
npm install
npm run build
```

### Database Connection Issues
```bash
# Check PostgreSQL status
sudo systemctl status postgresql

# Test database connection
psql -U tnsolution_user -d tnsolution_db -h localhost

# Check .env file has correct credentials
cat Tn-api/.env | grep DB_
```

### Memory Issues
```bash
# Check current memory usage
free -h

# Check Node.js memory settings
echo $NODE_OPTIONS

# Restart with memory configuration
./deployment/setup-memory.sh
```

---

## 📊 Health Monitoring

### Daily Checks
```bash
# Quick status check
pm2 list

# Check logs for errors
pm2 logs --err --lines 20

# Monitor system resources
./deployment/monitor-deployment.sh production
```

### Weekly Maintenance
```bash
# Update system packages
sudo apt update && sudo apt upgrade -y

# Clear old logs
pm2 flush

# Check disk space
df -h

# Review memory usage
./deployment/monitor-memory.sh
```

---

## 🔄 Rollback Procedure

### If deployment fails:
```bash
# 1. Stop current services
pm2 stop all

# 2. Restore from backup
cd /root
mv TnSolution TnSolution.failed
mv TnSolution.backup.<timestamp> TnSolution

# 3. Restart services
cd TnSolution
pm2 restart all

# 4. Verify services
pm2 list
./deployment/monitor-deployment.sh production
```

---

## 📝 Quick Command Reference

```bash
# Deployment
./deployment/check-deploy.sh # Check prerequisites
./deployment/deploy.sh # Deploy updates
./deployment/monitor-deployment.sh # Monitor status

# PM2 Management
pm2 list # List all services
pm2 restart all # Restart all services
pm2 logs # View logs
pm2 logs --err # View error logs only
pm2 monit # Real-time monitoring
pm2 save # Save current PM2 config

# System Status
free -h # Memory usage
df -h # Disk usage
netstat -tulpn # Check ports
systemctl status <service> # Check service status

# Git Operations
git status # Check current status
git pull origin develop # Pull latest changes
git log --oneline -10 # View recent commits
```

---

## ✅ Success Indicators

After deployment, verify:
- [ ] All PM2 services show "online" status
- [ ] All health check endpoints return 200 OK
- [ ] No errors in PM2 logs
- [ ] Memory usage is normal
- [ ] Disk space is sufficient
- [ ] All URLs are accessible
- [ ] Database migrations applied successfully

---

**Last Updated**: October 10, 2025
**Maintainer**: TnSolution DevOps Team
Loading