name: Build Docker images

on:
  push:
    branches:
      - "**"
    tags:
      - "v*"

permissions:
  contents: read
  packages: write

concurrency:
  group: docker-images-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Hub
            image: beszel
            dockerfile: ./internal/dockerfile_hub
            platforms: linux/amd64,linux/arm64,linux/arm/v7
            tags: |
              type=raw,value=latest,enable={{is_default_branch}}
              type=raw,value=edge,enable={{is_default_branch}}
              type=ref,event=branch
              type=sha,prefix=sha-
              type=semver,pattern={{version}}
              type=semver,pattern={{major}}.{{minor}}
              type=semver,pattern={{major}}

    steps:
      - name: Check out code
        uses: actions/checkout@v4

      - name: Normalize GHCR owner
        id: ghcr
        run: echo "owner=${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2

      - name: Install frontend dependencies
        run: bun install --frozen-lockfile --cwd ./internal/site
        shell: bash

      - name: Build frontend
        run: bun run --cwd ./internal/site build
        shell: bash

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Sign in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}

      - name: Docker metadata
        id: metadata
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ steps.ghcr.outputs.owner }}/${{ matrix.image }}
          tags: ${{ matrix.tags }}

      - name: Build and publish
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ${{ matrix.dockerfile }}
          platforms: ${{ matrix.platforms }}
          push: true
          tags: ${{ steps.metadata.outputs.tags }}
          labels: ${{ steps.metadata.outputs.labels }}
