Some checks failed
Deploy to Production / Deploy TurboTrades (push) Has been cancelled
153 lines
4.9 KiB
YAML
153 lines
4.9 KiB
YAML
name: Deploy to Production
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main # triggers automatically on pushes to main
|
|
|
|
jobs:
|
|
deploy:
|
|
name: Deploy TurboTrades
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: "20"
|
|
cache: "npm"
|
|
|
|
- name: Install Backend Dependencies
|
|
run: npm ci --production
|
|
|
|
- name: Install Frontend Dependencies
|
|
run: |
|
|
cd frontend
|
|
npm ci
|
|
|
|
- name: Build Frontend
|
|
run: |
|
|
cd frontend
|
|
npm run build
|
|
env:
|
|
NODE_ENV: production
|
|
VITE_API_URL: https://api.turbotrades.dev
|
|
VITE_WS_URL: https://ws.turbotrades.dev
|
|
|
|
- name: Deploy via SSH (Gitea + password)
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
password: ${{ secrets.SERVER_PASSWORD }}
|
|
port: ${{ secrets.SERVER_PORT || 22 }}
|
|
script: |
|
|
echo "🚀 Starting deployment to turbotrades.dev..."
|
|
|
|
# Ensure directories exist
|
|
mkdir -p /root/ttbackend /root/ttbackend/logs /var/www/html/turbotrades
|
|
|
|
cd /root/ttbackend
|
|
|
|
# Stop backend if running
|
|
echo "⏸️ Stopping backend..."
|
|
pm2 stop turbotrades-backend || echo "Backend not running"
|
|
pm2 delete turbotrades-backend || true
|
|
|
|
# Backup current version
|
|
echo "💾 Creating backup..."
|
|
if [ -d "/root/ttbackend-backup" ]; then
|
|
rm -rf /root/ttbackend-backup-old
|
|
mv /root/ttbackend-backup /root/ttbackend-backup-old
|
|
fi
|
|
mkdir -p /root/ttbackend-backup
|
|
rsync -a --delete /root/ttbackend/ /root/ttbackend-backup/ || true
|
|
|
|
# Pull latest code via HTTPS (password auth)
|
|
if [ -d ".git" ]; then
|
|
echo "📥 Pulling latest code..."
|
|
git fetch origin
|
|
git reset --hard origin/main
|
|
git clean -fd
|
|
else
|
|
echo "📥 Cloning repository..."
|
|
cd /root
|
|
rm -rf ttbackend
|
|
git clone https://username:${{ secrets.SERVER_PASSWORD }}@git.turbotrades.dev/iDefineHD/TurboTrades.git ttbackend
|
|
cd ttbackend
|
|
fi
|
|
|
|
# Install backend dependencies
|
|
echo "📦 Installing backend dependencies..."
|
|
npm ci --production
|
|
|
|
# Build frontend
|
|
echo "🎨 Building frontend..."
|
|
cd frontend
|
|
npm ci
|
|
npm run build
|
|
|
|
# Deploy frontend
|
|
echo "🚀 Deploying frontend..."
|
|
rm -rf /var/www/html/turbotrades/*
|
|
cp -r dist/* /var/www/html/turbotrades/
|
|
chown -R www-data:www-data /var/www/html/turbotrades
|
|
chmod -R 755 /var/www/html/turbotrades
|
|
|
|
# Start backend
|
|
cd /root/ttbackend
|
|
echo "▶️ Starting backend..."
|
|
pm2 start ecosystem.config.js --env production
|
|
pm2 save
|
|
|
|
echo "✅ Deployment complete!"
|
|
pm2 list
|
|
|
|
- name: Health Check
|
|
run: |
|
|
echo "⏳ Waiting for services to start..."
|
|
sleep 15
|
|
echo "🏥 Checking backend..."
|
|
curl -f https://api.turbotrades.dev/api/health || echo "⚠️ Backend health check failed"
|
|
echo "🏥 Checking frontend..."
|
|
curl -f https://turbotrades.dev || echo "⚠️ Frontend check failed"
|
|
|
|
- name: Notify Success
|
|
if: success()
|
|
run: |
|
|
echo "✅ Deployment successful!"
|
|
echo "🌐 Frontend: https://turbotrades.dev"
|
|
echo "🔧 Backend: https://api.turbotrades.dev"
|
|
echo "💬 WebSocket: https://ws.turbotrades.dev"
|
|
echo "📅 Time: $(date)"
|
|
|
|
- name: Notify Failure
|
|
if: failure()
|
|
run: |
|
|
echo "❌ Deployment failed! Check logs above."
|
|
|
|
- name: Rollback on Failure
|
|
if: failure()
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
password: ${{ secrets.SERVER_PASSWORD }}
|
|
port: ${{ secrets.SERVER_PORT || 22 }}
|
|
script: |
|
|
echo "🔄 Rolling back to previous version..."
|
|
if [ -d "/root/ttbackend-backup" ]; then
|
|
echo "📦 Restoring backend from backup..."
|
|
rsync -a --delete /root/ttbackend-backup/ /root/ttbackend/
|
|
cd /root/ttbackend
|
|
echo "▶️ Restarting backend..."
|
|
pm2 start ecosystem.config.js --env production
|
|
pm2 save
|
|
echo "✅ Rollback complete"
|
|
else
|
|
echo "❌ No backup found!"
|
|
fi
|