name: Frontend CI/CD

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'
    paths:
      - 'ui/**'
      - '.github/workflows/frontend-ci-cd.yml'
  pull_request:
    branches:
      - main
    paths:
      - 'ui/**'
      - '.github/workflows/frontend-ci-cd.yml'

env:
  REGISTRY: chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.VERSION }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Set up pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 10.12.1

      - name: Get version
        id: get_version
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          else
            VERSION=$(git describe --tags --always --dirty)
          fi
          echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT

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

      - name: Install dependencies
        run: |
          cd ui
          pnpm install

      - name: Build frontend
        run: |
          cd ui
          echo "VITE_APP_VERSION=${{ steps.get_version.outputs.VERSION }}" >> .env.production
          pnpm run build

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

      - name: List files for debugging
        run: |
          echo "Current directory: $(pwd)"
          echo "Listing current directory:"
          ls -la
          echo "Listing dist directory:"
          ls -la ui/dist || echo "dist directory not found"
          echo "Checking if dist.tar exists:"
          ls -la ui/dist.tar || echo "dist.tar not found"

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

  package:
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: frontend-build

      - name: Check file structure
        run: |
          echo "Current directory: $(pwd)"
          echo "Listing current directory:"
          ls -la

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

      - 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: ${{ env.REGISTRY }}
          username: ${{ secrets.CT_ALIYUN_USER }}
          password: ${{ secrets.CT_ALIYUN_PASS }}

      - name: Package and push
        uses: docker/build-push-action@v5
        with:
          context: ui
          file: ui/.Dockerfile
          push: true
          platforms: linux/amd64, linux/arm64
          tags: |
            ${{ env.REGISTRY }}/frontend:${{ needs.build.outputs.version }}
            ${{ env.REGISTRY }}/frontend:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
