name: Docker build and publish

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

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Bun
        run: curl -fsSL https://bun.sh/install | bash

      - name: Install dependencies
        run: ~/.bun/bin/bun install --frozen-lockfile

      - name: Run tests (gate)
        run: ~/.bun/bin/bun test --recursive

      - name: Extract version from tag
        id: version
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

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

      - name: Log in to Gitea container registry
        uses: docker/login-action@v3
        with:
          registry: gt.zyon.no
          username: Stian
          password: ${{ secrets.GITEA_PUBLISH_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: packages/shade-server/Dockerfile
          push: true
          tags: |
            gt.zyon.no/stian/shade-prekey:${{ steps.version.outputs.version }}
            gt.zyon.no/stian/shade-prekey:latest
          labels: |
            org.opencontainers.image.version=${{ steps.version.outputs.version }}
            org.opencontainers.image.source=https://gt.zyon.no/Stian/Shade
            org.opencontainers.image.revision=${{ github.sha }}
