#!/bin/bash # deploy.sh — Deploy main branch to production (vhtv-stiftung.de) # # Usage: # ./deploy.sh # Deploy current main to production # ./deploy.sh --dry-run # Show what would happen without deploying # # Prerequisites: # - SSH access to the production server (key-based auth) # - Production .env file at /opt/stiftung/.env on the server # - Git remote 'origin' configured on the server pointing to Gitea set -euo pipefail SERVER="${DEPLOY_SERVER:-remmer@vhtv-stiftung.de}" PROD_DIR="${DEPLOY_DIR:-/opt/stiftung}" COMPOSE_FILE="compose.yml" DRY_RUN=false if [[ "${1:-}" == "--dry-run" ]]; then DRY_RUN=true echo "=== DRY RUN — no changes will be made ===" fi echo "=== Stiftung Production Deployment ===" echo "Server: $SERVER" echo "Path: $PROD_DIR" echo "Compose: $COMPOSE_FILE" echo "" # Verify local main is up to date with remote LOCAL_MAIN=$(git rev-parse main 2>/dev/null || echo "unknown") echo "Local main: $LOCAL_MAIN" if [[ "$DRY_RUN" == true ]]; then echo "" echo "Would SSH to $SERVER and:" echo " 1. git fetch origin main && git reset --hard origin/main" echo " 2. docker compose down" echo " 3. docker compose up -d --build" echo " 4. Run migrations and collectstatic" echo "" echo "=== Dry run complete ===" exit 0 fi echo "" read -p "Deploy main ($LOCAL_MAIN) to production? [y/N] " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 0 fi echo "" echo "=== Deploying to $SERVER:$PROD_DIR ===" ssh "$SERVER" bash -s "$PROD_DIR" "$COMPOSE_FILE" << 'DEPLOY_SCRIPT' set -euo pipefail PROD_DIR="$1" COMPOSE_FILE="$2" cd "$PROD_DIR" echo "--- Fetching latest main ---" git fetch origin main git checkout main git reset --hard origin/main echo "" echo "--- Pulling standard images ---" docker compose -f "$COMPOSE_FILE" pull db redis grampsweb || echo "Some pulls failed, using cached" echo "" echo "--- Stopping containers ---" docker compose -f "$COMPOSE_FILE" down echo "" echo "--- Building and starting ---" docker compose -f "$COMPOSE_FILE" up -d --build echo "" echo "--- Waiting for services to start ---" sleep 20 echo "" echo "--- Running migrations ---" docker compose -f "$COMPOSE_FILE" exec -T web python manage.py migrate echo "" echo "--- Collecting static files ---" docker compose -f "$COMPOSE_FILE" exec -T web python manage.py collectstatic --noinput echo "" echo "--- Service status ---" docker compose -f "$COMPOSE_FILE" ps echo "" echo "=== Deployment complete ===" DEPLOY_SCRIPT echo "" echo "=== Done! Production updated to main ($LOCAL_MAIN) ===" echo "Site: https://vhtv-stiftung.de"