name: Docker Image CI on merge to master

# 触发器：当一个从 dev 指向 master 的 Pull Request 被关闭并合并时触发
on:
  pull_request:
    types: [closed] # 当PR被关闭时触发
    branches: [ "master" ] # 目标分支是 master

jobs:
  build-and-push:
    # 条件：仅当PR被合并（而不是仅仅关闭）
    if: github.event.pull_request.merged == true
    
    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout master branch code (with dev merged)
      # 当PR合并后，默认会检出 master 分支的最新代码
      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 GitHub Container Registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Build and push multi-arch Docker images
      uses: docker/build-push-action@v5
      with:
        context: .
        file: ./Dockerfile
        platforms: linux/amd64,linux/arm64
        push: true
        tags: |
          ghcr.io/${{ github.repository_owner }}/ai-goofish:latest
          ghcr.io/${{ github.repository_owner }}/ai-goofish:${{ github.sha }}
