name: Deploy Laravel Staging

on:
  push:
    branches: [staging]

jobs:
  deploy:
    runs-on: self-hosted

    steps:
      - name: Configure Git credentials
        run: |
          git config --global credential.helper store
          echo "https://staging-runner:${{ env.GITEA_TOKEN }}@code.romhackplaza.org" \
            > /root/.git-credentials

      - name: Mark Git Folder as safe
        run: git config --global --add safe.directory /deploy/laravel

      - name: Code pull
        run: |
          git -C /deploy/laravel fetch origin staging
          git -C /deploy/laravel reset --hard origin/staging

      - name: Storage Link
        run: |
          docker exec staging_rhpz_web \
            php /sites/laravel/artisan storage:link --force

      - name: Composer Dependencies
        run: |
          docker exec staging_rhpz_web composer install \
            --no-dev \
            --optimize-autoloader \
            --working-dir=/sites/laravel

      - name: JS Dependencies
        run: cd /deploy/laravel && npm ci

      - name: Build JS/CSS
        run: cd /deploy/laravel && npm run build

      - name: Migrations
        run: |
          docker exec staging_rhpz_web \
            php /sites/laravel/artisan migrate --force

      - name: Opti Laravel
        run: |
          docker exec staging_rhpz_web \
            php /sites/laravel/artisan optimize
