Files
fullstack-portfolio/deploy.sh

52 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
echo "Starting Production Deployment..."
# Load environment variables from .env if it exists
if [ -f .env ]; then
echo "Loading environment variables from .env..."
export $(grep -v '^#' .env | xargs)
fi
# Check if 'docker compose' (v2) or 'docker-compose' (v1) is available
if docker compose version >/dev/null 2>&1; then
COMPOSE_CMD="docker compose"
elif docker-compose version >/dev/null 2>&1; then
COMPOSE_CMD="docker-compose"
else
echo "Error: Docker Compose not found. Please install Docker Compose first."
exit 1
fi
# Configuration
DB_CONTAINER_NAME="fikri-portfolio-db"
DB_PORT="5429"
# Check if any container is using the DB port or if our specific container is running
EXISTING_DB=$(docker ps -q -f publish=${DB_PORT})
if [ ! -z "$EXISTING_DB" ]; then
echo "Database is already running on port ${DB_PORT}. Container ID: $EXISTING_DB"
echo "Building and updating only the application..."
# --build ensures we use the latest code
# --no-deps ensures we don't restart the DB if it's healthy
$COMPOSE_CMD up -d --build --no-deps app
else
echo "Database not found on port ${DB_PORT}."
echo "Full deployment (Database + App)..."
$COMPOSE_CMD up -d --build
fi
# Health check - optional but recommended
echo "Waiting for application to be healthy..."
# You could add a curl check here if desired
# Cleanup: remove dangling images to save disk space
echo "Cleaning up old images..."
docker image prune -f
echo "Deployment script finished successfully!"