name: build_docker
run-name: ${{ gitea.event.head_commit.message }}

on:
  pull_request:
    types:
      - opened
      - synchronize
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  essential:
    runs-on: ubuntu-latest
    outputs:
      Version: 1.0.${{ gitea.run_number }}
      repo: seryus.ddns.net
      image_cpu: seryus.ddns.net/unir/paddle-ocr-cpu
      image_gpu: seryus.ddns.net/unir/paddle-ocr-gpu
      image_easyocr: seryus.ddns.net/unir/easyocr-cpu
      image_easyocr_gpu: seryus.ddns.net/unir/easyocr-gpu
      image_doctr: seryus.ddns.net/unir/doctr-cpu
      image_doctr_gpu: seryus.ddns.net/unir/doctr-gpu
      image_raytune: seryus.ddns.net/unir/raytune
    steps:
      - name: Output version info
        run: |
          echo "## Build Info" >> $GITHUB_STEP_SUMMARY
          echo "Version: 1.0.${{ gitea.run_number }}" >> $GITHUB_STEP_SUMMARY
          echo "Event: ${{ gitea.event_name }}" >> $GITHUB_STEP_SUMMARY

  # PaddleOCR CPU image (amd64 only)
  build_cpu:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push CPU image
        uses: docker/build-push-action@v5
        with:
          context: src/paddle_ocr
          file: src/paddle_ocr/Dockerfile.cpu
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_cpu }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_cpu }}:latest

  # PaddleOCR GPU image (amd64 only)
  build_gpu:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push GPU image
        uses: docker/build-push-action@v5
        with:
          context: src/paddle_ocr
          file: src/paddle_ocr/Dockerfile.gpu
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_gpu }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_gpu }}:latest

  # EasyOCR CPU image (amd64 only)
  build_easyocr:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push EasyOCR image
        uses: docker/build-push-action@v5
        with:
          context: src/easyocr_service
          file: src/easyocr_service/Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_easyocr }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_easyocr }}:latest

  # EasyOCR GPU image (amd64 only)
  build_easyocr_gpu:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push EasyOCR GPU image
        uses: docker/build-push-action@v5
        with:
          context: src/easyocr_service
          file: src/easyocr_service/Dockerfile.gpu
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_easyocr_gpu }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_easyocr_gpu }}:latest

  # DocTR CPU image (amd64 only)
  build_doctr:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push DocTR image
        uses: docker/build-push-action@v5
        with:
          context: src/doctr_service
          file: src/doctr_service/Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_doctr }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_doctr }}:latest

  # DocTR GPU image (amd64 only)
  build_doctr_gpu:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push DocTR GPU image
        uses: docker/build-push-action@v5
        with:
          context: src/doctr_service
          file: src/doctr_service/Dockerfile.gpu
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_doctr_gpu }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_doctr_gpu }}:latest

  # Ray Tune OCR image (amd64 only)
  build_raytune:
    runs-on: ubuntu-latest
    needs: essential
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ needs.essential.outputs.repo }}
          username: username
          password: ${{ secrets.CI_READWRITE }}

      - name: Build and push Ray Tune image
        uses: docker/build-push-action@v5
        with:
          context: src/raytune
          file: src/raytune/Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ${{ needs.essential.outputs.image_raytune }}:${{ needs.essential.outputs.Version }}
            ${{ needs.essential.outputs.image_raytune }}:latest
