name: Build & Push Docker Image

on:
  push:
    branches:
      - main
      - develop-calendar
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      # Set to 'true' if you need to compile native modules from source (e.g. better-sqlite3 on arm)
      BUILD_FROM_SOURCE: 'false'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Extract registry host
        id: registry
        run: echo "host=$(echo ${{ github.server_url }} | sed 's|https\?://||')" >> "$GITHUB_OUTPUT"

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

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

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ steps.registry.outputs.host }}
          username: ${{ github.repository_owner }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Docker metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ steps.registry.outputs.host }}/${{ github.repository }}
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha,prefix=
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          # Build multi-arch images for amd64 and arm64
          platforms: linux/amd64,linux/arm64
          push: true
          build-args: |
            BUILD_FROM_SOURCE=${{ env.BUILD_FROM_SOURCE }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
