name: Deploy Staging Branch code in MangoEcart

on:
    pull_request:
        branches: ["develop"]

jobs:
    # Build job
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v3

            - name: Setup Node
              uses: actions/setup-node@v3
              with:
                  node-version: "20"
                  cache: ${{ steps.detect-package-manager.outputs.manager }}

            # Detect changed files
            - name: Detect Changed Files
              id: changes
              run: |
                  git fetch --all
                  CHANGED_FILES=$(git diff --name-only origin/develop HEAD)
                  echo "Changed files: $CHANGED_FILES"
                  echo "::set-output name=changed_files::$CHANGED_FILES"

            # Debug: Print out the value of CHANGED_FILES
            - name: Debug Changed Files
              run: |
                  echo "Changed files: ${{ steps.changes.outputs.changed_files }}"

            # Deploy only modified files
            - name: Deploy Modified Files
              if: ${{ steps.changes.outputs.changed_files != '' }}
              uses: wlixcc/SFTP-Deploy-Action@v1.2.4
              with:
                  username: ${{ secrets.SFTP_USERNAME }}
                  server: ${{ secrets.SFTP_HOST }}
                  port: ${{ secrets.SFTP_PORT }}
                  password: ${{ secrets.SFTP_PASSWORD }}
                  local_path: ${{ steps.changes.outputs.changed_files }}
                  remote_path: "/var/www/html/ecart-backend"
                  sftp_only: true
           
