name: Deploy to Production Server on: push: branches: - main workflow_dispatch: # Allow manual trigger jobs: deploy: name: Deploy to 178.63.127.19 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 dependencies (Backend) run: | npm ci - name: Install dependencies (Frontend) run: | cd frontend npm ci - name: Build Frontend run: | cd frontend npm run build env: NODE_ENV: production VITE_API_URL: http://178.63.127.19:3000 - name: Run Tests (if any) run: | npm test || echo "No tests configured" continue-on-error: true - name: Deploy to Production Server uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} port: ${{ secrets.SERVER_PORT || 22 }} script: | echo "πŸš€ Starting deployment..." # Navigate to project directory cd ${{ secrets.DEPLOY_PATH || '/var/www/turbotrades' }} # Stop the application echo "⏸️ Stopping application..." pm2 stop turbotrades || echo "App not running" # Backup current version echo "πŸ’Ύ Creating backup..." cp -r . ../turbotrades-backup-$(date +%Y%m%d-%H%M%S) || true # Pull latest code echo "πŸ“₯ Pulling latest code..." git fetch origin git reset --hard origin/main # Install backend dependencies echo "πŸ“¦ Installing backend dependencies..." npm ci --production # Build and install frontend echo "🎨 Building frontend..." cd frontend npm ci npm run build cd .. # Run database migrations (if any) echo "πŸ—„οΈ Running migrations..." npm run migrate || echo "No migrations to run" # Start the application echo "▢️ Starting application..." pm2 start ecosystem.config.js || pm2 start index.js --name turbotrades pm2 save # Verify deployment echo "βœ… Deployment complete!" pm2 list - name: Health Check run: | sleep 10 curl -f http://178.63.127.19:3000/api/health || echo "⚠️ Health check failed" - name: Notify Success if: success() run: | echo "βœ… Deployment successful!" echo "🌐 Server: 178.63.127.19" echo "πŸ‘€ Repository: git.turbotrades.dev/iDefineHD/TurboTrades" 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 }} key: ${{ secrets.SSH_PRIVATE_KEY }} port: ${{ secrets.SERVER_PORT || 22 }} script: | echo "πŸ”„ Rolling back to previous version..." cd ${{ secrets.DEPLOY_PATH || '/var/www/turbotrades' }} # Find latest backup BACKUP=$(ls -t ../turbotrades-backup-* | head -1) if [ -n "$BACKUP" ]; then echo "Found backup: $BACKUP" rm -rf ./* cp -r $BACKUP/* . pm2 restart turbotrades echo "βœ… Rollback complete" else echo "❌ No backup found!" fi