stages:
  - deploy

deploy_to_ec2:
  stage: deploy
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - echo "$DEPLOY_SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan -H $EC2_HOST >> ~/.ssh/known_hosts
  script:
    - echo "🚀 Déploiement en cours..."
    - ssh ubuntu@$EC2_HOST "
        cd /var/www/html &&
        git pull origin dev &&
        composer install --no-dev --optimize-autoloader &&
        php artisan config:clear &&
        php artisan cache:clear &&
        sudo systemctl restart apache2
      "
  only:
    - dev
