name: Publish Docker image to Gitea

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

jobs:
  build-docker:
    name: Build and push to Gitea Registry
    runs-on: ubuntu-latest
    permissions:
      packages: write  # 必须：允许写入软件包
      contents: read
      
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

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

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

      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          # 修改点1: 这里填你的 Gitea 域名
          registry: git.chgr.cc
          # 修改点2: 使用触发 Action 的 Gitea 用户名
          username: ${{ github.actor }}
          # 修改点3: 直接使用 Gitea 内置的 Token，无需额外配置 Secret！
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          # 修改点4: 这里的格式是 "域名/用户名/仓库名"
          # 注意：Gitea 镜像名通常要求全小写
          images: git.chgr.cc/${{ github.repository }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          platforms: |
            linux/amd64
            linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}