name: Build and Push Multi-Arch Docker Image

on:
  push:
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v2
        with:
          registry: ${{ secrets.DOMAIN }}
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Extract Version
        working-directory: ./server/cinema
        run: |
          VERSION=$(grep 'version:' pubspec.yaml | sed 's/version: //')
          echo "VERSION=$VERSION" >> $GITHUB_ENV
          echo "MAJOR=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_ENV
          echo "MAJOR_MINOR=$(echo $VERSION | cut -d. -f1,2)" >> $GITHUB_ENV

      - name: Build and Push Multi-Arch Image
        working-directory: ./server/cinema
        run: |
          docker buildx build \
            --platform linux/amd64,linux/arm64 \
            -t ${{ secrets.DOMAIN }}/${{ secrets.OWNER }}/${{ secrets.REPO }}/server:latest \
            -t ${{ secrets.DOMAIN }}/${{ secrets.OWNER }}/${{ secrets.REPO }}/server:${{ env.MAJOR }} \
            -t ${{ secrets.DOMAIN }}/${{ secrets.OWNER }}/${{ secrets.REPO }}/server:${{ env.MAJOR_MINOR }} \
            -t ${{ secrets.DOMAIN }}/${{ secrets.OWNER }}/${{ secrets.REPO }}/server:${{ env.VERSION }} \
            --push .
