name: release-plz

permissions:
  contents: write
  pull-requests: write
  packages: write

on:
  push:
    branches:
      - main

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: s2-streamstore/cachey

jobs:
  release-plz:
    runs-on: ubuntu-latest
    outputs:
      releases_created: ${{ steps.release-plz.outputs.releases_created }}
      version: ${{ steps.release-plz.outputs.releases_created == 'true' && fromJSON(steps.release-plz.outputs.releases)[0].version || '' }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Run release-plz
        id: release-plz
        uses: release-plz/action@1528104d2ca23787631a1c1f022abb64b34c1e11 # v0.5.128
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  build-images:
    name: Build ${{ matrix.arch }} image
    needs: release-plz
    if: needs.release-plz.outputs.releases_created == 'true'
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86-64
            runner: ubuntu-latest
          - arch: arm64
            runner: ubuntu-24.04-arm
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ needs.release-plz.outputs.version }}

      - name: Log in to Container Registry
        uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build image
        run: |
          docker build \
            --target runtime \
            -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release-plz.outputs.version }}-${{ matrix.arch }} \
            -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-${{ matrix.arch }} \
            .

      - name: Push images
        run: |
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release-plz.outputs.version }}-${{ matrix.arch }}
          docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-${{ matrix.arch }}

  create-manifest:
    name: Create multi-arch manifests
    needs: [release-plz, build-images]
    runs-on: ubuntu-latest
    steps:
      - name: Log in to Container Registry
        uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Create and push versioned manifest
        run: |
          docker buildx imagetools create -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release-plz.outputs.version }} \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release-plz.outputs.version }}-x86-64 \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release-plz.outputs.version }}-arm64

      - name: Create and push latest manifest
        run: |
          docker buildx imagetools create -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-x86-64 \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest-arm64
