name: Release
on:
  push:
    tags:
      - "*"

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: true

permissions:
  actions: write # Allow canceling in-progress runs
  contents: read # Read access to the repository
  packages: write # Write access to the container registry
  id-token: write # For the attest action to push
  attestations: write # For the attest action to push

jobs:
  docker:
    name: Docker Release
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - target: final
            tag_suffix: ''
          - target: debug-shell
            tag_suffix: '-shell'

    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}}
          flavor: |
            latest=auto
            suffix=${{ matrix.tag_suffix }},onlatest=true

      - 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 ghcr.io
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
        uses: docker/build-push-action@v6
        id: push
        with:
          context: .
          file: ./Dockerfile
          target: ${{ matrix.target }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64, linux/arm64
          cache-from: type=gha
          cache-to: type=gha,mode=max
          build-args: |
            IMAGE_TAG=ghcr.io/${{ github.repository }}:${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
      - name: Attestation Provenance for ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
        uses: actions/attest-build-provenance@v2
        id: attest
        with:
          subject-name: ghcr.io/${{ github.repository }}
          subject-digest: ${{ steps.push.outputs.digest }}
          push-to-registry: true
