Files
recipe-app/deploy.sh
T
Nils-Johan Gynther da193b26ef
Test Suite / test (24.15.0) (push) Has been cancelled
feat: enhance deploy script with improved flag handling and usage instructions
2026-05-09 23:05:02 +02:00

100 lines
3.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# deploy.sh Bygg och starta om recipe-app
# Kör från: /opt/containers/recipe-app/
# Kräver: .env-fil i samma mapp
#
# Användning:
# ./deploy.sh bygg allt (backend + flutter + importer)
# ./deploy.sh --backend bygg bara backend
# ./deploy.sh --flutter bygg bara flutter
# ./deploy.sh --importer bygg bara importer
# ./deploy.sh --seed kör seed (körs ej per default)
# ./deploy.sh --backend --seed kombinera flaggor fritt
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ── Flaggor ──────────────────────────────────────────────────────────────────
BUILD_BACKEND=false
BUILD_FLUTTER=false
BUILD_IMPORTER=false
RUN_SEED=false
BUILD_ALL=true # om inga specifika tjänster anges, bygg allt
for arg in "$@"; do
case "$arg" in
--backend) BUILD_BACKEND=true; BUILD_ALL=false ;;
--flutter) BUILD_FLUTTER=true; BUILD_ALL=false ;;
--importer) BUILD_IMPORTER=true; BUILD_ALL=false ;;
--seed) RUN_SEED=true ;;
--help|-h)
sed -n '/^# Användning:/,/^[^#]/p' "$0" | grep '^#' | sed 's/^# \?//'
exit 0
;;
*) echo "Okänd flagga: $arg (--help för hjälp)"; exit 1 ;;
esac
done
if [ "$BUILD_ALL" = true ]; then
BUILD_BACKEND=true
BUILD_FLUTTER=true
BUILD_IMPORTER=true
fi
# ── Validering ────────────────────────────────────────────────────────────────
if [ ! -f ".env" ]; then
echo "Fel: .env saknas. Kopiera .env.example och fyll i värdena:"
echo " cp .env.example .env && nano .env"
exit 1
fi
# ── Git pull ──────────────────────────────────────────────────────────────────
echo "Hämtar senaste kod (recipe-app)..."
git pull origin main
echo "Hämtar senaste kod (microservice-importer)..."
(cd "$SCRIPT_DIR/../microservice-importer" && git pull origin main)
# ── Bygger valda tjänster ─────────────────────────────────────────────────────
COMPOSE="docker compose -f compose.yml -f compose.flutter.yml"
SERVICES=""
[ "$BUILD_BACKEND" = true ] && SERVICES="$SERVICES recipe-api"
[ "$BUILD_FLUTTER" = true ] && SERVICES="$SERVICES recipe-flutter"
[ "$BUILD_IMPORTER" = true ] && SERVICES="$SERVICES importer-api"
echo "Bygger: ${SERVICES:-alla tjänster}..."
# --pull=false hindrar Docker från att kontrollera nya versioner av basimages
$COMPOSE build --pull=false $SERVICES
echo "Startar tjänster..."
$COMPOSE up -d
# ── Seed (opt-in) ─────────────────────────────────────────────────────────────
if [ "$RUN_SEED" = true ]; then
MARIADB_ROOT_PASSWORD=$(grep MARIADB_ROOT_PASSWORD .env | cut -d '=' -f2 | tr -d '"' | tr -d "'")
MARIADB_DATABASE=$(grep MARIADB_DATABASE .env | cut -d '=' -f2 | tr -d '"' | tr -d "'")
echo "Väntar på att databasen är redo..."
for i in $(seq 1 30); do
if docker exec recipe-db mariadb-admin ping -h 127.0.0.1 -uroot -p"$MARIADB_ROOT_PASSWORD" --silent 2>/dev/null; then
break
fi
echo " ...försök $i/30"
sleep 2
done
if [ -f "db/seeds/seed_all.sql" ]; then
docker exec -i recipe-db mariadb -uroot -p"$MARIADB_ROOT_PASSWORD" "$MARIADB_DATABASE" \
< db/seeds/seed_all.sql
echo "Full seed klar."
else
echo "Ingen db/seeds/seed_all.sql hittades — hoppar över seed."
fi
fi
echo "Status:"
$COMPOSE ps