name: Publish the package when a tag is pushed

on:
  push:
    tags:
      - 'v*.*.*' # Trigger only for tags starting with 'v' (e.g., v0.1.9)
env:
  REPO_NAME: marcua/ayb
  REGISTRY: ghcr.io

jobs:
  docker-image:
    name: Build and push Docker image
    runs-on: ubuntu-latest
    env:
      platform: "linux/amd64"
    steps:
    - name: Check out code
      uses: actions/checkout@v6

    - name: Docker meta
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.REPO_NAME }}
        tags: |
          type=semver,pattern={{version}}
          type=raw,value=latest,enable=${{ !contains(github.ref_name, '-') }}
          type=sha,prefix=,format=short

    - 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: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Build and push Docker image
      uses: docker/build-push-action@v6
      with:
        # TODO(marcua): List more platforms here to get `linux-arm64` built: https://github.com/marcua/ayb/issues/523
        platforms: ${{ env.platform }}
        labels: ${{ steps.meta.outputs.labels }}
        tags: ${{ steps.meta.outputs.tags }}
        push: true

    - name: Display Docker image size
      run: |
        echo "Docker image size:"
        docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.REPO_NAME }}:${{ github.ref_name }} | grep -E 'Digest|Size'
