name: 自动构建并发布 Docker 镜像

on:
  release:
    types: [published]

permissions:
  contents: read
  packages: write
  attestations: write
  id-token: write

env:
  REGISTRY: ghcr.io
  DOCKER_REPO: ${{ secrets.DOCKERHUB_USERNAME }}/xhs-downloader
  GHCR_REPO: ghcr.io/${{ secrets.DOCKERHUB_USERNAME }}/xhs-downloader

jobs:
  publish-docker:
    runs-on: ubuntu-latest

    steps:
      - name: 拉取源码
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: 设置 QEMU
        uses: docker/setup-qemu-action@v3

      - name: 设置 Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: 登录到 Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: 登录到 GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: 构建和推送 Docker 镜像到 Docker Hub 和 GHCR
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          push: true
          tags: |
            ${{ env.DOCKER_REPO }}:${{ github.event.release.tag_name }}
            ${{ env.DOCKER_REPO }}:latest
            ${{ env.GHCR_REPO }}:${{ github.event.release.tag_name }}
            ${{ env.GHCR_REPO }}:latest
          provenance: false
          sbom: false
