name: Build Containers

on:
  workflow_call:
    inputs:
      tag:
        required: true
        type: string
      tags:
        required: false
        type: string
      ref:
        required: false
        type: string
    secrets:
      DOCKERHUB_USERNAME:
        required: true
      DOCKERHUB_TOKEN:
        required: true

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          ref: ${{ inputs.ref || github.sha }}

      - name: Log in to the Container registry (ghcr.io)
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Log in to the Container registry (dockerhub)
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - uses: depot/setup-action@v1

      - name: Generate Docker metadata
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: |
            hkotel/mealie
            ghcr.io/${{ github.repository }}
          # Overwrite the image.version label with our tag
          labels: |
            org.opencontainers.image.version=${{ inputs.tag }}
            org.opencontainers.image.revision=${{ inputs.ref || github.sha }}

      - name: Retrieve Python package
        uses: actions/download-artifact@v6
        with:
          name: backend-dist
          path: dist

      - name: Build and push Docker image, via Depot.dev
        uses: depot/build-push-action@v1
        with:
          project: srzjb6mhzm
          file: ./docker/Dockerfile
          context: .
          build-contexts: |
            packages=dist
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            hkotel/mealie:${{ inputs.tag }}
            ghcr.io/${{ github.repository }}:${{ inputs.tag }}
            ${{ inputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            COMMIT=${{ inputs.ref || github.sha }}
