name: Build & Push Docker Image

on:
  workflow_call:
    inputs:
      registry:
        required: true
        type: string
      image-name:
        required: true
        type: string
      dockerfile-path:
        required: false
        type: string
        default: 'Dockerfile'
      build-context:
        required: false
        type: string
        default: '.'
    outputs:
      image-tag:
        value: ${{{{ jobs.docker.outputs.tag }}}}

jobs:
  docker:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{{{ steps.meta.outputs.tags }}}}
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Registry
        uses: docker/login-action@v3
        with:
          registry: ${{{{ inputs.registry }}}}
          username: ${{{{ secrets.REGISTRY_USERNAME }}}}
          password: ${{{{ secrets.REGISTRY_PASSWORD }}}}

      - name: Docker metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{{{ inputs.registry }}}}/${{{{ inputs.image-name }}}}
          tags: |
            type=ref,event=branch
            type=sha,prefix=${{{{ inputs.image-name }}}}-

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: ${{{{ inputs.build-context }}}}
          file: ${{{{ inputs.dockerfile-path }}}}
          push: true
          tags: ${{{{ steps.meta.outputs.tags }}}}
          cache-from: type=gha
          cache-to: type=gha,mode=max
