A community based topic aggregation platform built on atproto
at main 3.7 kB view raw
1#!/bin/bash 2# Coves Deployment Script 3# Usage: ./scripts/deploy.sh [service] 4# 5# Examples: 6# ./scripts/deploy.sh # Deploy all services 7# ./scripts/deploy.sh appview # Deploy only AppView 8# ./scripts/deploy.sh --pull # Pull from git first, then deploy 9 10set -e 11 12SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" 13PROJECT_DIR="$(dirname "$SCRIPT_DIR")" 14COMPOSE_FILE="$PROJECT_DIR/docker-compose.prod.yml" 15 16# Colors for output 17RED='\033[0;31m' 18GREEN='\033[0;32m' 19YELLOW='\033[1;33m' 20NC='\033[0m' # No Color 21 22log() { 23 echo -e "${GREEN}[DEPLOY]${NC} $1" 24} 25 26warn() { 27 echo -e "${YELLOW}[WARN]${NC} $1" 28} 29 30error() { 31 echo -e "${RED}[ERROR]${NC} $1" 32 exit 1 33} 34 35# Parse arguments 36PULL_GIT=false 37SERVICE="" 38 39for arg in "$@"; do 40 case $arg in 41 --pull) 42 PULL_GIT=true 43 ;; 44 *) 45 SERVICE="$arg" 46 ;; 47 esac 48done 49 50cd "$PROJECT_DIR" 51 52# Load environment variables 53if [ ! -f ".env.prod" ]; then 54 error ".env.prod not found! Copy from .env.prod.example and configure secrets." 55fi 56 57log "Loading environment from .env.prod..." 58set -a 59source .env.prod 60set +a 61 62# Optional: Pull from git 63if [ "$PULL_GIT" = true ]; then 64 log "Pulling latest code from git..." 65 git fetch origin 66 git pull origin main 67fi 68 69# Check database connectivity before deployment 70log "Checking database connectivity..." 71if docker compose -f "$COMPOSE_FILE" exec -T postgres pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; then 72 log "Database is ready" 73else 74 warn "Database not ready yet - it will start with the deployment" 75fi 76 77# Build and deploy 78if [ -n "$SERVICE" ]; then 79 log "Building $SERVICE..." 80 docker compose -f "$COMPOSE_FILE" build --no-cache "$SERVICE" 81 82 log "Deploying $SERVICE..." 83 docker compose -f "$COMPOSE_FILE" up -d "$SERVICE" 84else 85 log "Building all services..." 86 docker compose -f "$COMPOSE_FILE" build --no-cache 87 88 log "Deploying all services..." 89 docker compose -f "$COMPOSE_FILE" up -d 90fi 91 92# Health check 93log "Waiting for services to be healthy..." 94sleep 10 95 96# Wait for database to be ready before running migrations 97log "Waiting for database..." 98for i in {1..30}; do 99 if docker compose -f "$COMPOSE_FILE" exec -T postgres pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; then 100 break 101 fi 102 sleep 1 103done 104 105# Run database migrations 106# The AppView runs migrations on startup, but we can also trigger them explicitly 107log "Running database migrations..." 108if docker compose -f "$COMPOSE_FILE" exec -T appview /app/coves-server migrate 2>/dev/null; then 109 log "✅ Migrations completed" 110else 111 warn "⚠️ Migration command not available or failed - AppView will run migrations on startup" 112fi 113 114# Check AppView health 115if docker compose -f "$COMPOSE_FILE" exec -T appview wget --spider -q http://localhost:8080/xrpc/_health 2>/dev/null; then 116 log "✅ AppView is healthy" 117else 118 warn "⚠️ AppView health check failed - check logs with: docker compose -f docker-compose.prod.yml logs appview" 119fi 120 121# Check PDS health 122if docker compose -f "$COMPOSE_FILE" exec -T pds wget --spider -q http://localhost:3000/xrpc/_health 2>/dev/null; then 123 log "✅ PDS is healthy" 124else 125 warn "⚠️ PDS health check failed - check logs with: docker compose -f docker-compose.prod.yml logs pds" 126fi 127 128log "Deployment complete!" 129log "" 130log "Useful commands:" 131log " View logs: docker compose -f docker-compose.prod.yml logs -f" 132log " Check status: docker compose -f docker-compose.prod.yml ps" 133log " Rollback: docker compose -f docker-compose.prod.yml down && git checkout HEAD~1 && ./scripts/deploy.sh"