name: Build and push Docker image
description: Build and push Docker image to DockerHub and GitHub Container Registry
inputs:
  image_name:
    description: Name of the image to build
    required: true
  push_image:
    description: Whether to push the image to the registry (true/false)
    required: true
  directory:
    description: The directory containing the Dockerfile
    required: true
  tags:
    description: Comma separated tags to apply to the image
    required: true
  platforms:
    description: Platforms to build the image for (e.g. linux/amd64,linux/arm64)
    required: true
  description:
    description: Short description of the image
    required: true
runs:
  using: composite
  steps:
      - name: Login to DockerHub
        if: ${{ inputs.push_image == 'true' }}
        uses: docker/login-action@v3
        with:
          username: ${{ env.DOCKERHUB_USER }}
          password: ${{ env.DOCKERHUB_PW }}

      - name: Login to GitHub Container Registry
        if: ${{ inputs.push_image == 'true' }}
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ env.GITHUB_TOKEN }}

      - uses: docker/setup-qemu-action@v3
      - uses: docker/setup-buildx-action@v3

      - uses: docker/metadata-action@v5
        id: meta
        with:
          images: ${{ inputs.image_name }}
          tags: ${{ inputs.tags }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: ${{ inputs.directory }}
          platforms: ${{ inputs.platforms }}
          push: ${{ inputs.push_image == 'true' }}
          tags: ${{ steps.meta.outputs.tags }}

      - name: Update DockerHub description
        if: ${{ inputs.push_image == 'true' }}
        uses: peter-evans/dockerhub-description@v4
        with:
          username: ${{ env.DOCKERHUB_USER }}
          password: ${{ env.DOCKERHUB_PW }}
          repository: ${{ inputs.image_name }}
          short-description: ${{ inputs.description }}
          readme-filepath: ${{ inputs.directory }}/README.md

