name: Postgres image
on:
  push:
    paths:
      - postgres/**
    branches:
      - main
    tags:
      - "v*"

env:
  MINOR_VERSIONS: '{"15": "10","17": "2"}'

jobs:
  build:
    strategy:
      matrix:
        arch:
          - arm64
          - amd64
        postgres_major:
          - "15"
          - "17"
    runs-on: ubuntu-latest-${{ matrix.arch }}
    steps:
      - uses: actions/checkout@v4

      - name: Login to container registry
        uses: docker/login-action@v3
        with:
          registry: code.icb4dc0.de
          username: prskr
          password: ${{ secrets.RELEASE_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          file: postgres/Dockerfile
          push: true
          tags: code.icb4dc0.de/prskr/supabase-operator/postgres:${{ matrix.postgres_major }}.${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }}.${{ github.run_number }}-${{ matrix.arch }}
          build-args: |
            POSTGRES_MAJOR=${{ matrix.postgres_major }}
            POSTGRES_MINOR=${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }}
          cache-from: type=registry,ref=code.icb4dc0.de/prskr/supabase-operator/postgres:buildcache
          cache-to: type=registry,ref=code.icb4dc0.de/prskr/supabase-operator/postgres:buildcache,mode=max

  manifest:
    strategy:
      matrix:
        postgres_major:
          - "15"
          - "17"
    runs-on: ubuntu-latest
    needs:
      - build
    steps:
      - name: Login to container registry
        uses: docker/login-action@v3
        with:
          registry: code.icb4dc0.de
          username: prskr
          password: ${{ secrets.RELEASE_TOKEN }}

      - name: Create manifest
        run: |
          docker buildx imagetools create \
              -t code.icb4dc0.de/prskr/supabase-operator/postgres:${{ matrix.postgres_major }}.${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }} \
              -t code.icb4dc0.de/prskr/supabase-operator/postgres:${{ matrix.postgres_major }}.${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }}.${{ github.run_number }} \
              code.icb4dc0.de/prskr/supabase-operator/postgres:${{ matrix.postgres_major }}.${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }}.${{ github.run_number }}-arm64 \
              code.icb4dc0.de/prskr/supabase-operator/postgres:${{ matrix.postgres_major }}.${{ fromJSON(env.MINOR_VERSIONS)[matrix.postgres_major] }}.${{ github.run_number }}-amd64