name: Publish multiarch postgres-operator images on ghcr.io

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
  IMAGE_NAME_UI: ${{ github.repository }}-ui

on:
  push:
    tags:
      - '*'

jobs:
  publish:
    name: Build, test and push image
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - uses: actions/setup-go@v2
        with:
          go-version: "^1.22.5"

      - name: Run unit tests
        run: make deps mocks test

      - name: Define image name
        id: image
        run: |
            OPERATOR_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF/refs\/tags\//}"
            echo "OPERATOR_IMAGE=$OPERATOR_IMAGE" >> $GITHUB_OUTPUT

      - name: Define UI image name
        id: image_ui
        run: |
            UI_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME_UI }}:${GITHUB_REF/refs\/tags\//}"
            echo "UI_IMAGE=$UI_IMAGE" >> $GITHUB_OUTPUT

      - name: Define logical backup image name
        id: image_lb
        run: |
            BACKUP_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/logical-backup:${GITHUB_REF_NAME}"
            echo "BACKUP_IMAGE=$BACKUP_IMAGE" >> $GITHUB_OUTPUT

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to GHCR
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push multiarch operator image to ghcr
        uses: docker/build-push-action@v3
        with:
          context: .
          file: docker/Dockerfile
          push: true
          build-args: BASE_IMAGE=alpine:3
          tags: "${{ steps.image.outputs.OPERATOR_IMAGE }}"
          platforms: linux/amd64,linux/arm64

      - name: Build and push multiarch ui image to ghcr
        uses: docker/build-push-action@v3
        with:
          context: ui
          push: true
          build-args: BASE_IMAGE=python:3.11-slim
          tags: "${{ steps.image_ui.outputs.UI_IMAGE }}"
          platforms: linux/amd64,linux/arm64

      - name: Build and push multiarch logical-backup image to ghcr
        uses: docker/build-push-action@v3
        with:
          context: logical-backup
          push: true
          build-args: BASE_IMAGE=ubuntu:22.04
          tags: "${{ steps.image_lb.outputs.BACKUP_IMAGE }}"
          platforms: linux/amd64,linux/arm64
