stages:
  - build
  - deploy

# 🛠 Étape 1 : Compilation des assets SCSS/JS via Vite
build-assets:
  stage: build
  image: node:18
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - public/build/
    expire_in: 30m

# 🚀 Étape 2 : Déploiement sur OVH mutualisé
deploy:
  stage: deploy
  image: alpine:latest
  needs:
    - build-assets
  before_script:
    - apk add --no-cache openssh-client rsync sshpass
    - mkdir -p ~/.ssh
    - ssh-keyscan -H ssh.cluster030.hosting.ovh.net >> ~/.ssh/known_hosts
  script:
    # ⛔️ On exclut tout ce qu’on ne veut pas écraser
    - echo ".env" > .rsync-exclude
    - echo ".env.local" >> .rsync-exclude
    - echo "vendor/" >> .rsync-exclude
    - echo "var/" >> .rsync-exclude
    - echo "uploads/" >> .rsync-exclude

    - echo "→ Déploiement OVH en cours..."

    # 🔄 Envoi du projet (y compris public/build/) sans casser le reste
    - SSHPASS="$OVH_SSH_PASS" sshpass -e rsync -avz --exclude-from=".rsync-exclude" -e "ssh -p 22 -o StrictHostKeyChecking=no" ./ "$OVH_SSH_USER@ssh.cluster030.hosting.ovh.net:/home/chenryd/www/"

    # 🧼 Vidage + warmup du cache Symfony prod
    - echo "→ Vidage du cache Symfony prod"
    - SSHPASS="$OVH_SSH_PASS" sshpass -e ssh -o StrictHostKeyChecking=no "$OVH_SSH_USER@ssh.cluster030.hosting.ovh.net" "cd /home/chenryd/www && php bin/console cache:clear --env=prod && php bin/console cache:warmup --env=prod"
  only:
    - main
