#!/usr/bin/env bash set -euo pipefail # Gitea / Git deployment runner script for Proxmox LXC container with Docker. # # Usage: # ./gitea-deploy.sh # # Optional environment variables: # REPO_DIR Path to the repository root (default: script directory) # DEPLOY_BRANCH Git branch to deploy (default: main) # COMPOSE_FILE Docker Compose file path (default: docker-compose.yml) # DOCKER_CMD Docker command to use (auto-detected) # GIT_CLEAN If set to 1, clean untracked files before deploy SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="${REPO_DIR:-$SCRIPT_DIR}" DEPLOY_BRANCH="${DEPLOY_BRANCH:-main}" COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}" GIT_CLEAN="${GIT_CLEAN:-0}" if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then DOCKER_COMPOSE_CMD="docker compose" elif command -v docker-compose >/dev/null 2>&1; then DOCKER_COMPOSE_CMD="docker-compose" else echo "ERROR: docker compose command not found. Install Docker Compose or use Docker v20+ with built-in compose." exit 1 fi cd "$REPO_DIR" echo "Deploying $REPO_DIR" echo "Branch: $DEPLOY_BRANCH" echo "Compose file: $COMPOSE_FILE" if ! command -v git >/dev/null 2>&1; then echo "ERROR: git is not installed." exit 1 fi if [ ! -f "$COMPOSE_FILE" ]; then echo "ERROR: Compose file '$COMPOSE_FILE' not found in $REPO_DIR." exit 1 fi # Ensure repository is on the correct branch and in sync with remote. git fetch --all --prune git checkout "$DEPLOY_BRANCH" git reset --hard "origin/$DEPLOY_BRANCH" if [ "$GIT_CLEAN" = "1" ]; then echo "Cleaning untracked files..." git clean -fdx fi # Build and deploy with Docker Compose. echo "Stopping existing containers and removing orphans..." $DOCKER_COMPOSE_CMD -f "$COMPOSE_FILE" down --remove-orphans echo "Building image..." $DOCKER_COMPOSE_CMD -f "$COMPOSE_FILE" build --no-cache echo "Starting containers..." $DOCKER_COMPOSE_CMD -f "$COMPOSE_FILE" up -d echo "Deployment finished. Current service status:" $DOCKER_COMPOSE_CMD -f "$COMPOSE_FILE" ps exit 0