name: Deploy CV Server on: push: branches: - main workflow_dispatch: # Allow manual deployment from GitHub UI jobs: deploy: name: Pull and Restart runs-on: ubuntu-latest steps: - name: Deploy to server uses: appleboy/ssh-action@v1.0.3 env: SERVICE_NAME: ${{ secrets.SERVICE_NAME || 'cv' }} REPO_PATH: ${{ secrets.REPO_PATH || '/home/txeo/Git/yo/cv' }} with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} port: ${{ secrets.SSH_PORT || '22' }} envs: SERVICE_NAME,REPO_PATH script: | set -e echo "๐Ÿš€ Deploying to server..." echo "๐Ÿ“ฅ Pulling latest changes..." cd $REPO_PATH # Fix repository permissions (entire repo, not just .git) echo "๐Ÿ”ง Fixing repository permissions..." sudo chown -R $USER:$USER . # Stash any local changes before pulling if ! git diff-files --quiet || ! git diff-index --quiet --cached HEAD; then echo "๐Ÿ’พ Stashing local changes..." git stash push -m "Auto-stash before deployment $(date +%Y%m%d-%H%M%S)" fi # Clean untracked files to avoid merge conflicts echo "๐Ÿงน Cleaning untracked files..." git clean -fd git pull origin main # Build CSS bundle for production echo "๐ŸŽจ Building CSS bundle..." if command -v lightningcss &> /dev/null; then mkdir -p static/dist lightningcss --bundle --minify static/css/main.css -o static/dist/bundle.min.css echo "โœ… CSS bundle created ($(du -h static/dist/bundle.min.css | cut -f1))" elif command -v npx &> /dev/null; then # Fallback to npx if lightningcss not globally installed echo "๐Ÿ“ฆ Using npx to run lightningcss..." mkdir -p static/dist npx lightningcss-cli --bundle --minify static/css/main.css -o static/dist/bundle.min.css echo "โœ… CSS bundle created via npx" else echo "โš ๏ธ lightningcss not found, falling back to modular CSS" # Ensure dist directory doesn't exist so template falls back to main.css rm -rf static/dist fi # Reapply stashed changes if any (optional - comment out if not needed) # if git stash list | grep -q "Auto-stash"; then # echo "โ™ป๏ธ Reapplying stashed changes..." # git stash pop || echo "โš ๏ธ Could not reapply stash (conflicts?)" # fi # Update systemd service file if changed echo "๐Ÿ“‹ Updating systemd service..." sudo cp config/systemd/cv.service /etc/systemd/system/$SERVICE_NAME.service sudo systemctl daemon-reload echo "๐Ÿ”„ Stopping service..." sudo systemctl stop $SERVICE_NAME || true # Wait for port to be released (max 15s) echo "โณ Waiting for port 1999 to be free..." for i in $(seq 1 15); do if ! sudo fuser 1999/tcp >/dev/null 2>&1; then echo "โœ… Port 1999 free after ${i}s" break fi if [ $i -eq 15 ]; then echo "โš ๏ธ Force-killing port 1999..." sudo fuser -k 1999/tcp 2>/dev/null || true sleep 1 fi sleep 1 done echo "๐Ÿ”„ Starting service..." sudo systemctl start $SERVICE_NAME echo "โณ Waiting for health check..." sleep 3 # Check service status if sudo systemctl is-active --quiet $SERVICE_NAME; then echo "โœ… Service restarted successfully" sudo systemctl status $SERVICE_NAME --no-pager -l else echo "โŒ Service failed to start" sudo journalctl -u $SERVICE_NAME -n 50 --no-pager exit 1 fi - name: Verify deployment uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} port: ${{ secrets.SSH_PORT || '22' }} script: | echo "๐Ÿ” Verifying deployment..." echo "Testing health endpoint..." sleep 2 if curl -f http://localhost:1999/health > /dev/null 2>&1; then echo "โœ… Health check passed" curl http://localhost:1999/health else echo "โŒ Health check failed" exit 1 fi echo "โœ… Deployment verification complete!"