Files
smoothschedule/deploy.sh
poduck 47f1a4d7b4 fix(deploy): Backup and restore .ssh keys during git-based deployments
SSH keys for mail server management are not in git, need to preserve
them like .envs secrets.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 15:51:08 -05:00

177 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# SmoothSchedule Production Deployment Script
# Usage: ./deploy.sh [server_user@server_host]
# 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
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
SERVER=${1:-"poduck@smoothschedule.com"}
REPO_URL="https://git.talova.net/poduck/smoothschedule.git"
REMOTE_DIR="/home/poduck/smoothschedule"
echo -e "${GREEN}==================================="
echo "SmoothSchedule Deployment"
echo "===================================${NC}"
echo "Target server: $SERVER"
echo ""
# Function to print status
print_status() {
echo -e "${GREEN}>>> $1${NC}"
}
print_warning() {
echo -e "${YELLOW}>>> $1${NC}"
}
print_error() {
echo -e "${RED}>>> $1${NC}"
}
# Step 1: Check for uncommitted changes
print_status "Step 1: Checking for uncommitted changes..."
if [[ -n $(git status --porcelain) ]]; then
print_error "You have uncommitted changes. Please commit and push before deploying."
git status --short
exit 1
fi
# Check if local is ahead of remote
LOCAL_COMMIT=$(git rev-parse HEAD)
REMOTE_COMMIT=$(git rev-parse @{u} 2>/dev/null || echo "")
if [[ -z "$REMOTE_COMMIT" ]]; then
print_error "No upstream branch configured. Please push your changes first."
exit 1
fi
if [[ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ]]; then
print_warning "Local branch differs from remote. Checking if ahead..."
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 "All changes committed and pushed!"
# Step 2: Deploy on server
print_status "Step 2: Deploying on server..."
ssh "$SERVER" "bash -s" << ENDSSH
set -e
echo ">>> Setting up project directory..."
# 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
# Backup .ssh if it exists (SSH keys not in git)
if [ -d "$REMOTE_DIR/smoothschedule/.ssh" ]; then
echo ">>> Backing up .ssh keys..."
cp -r "$REMOTE_DIR/smoothschedule/.ssh" /tmp/.ssh-backup
elif [ -d "$REMOTE_DIR/.ssh" ]; then
# Old structure
echo ">>> Backing up .ssh keys (old location)..."
cp -r "$REMOTE_DIR/.ssh" /tmp/.ssh-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
# Restore .ssh keys
if [ -d /tmp/.ssh-backup ]; then
echo ">>> Restoring .ssh keys..."
cp -r /tmp/.ssh-backup "$REMOTE_DIR/smoothschedule/.ssh"
rm -rf /tmp/.ssh-backup
fi
echo ">>> Current commit:"
git log -1 --oneline
echo ">>> Building Docker images..."
cd smoothschedule
docker compose -f docker-compose.production.yml build
echo ">>> Starting containers..."
docker compose -f docker-compose.production.yml up -d
echo ">>> Waiting for containers to start..."
sleep 10
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'
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'
echo ">>> Seeding/updating platform plugins for all tenants..."
docker compose -f docker-compose.production.yml exec -T django python -c "
from django_tenants.utils import get_tenant_model
from django.core.management import call_command
Tenant = get_tenant_model()
for tenant in Tenant.objects.exclude(schema_name='public'):
print(f' Seeding plugins for {tenant.schema_name}...')
call_command('tenant_command', 'seed_platform_plugins', schema=tenant.schema_name, verbosity=0)
print(' Done!')
"
echo ">>> Checking container status..."
docker compose -f docker-compose.production.yml ps
echo ">>> Deployment complete!"
ENDSSH
echo ""
print_status "==================================="
print_status "Deployment Complete!"
print_status "==================================="
echo ""
echo "Your application should now be running at:"
echo " - https://smoothschedule.com"
echo " - https://platform.smoothschedule.com"
echo " - https://*.smoothschedule.com (tenant subdomains)"
echo ""
echo "To view logs:"
echo " ssh $SERVER 'cd ~/smoothschedule/smoothschedule && docker compose -f docker-compose.production.yml logs -f'"
echo ""
echo "To check status:"
echo " ssh $SERVER 'cd ~/smoothschedule/smoothschedule && docker compose -f docker-compose.production.yml ps'"