x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  gitea:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}gitea/gitea:${GITEA_VERSION:-1.25.4-rootless}
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - TZ=${TZ:-UTC}
      - GITEA__database__DB_TYPE=${GITEA_DB_TYPE:-postgres}
      - GITEA__database__HOST=${GITEA_POSTGRES_HOST:-db:5432}
      - GITEA__database__USER=${POSTGRES_USER:-gitea}
      - GITEA__database__NAME=${POSTGRES_DB:-gitea}
      - GITEA__database__PASSWD=${POSTGRES_PASSWORD:-gitea}
    volumes:
      - gitea_data:/var/lib/gitea
      - gitea_config:/etc/gitea
    ports:
      - '${GITEA_HTTP_PORT:-3000}:3000'
      - '${GITEA_SSH_PORT:-2222}:2222'
    depends_on:
      db:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: ${GITEA_CPU_LIMIT:-1.0}
          memory: ${GITEA_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${GITEA_CPU_RESERVATION:-0.5}
          memory: ${GITEA_MEMORY_RESERVATION:-512M}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:3000/'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-17.6}
    profiles:
      - ${COMPOSE_PROFILES:-}
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=${POSTGRES_USER:-gitea}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-gitea}
      - POSTGRES_DB=${POSTGRES_DB:-gitea}
    volumes:
      - postgres:/var/lib/postgresql/data
    deploy:
      resources:
        limits:
          cpus: ${GITEA_DB_CPU_LIMIT:-1.0}
          memory: ${GITEA_DB_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${GITEA_DB_CPU_RESERVATION:-0.5}
          memory: ${GITEA_DB_MEMORY_RESERVATION:-512M}
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  gitea_data:
  gitea_config:
  postgres:
