# ════════════════════════════════════════════════════════════════
# 光湖灯塔 · Gitea + PostgreSQL + Redis + Actions Runner
# Sovereign: TCS-0002∞ · 国作登字-2026-A-00037559
# 数据根: ${DATA_ROOT}/lighthouse/
# ════════════════════════════════════════════════════════════════

services:
  gitea-db:
    image: postgres:15-alpine
    container_name: lighthouse-gitea-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${GITEA_DB_USER}
      POSTGRES_PASSWORD: ${GITEA_DB_PASS}
      POSTGRES_DB: ${GITEA_DB_NAME}
      TZ: ${TZ}
    volumes:
      - ${DATA_ROOT}/lighthouse/postgres:/var/lib/postgresql/data
    networks:
      - lighthouse-net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${GITEA_DB_USER} -d ${GITEA_DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 10

  gitea-redis:
    image: redis:7-alpine
    container_name: lighthouse-gitea-redis
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - ${DATA_ROOT}/lighthouse/redis:/data
    networks:
      - lighthouse-net
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 10

  gitea:
    image: gitea/gitea:1.21.11
    container_name: lighthouse-gitea
    restart: unless-stopped
    depends_on:
      gitea-db:
        condition: service_healthy
      gitea-redis:
        condition: service_healthy
    environment:
      USER_UID: "1000"
      USER_GID: "1000"
      TZ: ${TZ}
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: gitea-db:5432
      GITEA__database__NAME: ${GITEA_DB_NAME}
      GITEA__database__USER: ${GITEA_DB_USER}
      GITEA__database__PASSWD: ${GITEA_DB_PASS}
      GITEA__cache__ADAPTER: redis
      GITEA__cache__HOST: redis://gitea-redis:6379/0?pool_size=100&idle_timeout=180s
      GITEA__server__DOMAIN: localhost
      GITEA__server__SSH_DOMAIN: localhost
      GITEA__server__HTTP_PORT: "3000"
      GITEA__server__SSH_LISTEN_PORT: "22"
      GITEA__server__SSH_PORT: "${GITEA_SSH_PORT}"
      GITEA__service__DISABLE_REGISTRATION: "true"
      GITEA__service__REQUIRE_SIGNIN_VIEW: "false"
      GITEA__actions__ENABLED: "true"
    ports:
      - "127.0.0.1:${GITEA_HTTP_PORT}:3000"
      - "${GITEA_SSH_PORT}:22"
    volumes:
      - ${DATA_ROOT}/lighthouse/gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    networks:
      - lighthouse-net
    healthcheck:
      test: ["CMD", "curl", "-fs", "http://127.0.0.1:3000/api/v1/version"]
      interval: 15s
      timeout: 5s
      retries: 20
      start_period: 60s

  # Gitea Actions Runner 容器 (act_runner)
  # 注: 首次启动需要 ZY_GITEA_RUNNER_TOKEN, 由 Gitea Site Admin → Runners 生成
  # 这里通过 .env 注入: GITEA_RUNNER_TOKEN=xxx, 没有则容器不启 (profile=runner)
  gitea-runner:
    image: gitea/act_runner:0.2.10
    container_name: lighthouse-gitea-runner
    restart: unless-stopped
    profiles: ["runner"]
    depends_on:
      gitea:
        condition: service_healthy
    environment:
      CONFIG_FILE: /config.yaml
      GITEA_INSTANCE_URL: http://gitea:3000
      GITEA_RUNNER_REGISTRATION_TOKEN: ${GITEA_RUNNER_TOKEN:-}
      GITEA_RUNNER_NAME: ${SERVER_ID}-runner
      GITEA_RUNNER_LABELS: ubuntu-22.04,docker
    volumes:
      - ${DATA_ROOT}/lighthouse/runner:/data
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - lighthouse-net

networks:
  lighthouse-net:
    driver: bridge
    name: lighthouse-net
