name: Deploy site (production)

# Déploie le site vitrine (packages/site) sur Plesk via FTPS (lftp), à l'identique
# de lidge_web / tracksniff-web. Ne se déclenche que sur un changement du site.
on:
  push:
    branches:
      - main
    paths:
      - 'packages/site/**'
      - '.gitea/workflows/prod.yml'
  workflow_dispatch:

concurrency:
  group: deploy-site
  cancel-in-progress: true

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    container:
      image: node:22-bookworm
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      # Install scopé au workspace du site : pas de build des deps natives du
      # serveur (node-pty / node:sqlite) qui n'ont rien à faire ici.
      - name: Install deps (@arboretum/site)
        run: npm install -w @arboretum/site --no-audit --no-fund

      - name: Build site
        run: npm run build:site

      - name: Check build output
        run: ls -la packages/site/dist

      - name: Deploy via FTPS (lftp)
        run: |
          if [ -z "$REMOTE_PATH" ]; then
            echo "::error::Secret SITE_REMOTE_PATH manquant (chemin docroot du vhost git-arboretum.com)."
            exit 1
          fi
          apt-get update && apt-get install -y lftp
          lftp -c "
          open -u \"$FTP_USER\",\"$FTP_PASSWORD\" \"$FTP_HOST\"
          set ssl:verify-certificate no
          mirror -R --delete --verbose packages/site/dist \"$REMOTE_PATH\"
          "
        env:
          FTP_HOST: ${{ secrets.FTP_HOST }}
          FTP_USER: ${{ secrets.FTP_USER }}
          FTP_PASSWORD: ${{ secrets.FTP_PASSWORD }}
          # Chemin docroot du vhost git-arboretum.com sur Plesk (ex. /httpdocs/arboretum/public).
          REMOTE_PATH: ${{ secrets.SITE_REMOTE_PATH }}
