name: Build and Push Docker Image

on:
  push:
    branches:
      - main
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Log in to Gitea Container Registry
        run: |
          echo "${{ secrets.CR_TOKEN }}" | \
          docker login 127.0.0.1:3333 -u ${{ github.actor }} --password-stdin

      - name: Build and push
        run: |
          IMAGE=127.0.0.1:3333/stardream/streamhall
          PUBLIC=git.stdm.moe/stardream/streamhall
          REF="${{ github.ref }}"

          if [[ "$REF" == refs/tags/* ]]; then
            VERSION="${REF#refs/tags/}"
            docker build -t "${IMAGE}:${VERSION}" -t "${IMAGE}:latest" .
            docker push "${IMAGE}:${VERSION}"
            docker push "${IMAGE}:latest"
          else
            docker build -t "${IMAGE}:latest" .
            docker push "${IMAGE}:latest"
          fi
