name: Build and Push Docker Image

on:
  push:
    branches: [ "main" ]
  workflow_dispatch:

env:
  IMAGE_NAME: gitea.umbra.mom/jay/football-competitiveness

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Login to Gitea Registry
        env:
          REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
          REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
        run: |
          echo "$REGISTRY_TOKEN" | docker login gitea.umbra.mom \
            -u "$REGISTRY_USER" \
            --password-stdin

      - name: Set up buildx
        run: |
          docker buildx create --name ci-builder --driver docker-container --use || docker buildx use ci-builder
          docker buildx inspect --bootstrap

      - name: Build and push image
        run: |
          docker buildx build \
            --pull \
            --platform linux/amd64 \
            -t "${IMAGE_NAME}:latest" \
            --cache-from "type=registry,ref=${IMAGE_NAME}:buildcache" \
            --cache-to "type=registry,ref=${IMAGE_NAME}:buildcache,mode=max" \
            --push \
            .
