#!/usr/bin/env bash set -euo pipefail # --- Wczytaj zmienne z .env --- if [[ -f .env ]]; then set -a source .env set +a fi APP_PORT="${APP_PORT:-8080}" REPO_DIR="${REPO_DIR:-$(pwd)}" COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}" GIT_REMOTE="${GIT_REMOTE:-origin}" GIT_BRANCH="${GIT_BRANCH:-$(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD 2>/dev/null || echo main)}" # Domyślny profil i usługi DB_PROFILE="sqlite" # domyślnie sqlite, czyli brak profilu pgsql/mysql SERVICES=() # Przetwarzanie argumentów if [[ $# -gt 0 ]]; then case "$1" in pgsql|mysql|sqlite) DB_PROFILE="$1" shift ;; esac SERVICES=("$@") fi log() { printf "\n==> %s\n" "$*"; } command -v git >/dev/null || { echo "Brak 'git' w PATH"; exit 1; } command -v docker >/dev/null || { echo "Brak 'docker' w PATH"; exit 1; } if ! docker compose version >/dev/null 2>&1; then echo "Wymagany jest 'docker compose' (plugin), nie stary 'docker-compose'." exit 1 fi if [[ ! -f "$REPO_DIR/$COMPOSE_FILE" ]]; then if [[ -f "$REPO_DIR/compose.yaml" ]]; then COMPOSE_FILE="compose.yaml" else echo "Nie znaleziono pliku Compose w: $REPO_DIR/$COMPOSE_FILE" exit 1 fi fi cd "$REPO_DIR" log "Aktualizacja repo: git pull --ff-only ($GIT_REMOTE/$GIT_BRANCH)" git fetch --prune "$GIT_REMOTE" git checkout "$GIT_BRANCH" >/dev/null 2>&1 || true git pull --ff-only "$GIT_REMOTE" "$GIT_BRANCH" log "Zapisywanie hasha commita do version.txt" git rev-parse --short HEAD > version.txt log "Docker Compose DOWN (usuwanie kontenerów i osieroconych usług)" docker compose -f "$COMPOSE_FILE" down --remove-orphans log "Generowanie default.vcl z APP_PORT=$APP_PORT" envsubst < deploy/varnish/default.vcl.template > deploy/varnish/default.vcl # Budowanie i uruchamianie log "Docker Compose UP (build bez deps) dla profilu: $DB_PROFILE i serwisów: ${SERVICES[*]:-(wszystkie)}" if [[ "$DB_PROFILE" == "pgsql" ]]; then PROFILE_OPT="--profile pgsql" elif [[ "$DB_PROFILE" == "mysql" ]]; then PROFILE_OPT="--profile mysql" else PROFILE_OPT="" fi if [[ ${#SERVICES[@]} -gt 0 ]]; then docker compose -f "$COMPOSE_FILE" up -d --no-deps --build $PROFILE_OPT "${SERVICES[@]}" else docker compose -f "$COMPOSE_FILE" up -d --no-deps --build $PROFILE_OPT fi log "Gotowe ✅ (wersja: $(cat version.txt))"