Files
recipe-app/deploy.sh
T
2026-05-09 23:09:36 +02:00

109 lines
4.3 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 (snabbast, ~2-3 min)
# ./deploy.sh --flutter bygg bara flutter web-app
# ./deploy.sh --importer bygg bara importer-microservice
# ./deploy.sh --seed kör full seed på databasen (opt-in)
# ./deploy.sh --pull-always kontrollera uppdateringar för basimages (flutter:3.41.9, node:24.15.0 etc)
# ./deploy.sh --backend --seed kombinera flaggor fritt (git pull körs alltid)
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
PULL_IMAGES=false # --pull=false är standard (snabbt)
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 ;;
--pull-always) PULL_IMAGES=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}..."
if [ "$PULL_IMAGES" = true ]; then
# Kontrollera om nya versioner av basimages finns på Docker Hub / ghcr.io
echo " (kontrollerar uppdateringar för basimages...)"
$COMPOSE build $SERVICES
else
# Standard: använd lokala cachade images, snabbare
$COMPOSE build --pull=false $SERVICES
fi
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