kind: pipeline
name: Touchh Hotel AntiFraud Pipeline
namespace: touchh

steps:
  # Шаг 1: Клонирование репозитория
  - name: clone_repo
    image: alpine/git
    commands:
      - if [ ! -d .git ]; then git clone $DRONE_REPO_URL .; fi
      - git fetch --all
      - git reset --hard $DRONE_COMMIT

  # Шаг 2: Обновление и запуск с помощью update.sh
  - name: deploy_app
    image: docker:24
    environment:
      MYSQL_PASSWORD: touchh
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - apk add --no-cache bash
      - chmod +x ./bin/update
      - docker-compose up -d
      - until docker inspect -f '{{.State.Running}}' web | grep true; do echo "Waiting for container to be running..."; sleep 5; done
      - git branch --set-upstream-to=origin/PMSManager_refactor PMSManager_refactor || true
      - ./bin/update

  # Шаг 3: Миграция базы данных
  - name: run_migrations
    image: docker:24
    environment:
      MYSQL_PASSWORD: touchh
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    depends_on:
      - web
    commands:
      - apk add --no-cache bash
      - until docker inspect -f '{{.State.Running}}' web | grep true; do echo "Waiting for container to be running..."; sleep 5; done
      - chmod +x ./bin/cli
      - ./bin/cli migrate

  # Шаг 4: Тестирование
  - name: run_tests
    image: python:3.12-alpine
    environment:
      MYSQL_PASSWORD: touchh
    depends_on:
      - db
    commands:
      - apk add --no-cache mariadb-client mariadb-connector-c-dev gcc musl-dev pkgconfig
      - pip install -r requirements.txt
      - python manage.py test

services:
  # Сервис базы данных
  - name: db
    image: mariadb:11.6
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: touchh
      MYSQL_USER: touchh
      MYSQL_PASSWORD: touchh
    volumes:
      - name: mysql_data
        temp: {}

  # Сервис Django (Web)
  - name: web
    image: touchh-py
    environment:
      MYSQL_PASSWORD: touchh
    command: ['python3', 'manage.py', 'runserver', '0.0.0.0:8000']
    ports:
      - port: 8000
    depends_on:
      - db

  # Сервис Telegram Bot
  - name: bot
    image: touchh-py
    environment:
      MYSQL_PASSWORD: touchh
    command: ['python3', 'manage.py', 'run_bot']
    depends_on:
      - db

  # Сервис планировщика задач
  - name: scheduler
    image: touchh-py
    environment:
      MYSQL_PASSWORD: touchh
    command: ['python3', 'manage.py', 'start_scheduler']
    depends_on:
      - db

volumes:
  - name: docker_sock
    host:
      path: /var/run/docker.sock
  - name: mysql_data
    temp: {}

trigger:
  event:
    - push
    - pull_request
