name: Build and publish image

on:
  workflow_call:
    inputs:
      tags:
        required: true
        type: string
      platforms:
        required: true
        type: string
      build-args:
        required: true
        type: string
      context:
        required: true
        type: string
      file:
        required: false
        type: string
        default: Dockerfile
    secrets:
      token:
        required: true

jobs:
  build_push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      with:
        persist-credentials: false
    - name: Set up QEMU
      uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0
    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
      with:
        # Images after this version (>=v0.10) are incompatible with gcr and aws.
        version: v0.9.1  # https://github.com/docker/buildx/issues/1533
    - name: Login to Docker
      run: docker login -u publisher -p ${{ secrets.token }} ghcr.io
    - name: Docker Build
      uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
      with:
        push: true
        tags: ${{ inputs.tags }}
        platforms: ${{ inputs.platforms }}
        build-args: ${{ inputs.build-args }}
        context: ${{ inputs.context }}
        file: ${{ inputs.context }}/${{ inputs.file }}
