refactor(deploy): Use git pull instead of rsync for deployments

- Requires changes to be committed and pushed before deploying
- Clones repo on first deploy, then uses git fetch/reset
- Preserves .envs secrets which are not in git
- Better for multi-developer workflows

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
poduck
2025-12-03 15:46:01 -05:00
parent 04bb9e3c14
commit fd751f02f8

122
deploy.sh
View File

@@ -2,6 +2,9 @@
# SmoothSchedule Production Deployment Script # SmoothSchedule Production Deployment Script
# Usage: ./deploy.sh [server_user@server_host] # Usage: ./deploy.sh [server_user@server_host]
# Example: ./deploy.sh poduck@smoothschedule.com # Example: ./deploy.sh poduck@smoothschedule.com
#
# This script deploys from git repository, not local files.
# Changes must be committed and pushed before deploying.
set -e set -e
@@ -12,7 +15,8 @@ YELLOW='\033[1;33m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
SERVER=${1:-"poduck@smoothschedule.com"} SERVER=${1:-"poduck@smoothschedule.com"}
PROJECT_DIR="/home/poduck/Desktop/smoothschedule2" REPO_URL="https://git.talova.net/poduck/smoothschedule.git"
REMOTE_DIR="/home/poduck/smoothschedule"
echo -e "${GREEN}===================================" echo -e "${GREEN}==================================="
echo "SmoothSchedule Deployment" echo "SmoothSchedule Deployment"
@@ -33,57 +37,80 @@ print_error() {
echo -e "${RED}>>> $1${NC}" echo -e "${RED}>>> $1${NC}"
} }
# Step 1: Build frontend # Step 1: Check for uncommitted changes
print_status "Step 1: Skipping local build (building on server)..." print_status "Step 1: Checking for uncommitted changes..."
# cd "$PROJECT_DIR/frontend" if [[ -n $(git status --porcelain) ]]; then
# if [ ! -d "node_modules" ]; then print_error "You have uncommitted changes. Please commit and push before deploying."
# print_warning "Installing frontend dependencies..." git status --short
# npm install exit 1
# fi fi
# npm run build
# print_status "Frontend build complete!"
# Step 2: Prepare deployment package # Check if local is ahead of remote
print_status "Step 2: Preparing deployment package..." LOCAL_COMMIT=$(git rev-parse HEAD)
cd "$PROJECT_DIR" REMOTE_COMMIT=$(git rev-parse @{u} 2>/dev/null || echo "")
mkdir -p /tmp/smoothschedule-deploy
# Copy backend if [[ -z "$REMOTE_COMMIT" ]]; then
rsync -av --exclude='.venv' --exclude='__pycache__' --exclude='*.pyc' \ print_error "No upstream branch configured. Please push your changes first."
--exclude='.git' --exclude='node_modules' \ exit 1
"$PROJECT_DIR/smoothschedule/" /tmp/smoothschedule-deploy/backend/ fi
# Copy frontend source if [[ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ]]; then
rsync -av --exclude='node_modules' --exclude='dist' --exclude='.git' \ print_warning "Local branch differs from remote. Checking if ahead..."
"$PROJECT_DIR/frontend/" /tmp/smoothschedule-deploy/frontend/ AHEAD=$(git rev-list --count @{u}..HEAD)
if [[ "$AHEAD" -gt 0 ]]; then
print_error "You have $AHEAD unpushed commit(s). Please push before deploying."
exit 1
fi
fi
print_status "Deployment package prepared!" print_status "All changes committed and pushed!"
# Step 3: Upload to server # Step 2: Deploy on server
print_status "Step 3: Uploading to server..." print_status "Step 2: Deploying on server..."
ssh "$SERVER" "mkdir -p ~/smoothschedule"
# Upload backend ssh "$SERVER" "bash -s" << ENDSSH
rsync -avz --delete /tmp/smoothschedule-deploy/backend/ "$SERVER:~/smoothschedule/"
# Upload nginx config
scp "$PROJECT_DIR/frontend/nginx.conf" "$SERVER:~/smoothschedule/nginx.conf"
# Upload frontend
rsync -avz --delete /tmp/smoothschedule-deploy/frontend/ "$SERVER:~/smoothschedule-frontend/"
print_status "Files uploaded!"
# Step 4: Deploy on server
print_status "Step 4: Deploying on server..."
ssh "$SERVER" 'bash -s' << 'ENDSSH'
set -e set -e
echo ">>> Navigating to project directory..." echo ">>> Setting up project directory..."
cd ~/smoothschedule
# Backup .envs if they exist (secrets not in git)
if [ -d "$REMOTE_DIR/smoothschedule/.envs" ]; then
echo ">>> Backing up .envs secrets..."
cp -r "$REMOTE_DIR/smoothschedule/.envs" /tmp/.envs-backup
elif [ -d "$REMOTE_DIR/.envs" ]; then
# Old structure - .envs was at root level
echo ">>> Backing up .envs secrets (old location)..."
cp -r "$REMOTE_DIR/.envs" /tmp/.envs-backup
fi
if [ ! -d "$REMOTE_DIR/.git" ]; then
echo ">>> Cloning repository for the first time..."
# Remove old non-git deployment if exists
if [ -d "$REMOTE_DIR" ]; then
rm -rf "$REMOTE_DIR"
fi
git clone "$REPO_URL" "$REMOTE_DIR"
else
echo ">>> Repository exists, pulling latest changes..."
cd "$REMOTE_DIR"
git fetch origin
git reset --hard origin/main
fi
cd "$REMOTE_DIR"
# Restore .envs secrets
if [ -d /tmp/.envs-backup ]; then
echo ">>> Restoring .envs secrets..."
cp -r /tmp/.envs-backup "$REMOTE_DIR/smoothschedule/.envs"
rm -rf /tmp/.envs-backup
fi
echo ">>> Current commit:"
git log -1 --oneline
echo ">>> Building Docker images..." echo ">>> Building Docker images..."
cd smoothschedule
docker compose -f docker-compose.production.yml build docker compose -f docker-compose.production.yml build
echo ">>> Starting containers..." echo ">>> Starting containers..."
@@ -93,10 +120,10 @@ echo ">>> Waiting for containers to start..."
sleep 10 sleep 10
echo ">>> Running database migrations..." echo ">>> Running database migrations..."
docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} && python manage.py migrate' docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://\${POSTGRES_USER}:\${POSTGRES_PASSWORD}@\${POSTGRES_HOST}:\${POSTGRES_PORT}/\${POSTGRES_DB} && python manage.py migrate'
echo ">>> Collecting static files..." echo ">>> Collecting static files..."
docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} && python manage.py collectstatic --noinput' docker compose -f docker-compose.production.yml exec -T django sh -c 'export DATABASE_URL=postgres://\${POSTGRES_USER}:\${POSTGRES_PASSWORD}@\${POSTGRES_HOST}:\${POSTGRES_PORT}/\${POSTGRES_DB} && python manage.py collectstatic --noinput'
echo ">>> Seeding/updating platform plugins for all tenants..." echo ">>> Seeding/updating platform plugins for all tenants..."
docker compose -f docker-compose.production.yml exec -T django python -c " docker compose -f docker-compose.production.yml exec -T django python -c "
@@ -115,9 +142,6 @@ docker compose -f docker-compose.production.yml ps
echo ">>> Deployment complete!" echo ">>> Deployment complete!"
ENDSSH ENDSSH
# Cleanup
rm -rf /tmp/smoothschedule-deploy
echo "" echo ""
print_status "===================================" print_status "==================================="
print_status "Deployment Complete!" print_status "Deployment Complete!"
@@ -129,7 +153,7 @@ echo " - https://platform.smoothschedule.com"
echo " - https://*.smoothschedule.com (tenant subdomains)" echo " - https://*.smoothschedule.com (tenant subdomains)"
echo "" echo ""
echo "To view logs:" echo "To view logs:"
echo " ssh $SERVER 'cd ~/smoothschedule && docker compose -f docker-compose.production.yml logs -f'" echo " ssh $SERVER 'cd ~/smoothschedule/smoothschedule && docker compose -f docker-compose.production.yml logs -f'"
echo "" echo ""
echo "To check status:" echo "To check status:"
echo " ssh $SERVER 'cd ~/smoothschedule && docker compose -f docker-compose.production.yml ps'" echo " ssh $SERVER 'cd ~/smoothschedule/smoothschedule && docker compose -f docker-compose.production.yml ps'"