name: Build & Push Image

on:
  push:
    branches: [main]
    paths:
      - "code/src/**"
      - "code/proto/**"
      - "code/config.yaml"
      - "code/pyproject.toml"
      - "code/Dockerfile"
    workflow_dispatch: 

jobs:
  build-push:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up QEMU (for cross-arch builds)
        uses: docker/setup-qemu-action@v3

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

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

      - name: Build and push multi-arch image
        uses: docker/build-push-action@v5
        with:
          context: code
          file: code/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            cindyncl26/fsl-client:latest
            cindyncl26/fsl-client:sha-${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
