name: Web Build and Push

on:
  push:
    branches:
      - frontend-*
      - admin-*
      - app-*
    tags:
      - 'admin-v[0-9]+.[0-9]+.[0-9]+*'
      - 'app-v[0-9]+.[0-9]+.[0-9]+*'
      - 'v[0-9]+.[0-9]+.[0-9]+*'
  pull_request:
    branches:
      - main
    paths:
      - 'web/**'

jobs:
  version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.VERSION }}

    steps:
      - name: Get version
        id: get_version
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            # 支持 admin-v* / app-v* / v*
            if [[ $GITHUB_REF == refs/tags/admin-v* ]]; then
              echo "VERSION=${GITHUB_REF#refs/tags/admin-v}" >> $GITHUB_OUTPUT
            elif [[ $GITHUB_REF == refs/tags/app-v* ]]; then
              echo "VERSION=${GITHUB_REF#refs/tags/app-v}" >> $GITHUB_OUTPUT
            else
              echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
            fi
          else
            echo "VERSION=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
          fi

  build:
    runs-on: ubuntu-latest
    needs: [version]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 10

      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Setup pnpm cache
        uses: actions/cache@v3
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('web/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install dependencies
        run: |
          cd web
          pnpm install --frozen-lockfile --prefer-offline

      - name: Setup Env for admin
        run: |
          cd web/admin
          echo "VITE_APP_VERSION=${{ needs.version.outputs.version }}" >> .env.production

      - name: Build admin and app (parallel)
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
        run: |
          cd web
          pnpm run build

      - name: 'Tar admin files'
        run: tar -cvf web/admin/dist.tar web/admin/dist

      - name: Upload admin build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: admin-build
          path: web/admin/dist.tar
          if-no-files-found: error
          include-hidden-files: true

      - name: 'Tar app files'
        run: tar -cvf web/app/dist.tar web/app/dist

      - name: Upload app build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: app-build
          path: web/app/dist.tar
          if-no-files-found: error
          include-hidden-files: true

  package:
    needs: [build, version]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        project: [admin, app]
    if: startsWith(github.ref, 'refs/tags/')

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: ${{ matrix.project }}-build

      - name: Extract files
        run: |
          tar -xvf dist.tar

      - name: Check file structure
        run: |
          echo "Current directory: $(pwd)"
          echo "Listing web/${{ matrix.project }} directory:"
          ls -la web/${{ matrix.project }}
          echo "Listing web/${{ matrix.project }}/dist directory:"
          ls -la web/${{ matrix.project }}/dist

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Aliyun Container Registry
        uses: docker/login-action@v3
        with:
          registry: chaitin-registry.cn-hangzhou.cr.aliyuncs.com
          username: ${{ secrets.CT_ALIYUN_USER }}
          password: ${{ secrets.CT_ALIYUN_PASS }}

      - name: Package and push
        uses: docker/build-push-action@v5
        with:
          context: ./web/${{ matrix.project }}
          file: ./web/${{ matrix.project }}/Dockerfile
          push: true
          platforms: linux/amd64, linux/arm64
          tags: chaitin-registry.cn-hangzhou.cr.aliyuncs.com/chaitin/panda-wiki-${{ matrix.project == 'admin' && 'nginx' || 'app' }}:v${{ needs.version.outputs.version }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
