name: Build KAMOS images

on:
  push:
    branches: [main]
    paths-ignore:
      - "README.md"
  schedule:
    # Weekly rebuild so KAMOS picks up upstream Bazzite stable updates
    - cron: "0 6 * * 2"
  workflow_dispatch:

env:
  REGISTRY_HOST: fcs.tgsad.ae

jobs:
  build:
    name: Build ${{ matrix.image_name }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - image_name: kamos
            base_image: ghcr.io/ublue-os/bazzite
          - image_name: kamos-nvidia
            base_image: ghcr.io/ublue-os/bazzite-nvidia

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

      - name: Set image ref
        run: |
          echo "IMAGE=${REGISTRY_HOST}/${GITHUB_REPOSITORY_OWNER,,}/${{ matrix.image_name }}" >> "$GITHUB_ENV"
          echo "DATE_TAG=$(date +%Y%m%d)" >> "$GITHUB_ENV"

      - name: Build image (uses host Docker via mounted socket)
        run: |
          docker build \
            -f Containerfile \
            --build-arg BASE_IMAGE="${{ matrix.base_image }}" \
            --build-arg BASE_TAG=stable \
            -t "${IMAGE}:stable" \
            -t "${IMAGE}:latest" \
            -t "${IMAGE}:${DATE_TAG}" \
            .

      - name: Push to Forgejo container registry
        run: |
          echo "${{ secrets.REGISTRY_TOKEN }}" | \
            docker login "${REGISTRY_HOST}" -u "${{ secrets.REGISTRY_USER }}" --password-stdin
          for tag in stable latest "${DATE_TAG}"; do
            docker push "${IMAGE}:${tag}"
          done

      - name: Clean up build cache (keep server disk healthy)
        if: always()
        run: docker image prune -f
