name: Build and Push Docker Image

on:
  push:
    branches:
      - main
    paths-ignore:
      - '.github/patch_android.py'
      - '.github/workflows/release.yml'
      - 'package.json'
      - 'README.md'
      - 'README_zh.md'
  workflow_dispatch:

# 添加权限声明，确保可以推送到 GHCR
permissions:
  packages: write
  contents: read

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        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: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Lowercase the repo name
        run: echo "REPO_LOWER=${GITHUB_REPOSITORY_OWNER,,}" >> ${GITHUB_ENV}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ghcr.io/${{ env.REPO_LOWER }}/embyx:latest
            ${{ secrets.DOCKER_USER }}/embyx:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
