name: Deploy to Production on: push: branches: - main workflow_dispatch: jobs: deploy: name: Deploy to turbotrades.dev 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 to Production 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..." # Create directories if they don't exist mkdir -p /root/ttbackend mkdir -p /root/ttbackend/logs mkdir -p /var/www/html/turbotrades # Navigate to backend directory cd /root/ttbackend # Stop the application echo "⏸️ Stopping backend..." pm2 stop turbotrades-backend || echo "Backend not running" # 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 cp -r /root/ttbackend/* /root/ttbackend-backup/ 2>/dev/null || true # Clone or pull repository 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://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 to nginx directory echo "πŸš€ Deploying frontend to /var/www/html/turbotrades..." rm -rf /var/www/html/turbotrades/* cp -r dist/* /var/www/html/turbotrades/ # Set proper permissions chown -R www-data:www-data /var/www/html/turbotrades chmod -R 755 /var/www/html/turbotrades # Back to backend directory cd /root/ttbackend # Start backend with PM2 echo "▢️ Starting backend..." pm2 start ecosystem.config.js --env production pm2 save # Verify deployment echo "βœ… Deployment complete!" echo "" echo "πŸ“Š PM2 Status:" pm2 list echo "" echo "🌐 Frontend: https://turbotrades.dev" echo "πŸ”§ Backend: https://api.turbotrades.dev" echo "πŸ’¬ WebSocket: https://ws.turbotrades.dev" - name: Health Check run: | echo "⏳ Waiting for services to start..." sleep 15 echo "πŸ₯ Checking backend health..." 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!" echo "Check the logs above for details" - 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..." cd /root rm -rf ttbackend cp -r ttbackend-backup ttbackend cd ttbackend echo "▢️ Restarting backend..." pm2 restart turbotrades-backend pm2 save echo "βœ… Rollback complete" else echo "❌ No backup found!" fi