name: Publish Docker image

on:
  push:
    tags:
      - 'OFDLV*'

jobs:
  docker:
    name: Build and push Docker image to Gitea Registry
    runs-on: ubuntu-latest

    steps:
      - name: Extract tag name and version
        id: version
        run: |
          FULL_REF="${{ gitea.ref }}"
          TAG="${FULL_REF##refs/tags/}"
          VERSION="${TAG#OFDLV}"
          echo "Tag: $TAG"
          echo "Version: $VERSION"
          echo "tag=$TAG" >> "$GITEA_OUTPUT"
          echo "version=$VERSION" >> "$GITEA_OUTPUT"

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

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

      - name: Log in to Gitea Docker Registry
        uses: docker/login-action@v3
        with:
          registry: git.ofdl.tools
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            git.ofdl.tools/sim0n00ps/of-dl:latest
            git.ofdl.tools/sim0n00ps/of-dl:${{ steps.version.outputs.version }}
          build-args: |
            VERSION=${{ steps.version.outputs.version }}