kind: pipeline
type: docker
name: api-extranetwork

# Sadece main branch'te tetiklenir
trigger:
  branch:
    - main
  event:
    - push

steps:
  # -------------------------------------------------------
  # 1. Bağımlılıkları kur
  # -------------------------------------------------------
  - name: composer-install
    image: composer:2.2
    volumes:
      - name: composer-cache
        path: /root/.composer/cache
    commands:
      - composer config platform.php 7.4.0
      - composer install --optimize-autoloader --no-interaction --prefer-dist --ignore-platform-req=ext-gd

  # -------------------------------------------------------
  # 2. PHP Unit testlerini çalıştır
  # -------------------------------------------------------
  - name: tests
    image: php:7.4-cli
    commands:
      - apt-get update -qq
      - apt-get install -y --no-install-recommends libzip-dev libpng-dev libxml2-dev libonig-dev > /dev/null 2>&1
      - docker-php-ext-install pdo pdo_mysql zip mbstring > /dev/null 2>&1
      - cp .env.example .env
      - APP_KEY=$(php -r "echo 'base64:'.base64_encode(random_bytes(32));") && sed -i "s|APP_KEY=.*|APP_KEY=${APP_KEY}|" .env
      - mkdir -p storage/framework/views storage/framework/cache storage/framework/sessions storage/logs
      - vendor/bin/phpunit --no-coverage --stop-on-failure
    environment:
      DB_CONNECTION: sqlite
      DB_DATABASE: ":memory:"
    depends_on:
      - composer-install

  # -------------------------------------------------------
  # 3. Docker image build & Gitea Container Registry'e push
  # -------------------------------------------------------
  - name: docker-build-push
    image: plugins/docker
    settings:
      registry: git.extranetwork.net
      repo: git.extranetwork.net/gitea/api-extranetwork
      username: gitea
      password: 9359f6c8267582bf6c8197d62a6374150c63cc6a
      tags:
        - latest
        - ${DRONE_COMMIT_SHA:0:8}
      insecure: true
    depends_on:
      - tests
    when:
      branch:
        - main
      event:
        - push

  # -------------------------------------------------------
  # 4. SSH ile sunucuya deploy et
  # -------------------------------------------------------
  - name: deploy
    image: appleboy/drone-ssh
    settings:
      host: 178.105.136.25
      username: deploy
      key:
        from_secret: DEPLOY_SSH_KEY
      port: 22
      script:
        - cd /opt/api.extranetwork.com
        - docker compose pull
        - docker compose up -d --remove-orphans
        - docker compose exec -T app php artisan migrate --force
        - docker compose exec -T app php artisan config:cache
        - docker compose exec -T app php artisan route:cache
        - docker compose exec -T app php artisan view:cache
        - echo "Deploy tamamlandı - $(date)"
    depends_on:
      - docker-build-push
    when:
      branch:
        - main
      event:
        - push

# -------------------------------------------------------
# Uçucu volume (composer cache pipeline içinde paylaşılır)
# -------------------------------------------------------
volumes:
  - name: composer-cache
    temp: {}
