name: Deer Flow Docker Image CI

on:
  push:
    branches: [ "main" ]
    paths:
      - 'runtime/deer-flow/**'
      - 'scripts/images/deer-flow-backend/**'
      - 'scripts/images/deer-flow-frontend/**'
      - '.github/workflows/docker-image-deer-flow.yml'
  pull_request:
    branches: [ "main" ]
    paths:
      - 'runtime/deer-flow/**'
      - 'scripts/images/deer-flow-backend/**'
      - 'scripts/images/deer-flow-frontend/**'
      - '.github/workflows/docker-image-deer-flow.yml'
  workflow_dispatch:
  workflow_call:

jobs:
  build-and-push-amd:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v4

      - name: Login to GitHub Container Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set Docker Image Tag
        id: set-tag
        run: |
          if [[ $GITHUB_REF == refs/tags/v* ]]; then
            TAG=${GITHUB_REF#refs/tags/v}
            echo "TAGS=amd64-$TAG" >> $GITHUB_OUTPUT
          elif [[ $GITHUB_REF == refs/heads/main ]]; then
            echo "TAGS=amd64" >> $GITHUB_OUTPUT
          else
            echo "TAGS=amd64-temp" >> $GITHUB_OUTPUT
          fi

      - name: Build Docker Image
        run: |
          docker build -t deer-flow-backend:amd64 . -f scripts/images/deer-flow-backend/Dockerfile
          docker build -t deer-flow-frontend:amd64 . -f scripts/images/deer-flow-frontend/Dockerfile

      - name: Tag & Push Docker Image
        if: github.event_name != 'pull_request'
        run: |
          LOWERCASE_REPO=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
          docker tag deer-flow-backend:amd64 ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }}
          docker tag deer-flow-frontend:amd64 ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }}
          docker push ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }}
          docker push ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }}
          

  build-and-push-arm:
    runs-on: ubuntu-24.04-arm
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v4

      - name: Login to GitHub Container Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set Docker Image Tag
        id: set-tag
        run: |
          if [[ $GITHUB_REF == refs/tags/v* ]]; then
            TAG=${GITHUB_REF#refs/tags/v}
            echo "TAGS=arm64-$TAG" >> $GITHUB_OUTPUT
          elif [[ $GITHUB_REF == refs/heads/main ]]; then
            echo "TAGS=arm64" >> $GITHUB_OUTPUT
          else
            echo "TAGS=arm64-temp" >> $GITHUB_OUTPUT
          fi

      - name: Build Docker Image
        run: |
          docker build -t deer-flow-backend:arm64 . -f scripts/images/deer-flow-backend/Dockerfile
          docker build -t deer-flow-frontend:arm64 . -f scripts/images/deer-flow-frontend/Dockerfile

      - name: Tag & Push Docker Image
        if: github.event_name != 'pull_request'
        run: |
          LOWERCASE_REPO=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
          docker tag deer-flow-backend:arm64 ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }}
          docker tag deer-flow-frontend:arm64 ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }}
          docker push ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }}
          docker push ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }}

  manifest:
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request'
    needs: [ build-and-push-amd, build-and-push-arm ]
    steps:
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set Docker Image Tag
        id: set-tag
        run: |
          LOWERCASE_REPO=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
          BASE_IMAGE=ghcr.io/$LOWERCASE_REPO/datamate-${{ inputs.service_name }}
          if [[ $GITHUB_REF == refs/tags/v* ]]; then
            TAG=${GITHUB_REF#refs/tags/v}
            echo "TAGS=$TAG" >> $GITHUB_OUTPUT
            echo "ARM_TAGS=arm64-$TAG" >> $GITHUB_OUTPUT
            echo "AMD_TAGS=amd64-$TAG" >> $GITHUB_OUTPUT
          elif [[ $GITHUB_REF == refs/heads/main ]]; then
            echo "TAGS=latest" >> $GITHUB_OUTPUT
            echo "ARM_TAGS=arm64" >> $GITHUB_OUTPUT
            echo "AMD_TAGS=amd64" >> $GITHUB_OUTPUT
          else
            echo "TAGS=temp" >> $GITHUB_OUTPUT
            echo "ARM_TAGS=arm64-temp" >> $GITHUB_OUTPUT
            echo "AMD_TAGS=amd64-temp" >> $GITHUB_OUTPUT
          fi

      - name: Manifest Docker Image
        run: |
          LOWERCASE_REPO=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
          docker manifest create ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }} \
            ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.AMD_TAGS }} \
            ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.ARM_TAGS }}
          docker manifest push ghcr.io/$LOWERCASE_REPO/deer-flow-backend:${{ steps.set-tag.outputs.TAGS }}
          docker manifest create ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }} \
            ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.AMD_TAGS }} \
            ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.ARM_TAGS }}
          docker manifest push ghcr.io/$LOWERCASE_REPO/deer-flow-frontend:${{ steps.set-tag.outputs.TAGS }}