name: Build and publish images

on:
  push:
    branches: [main]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAMESPACE: ${{ github.repository_owner }}
  # Run JS-based actions on Node 24 — Node 20 is deprecated on GH-hosted
  # runners (forced switch 2026-06-02, removal 2026-09-16). Opt in early
  # so the warning goes away and we don't get caught flat-footed.
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: 'true'

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    strategy:
      fail-fast: false
      matrix:
        image:
          - name: familynido-api
            dockerfile: deploy/api.Dockerfile
          - name: familynido-web
            dockerfile: deploy/web.Dockerfile

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

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

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Compute image metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAMESPACE }}/${{ matrix.image.name }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=sha,format=short

      - name: Build and push ${{ matrix.image.name }}
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ${{ matrix.image.dockerfile }}
          push: true
          platforms: linux/amd64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha,scope=${{ matrix.image.name }}
          cache-to: type=gha,mode=max,scope=${{ matrix.image.name }}
