name: Build and Push Docker Image on Tag

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Extract tag name
        id: tag
        run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Set up QEMU (for multi-platform)
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver: docker-container
          bootstrap: true

      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.GITEA_REGISTRY }}
          username: ${{ secrets.GITEA_USERNAME }}
          password: ${{ secrets.GITEA_PASSWORD }}

      - name: Build and Push multi-platform image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ${{ secrets.GITEA_REGISTRY }}/renolation/english-toeic:${{ steps.tag.outputs.TAG }}
            ${{ secrets.GITEA_REGISTRY }}/renolation/english-toeic:latest