name: Deploy app frontend
run-name: ${{ github.actor }} deployed to build branch
on:
  push:
    branches:
      - main
    paths:
      - 'apps/frontend/**'
      - '.gitea/workflows/deploy-app-frontend.yaml'
      - 'docker-compose.yml'

jobs:
  app frontend:
    runs-on: ubuntu-latest

    container:
       volumes:
          - /www/hereconnect.condev.ru:/www/hereconnect.condev.ru

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '22'
      - name: Set up Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: 'latest' # или укажите конкретную версию, например, '1.0.0'

      - name: install dependencies
        run: |
          corepack enable pnpm && corepack use pnpm@8.15.9
          pnpm deploy --filter frontend --frozen-lockfile build/frontend

      - name: build production
        run: cd build/frontend && bun vite build --emptyOutDir --outDir /www/hereconnect.condev.ru/build-${{env.GITHUB_SHA}}-prod/

      - name: compress production with Brotli
        uses: StefH/ghaction-CompressFiles@v2
        with:
          path: /www/hereconnect.condev.ru/build-${{env.GITHUB_SHA}}-prod/
          extensions: '.css,.html,.js,.svg,.eot,.ttf,.woff,.woff2,.ico'
          tools: 'brotli'

      - name: deploy production
        run:  mv -v /www/hereconnect.condev.ru/current-prod /www/hereconnect.condev.ru/old-prod && mv -v /www/hereconnect.condev.ru/build-${{env.GITHUB_SHA}}-prod /www/hereconnect.condev.ru/current-prod && rm -rf /www/hereconnect.condev.ru/old-prod

#      - name: build dev
#        run: cd build/frontend && bun vite build --emptyOutDir --mode development --outDir /www/hereconnect.condev.ru/build-${{env.GITHUB_SHA}}-dev/
#
#      - name: deploy dev
#        run: mv -v /www/hereconnect.condev.ru/current-dev /www/hereconnect.condev.ru/old-dev && mv -v /www/hereconnect.condev.ru/build-${{env.GITHUB_SHA}}-dev /www/hereconnect.condev.ru/current-dev && rm -rf /www/hereconnect.condev.ru/old-dev

      - name: cleanup build directories
        if: always()
        run: |
          echo "Found directories:"
          find /www/hereconnect.condev.ru/ -maxdepth 1 -type d -name "build-${{env.GITHUB_SHA}}-*"

          echo "Deleting directories:"
          find /www/hereconnect.condev.ru/ -maxdepth 1 -type d -name "build-${{env.GITHUB_SHA}}-*" -print -exec rm -rf {} +
