name: build | Build and Push Cognee MCP Docker Image to dockerhub

on:
  push:
    branches:
      - main

jobs:
  docker-build-and-push:
    runs-on:
      group: Default
      labels:
        - docker_build_runner

    steps:
      - name: Check and free disk space before build
        run: |
          echo "=== Before cleanup ==="
          df -h
          echo "Removing unused preinstalled SDKs to free space..."
          sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc || true
          docker system prune -af || true
          echo "=== After cleanup ==="
          df -h

      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          buildkitd-flags: --root /tmp/buildkit

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: cognee/cognee-mcp
          tags: |
            type=ref,event=branch
            type=sha,prefix={{branch}}-
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          file: cognee-mcp/Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=registry,ref=cognee/cognee-mcp:buildcache
          cache-to: type=registry,ref=cognee/cognee-mcp:buildcache,mode=max


      - name: Image digest
        run: echo ${{ steps.build.outputs.digest }}
