name: Build and deploy theme to environment server

on:
  push:
    branches: [dev]
    paths:
      - 'wp-content/themes/**'
  workflow_dispatch:

env:
  THEME: filmspourenfants
  WP_CLI_PACKAGES_DIR: ${{ vars.WP_CLI_PACKAGES_DIR || '~/.wp-cli/packages/' }}

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  composer:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'

      - uses: ramsey/composer-install@v2
        with:
          working-directory: ./wp-content/themes/${{ env.THEME }}/

      - name: Run linting
        working-directory: ./wp-content/themes/${{ env.THEME }}/
        run: |
          composer run-script lint:php
          composer run-script lint:wpcs

  node-build:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./wp-content/themes/${{ env.THEME }}/

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"
          cache-dependency-path: ./wp-content/themes/${{ env.THEME }}/package-lock.json

      - run: npm ci
      - run: npm run build

      - name: Upload build files
        uses: actions/upload-artifact@v4
        with:
          name: build-files
          path: "wp-content/themes/${{ env.THEME }}/build/"

  deploy:
    needs: [composer, node-build]

    runs-on: ubuntu-latest

    environment:
      name: ${{ github.ref_name }}

    defaults:
      run:
        working-directory: ./

    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          name: build-files
          path: "wp-content/themes/${{ env.THEME }}/build/"

      - run: sshpass -p '${{ secrets.SSH_PASSWORD }}' rsync -e 'ssh -oStrictHostKeyChecking=no' -lr --delete-after --exclude=node_modules --exclude='twenty*' wp-content/themes/ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.SSH_PATH }}/wp-content/themes/

      - run: |
          sshpass -p '${{ secrets.SSH_PASSWORD }}' ssh -v -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} <<'ENDSSH'
          cd ${{ secrets.SSH_PATH }}
          WP_CMD=""
          if command -v wp &> /dev/null
          then
            WP_CMD="wp"
            echo "wp found"
          elif command -v wp-cli &> /dev/null
          then
            WP_CMD="wp-cli"
            echo "wp-cli found"
          else
            echo "wp not found"
          fi
          if command -v $WP_CMD &> /dev/null
          then
            $WP_CMD cache flush
            if $WP_CMD plugin is-active wp-rocket
            then
              export WP_CLI_PACKAGES_DIR=${{ env.WP_CLI_PACKAGES_DIR }}
              if ! $($WP_CMD cli has-command rocket)
              then
                $WP_CMD package install wp-media/wp-rocket-cli
              fi
              $WP_CMD rocket clean --confirm
              $WP_CMD rocket preload
            fi
          else
            echo "wp not found"
          fi
          ENDSSH
