name: Manual Deployment

on:
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.2'
        extensions: mbstring, intl, pdo, pdo_mysql

    - name: Install PHP dependencies
      run: |
        mkdir -p var/logs 
        mkdir -p var/cache
        composer install --no-scripts

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '18'
    
    - name: Setup SSH
      uses: webfactory/ssh-agent@v0.5.3
      with:
        ssh-private-key: ${{ secrets.OVH_SSH_PRIVATE_KEY }}

    - name: Install Node.js dependencies
      run: yarn install
      
    - name: Build project
      run: |
        yarn encore production --progress
    
    - name: Copy build to server via SCP
      uses: appleboy/scp-action@v0.1.7
      with:
        host: ${{ secrets.OVH_SSH_HOST }}
        username: ${{ secrets.OVH_SSH_USERNAME }}
        key: ${{ secrets.OVH_SSH_PRIVATE_KEY }}
        port: 22
        source: "public/build/*"
        target: "/homez.122/batrams/Kplan/public"

    - name: Deploy to OVH server
      run: |
        ssh -o StrictHostKeyChecking=no ${{ secrets.OVH_SSH_USERNAME }}@${{ secrets.OVH_SSH_HOST }} << 'EOF'
          cd ~/Kplan
          git reset --hard HEAD  # Discard local changes
          git pull origin master
        EOF
