name: Reusable Image Builder

on:
  workflow_call:
    inputs:
      dockerfile:
        required: true
        type: string
      build-json:
        required: true
        type: string
      build-args:
        required: false
        default: ""
        type: string

concurrency:
  group: ${{ github.workflow }}-${{ fromJSON(inputs.build-json).name }}-${{ fromJSON(inputs.build-json).version }}
  cancel-in-progress: false

jobs:
  build-image:
    name: Build ${{ fromJSON(inputs.build-json).name }} @ ${{ fromJSON(inputs.build-json).version }}
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Login to Github Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Build ${{ fromJSON(inputs.build-json).name }}
        uses: docker/build-push-action@v3
        with:
          context: .
          file: ${{ inputs.dockerfile }}
          tags: ${{ fromJSON(inputs.build-json).image_tag }}
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          build-args: ${{ inputs.build-args }}
          push: true
          cache-from: type=registry,ref=${{ fromJSON(inputs.build-json).cache_tag }}
          cache-to: type=registry,mode=max,ref=${{ fromJSON(inputs.build-json).cache_tag }}
