diff --git a/ci/scripts/deploy-new-tags.sh b/ci/scripts/deploy-new-tags.sh index 9d321bb..a3ab999 100755 --- a/ci/scripts/deploy-new-tags.sh +++ b/ci/scripts/deploy-new-tags.sh @@ -1,33 +1,45 @@ #!/bin/bash set -e -# Get the directory where the script is located -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -DEPLOY_DIR="$(cd "$SCRIPT_DIR/../deploy" && pwd)" +# Setup paths relative to the script location +DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../deploy" && pwd)" +ENV_FILE="$DEPLOY_DIR/.env" +TEMPLATE_FILE="$DEPLOY_DIR/.env.template" echo "Starting deployment for com-ermnvldmr..." -# Export current tags if they exist in .env to ensure continuity for services NOT being updated -if [ -f "$DEPLOY_DIR/.env" ]; then - export $(grep -v '^#' "$DEPLOY_DIR/.env" | xargs) +# 1. Load existing .env values as defaults (do NOT overwrite environment variables) +if [ -f "$ENV_FILE" ]; then + while IFS= read -r line || [ -n "$line" ]; do + # Skip comments and empty lines + [[ "$line" =~ ^# ]] || [[ -z "$line" ]] && continue + + key="${line%%=*}" + val="${line#*=}" + + # Only export if the variable is not already set in the environment + if [ -z "${!key}" ]; then + export "$key"="$val" + fi + done < "$ENV_FILE" fi -# Override with new tags provided via env (passed from GitHub) -# The variables DOCS_TAG and WWW_TAG should already be in the environment -export DOCS_TAG=${DOCS_TAG:-latest} -export WWW_TAG=${WWW_TAG:-latest} +# 2. Apply final fallback defaults +export DOCS_TAG="${DOCS_TAG:-latest}" +export WWW_TAG="${WWW_TAG:-latest}" -# Generate new .env from template +# 3. Generate the .env file from template +# Explicitly list variables to substitute to avoid messing with other values echo "Generating .env from template..." -envsubst < "$DEPLOY_DIR/.env.template" > "$DEPLOY_DIR/.env" +envsubst '$DOCS_TAG $WWW_TAG' < "$TEMPLATE_FILE" > "$ENV_FILE" -# Run Docker Compose +# 4. Run Docker operations cd "$DEPLOY_DIR" echo "Pulling images..." docker compose pull echo "Starting containers..." -docker compose up --force-recreate --build -d --remove-orphans +docker compose up -d --remove-orphans echo "Pruning unused images..." docker image prune -f