name: Docker Publish

on:
  push:
    tags:
      - 'v*'
    branches:
      - nightly
  workflow_dispatch:

jobs:
  build-push:
    runs-on: self-hosted
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          clean: true

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

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

      - name: Login to Gitea registry
        uses: docker/login-action@v3
        with:
          registry: gitea.it-drui.de
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Derive tags
        id: meta
        run: |
          REF="${GITHUB_REF}"
          if [[ "$REF" == refs/tags/v* ]]; then
            VER=$(cat VERSION)
            TAGS="gitea.it-drui.de/viewit/kx-bridge:latest"
            TAGS="$TAGS,gitea.it-drui.de/viewit/kx-bridge:$VER"
          else
            # Push auf nightly-Branch oder workflow_dispatch
            TAGS="gitea.it-drui.de/viewit/kx-bridge:nightly"
          fi
          echo "tags=$TAGS" >>"$GITHUB_OUTPUT"
          echo "Pushing tags: $TAGS"

      - name: Build & push (amd64 + arm64)
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          provenance: false
          no-cache: true
          tags: ${{ steps.meta.outputs.tags }}
