name: Build and Push Container Image

"on":
  workflow_call:
    inputs:
      registry:
        type: string
        required: false
        default: ghcr.io
        description: The container registry FQDN.
      image_name:
        type: string
        required: true
        description: The suffix for the image name, without the registry and without the repository path.
      context:
        type: string
        required: true
        description: The path to the context to start `docker build` into.
      target:
        type: string
        required: false
        default: ""
        description: The Dockerfile target stage to build the image for.

# see https://docs.github.com/en/enterprise-cloud@latest/actions/how-tos/use-cases-and-examples/publishing-packages/publishing-docker-images#publishing-images-to-github-packages
jobs:
  docker-build-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      attestations: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ inputs.registry }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ inputs.registry }}/${{ github.repository }}-${{ inputs.image_name }}
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
      - name: Build and push Docker image
        id: push
        uses: docker/build-push-action@v6
        with:
          context: ${{ inputs.context }}
          target: ${{ inputs.target }}
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      - name: Generate artifact attestation
        uses: actions/attest-build-provenance@v2
        with:
          subject-name: ${{ inputs.registry }}/${{ github.repository }}-${{ inputs.image_name }}
          subject-digest: ${{ steps.push.outputs.digest }}
          push-to-registry: true
      - name: Delete all untagged container images
        uses: actions/delete-package-versions@v5
        with:
          package-name: messages-${{ inputs.image_name }}
          package-type: 'container'
          min-versions-to-keep: 0
          delete-only-untagged-versions: true
      - name: Container images retention
        uses: actions/delete-package-versions@v5
        with:
          package-name: messages-${{ inputs.image_name }}
          package-type: 'container'
          min-versions-to-keep: 30
          ignore-versions: '^(latest|main|\\d+\\.\\d+(\\.\\d+)?)$'
