name: Build and deploy plugins to environment server

on:
  push:
    branches: [dev, stg, prd]
    paths:
      - 'wp-content/plugins/**'
  workflow_dispatch:

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

jobs:
  deploy:
    runs-on: ubuntu-latest

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

    defaults:
      run:
        working-directory: ./

    steps:
      - uses: actions/checkout@v4

      - run: |
          for folder in wp-content/plugins/*; do
            if [ -d "$folder" ]; then
              plugin_name=$(basename $folder)
              sshpass -p '${{ secrets.SSH_PASSWORD }}' ssh -oStrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} mkdir -p ${{ secrets.SSH_PATH }}/wp-content/plugins/$plugin_name
              sshpass -p '${{ secrets.SSH_PASSWORD }}' rsync -e 'ssh -oStrictHostKeyChecking=no' -lr --delete-after --exclude=node_modules $folder/ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.SSH_PATH }}/wp-content/plugins/$plugin_name/
            fi
          done

