Files
cv-site/.github/workflows/deploy.yml
T

131 lines
4.8 KiB
YAML

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!"