on:
  push:
    branches:
      - 'forgejo'
    tags:
      - '*-git-annex*'

jobs:
  build-oci-image:
    runs-on: docker
    strategy:
      matrix:
        type: ["rootful", "rootless"]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the full history so that the Forgejo version is determined properly
      - name: Determine registry and username
        id: determine-registry-and-username
        run: |
          echo "registry=${GITHUB_SERVER_URL#https://}" >> "$GITHUB_OUTPUT"
          echo "username=${GITHUB_REPOSITORY%/*}" >> "$GITHUB_OUTPUT"
      - name: Install Docker
        run: curl -fsSL https://get.docker.com | sh
      - name: Set up QEMU
        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:
          registry: ${{ steps.determine-registry-and-username.outputs.registry }}
          username: ${{ steps.determine-registry-and-username.outputs.username }}
          password: ${{ secrets.REGISTRY_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ${{ (matrix.type == 'rootful' && 'Dockerfile') || (matrix.type == 'rootless' && 'Dockerfile.rootless') }}
          push: true
          tags: ${{ steps.determine-registry-and-username.outputs.registry }}/${{ github.repository }}:${{ github.ref_name }}${{ (matrix.type == 'rootful' && ' ') || (matrix.type == 'rootless' && '-rootless') }}
