name: Build Docker Images (PR)

on:
  pull_request:
    branches:
      - main
      - develop

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read

    strategy:
      matrix:
        include:
          - service: web
            dockerfile: docker/web/Dockerfile
            context: .
          - service: caddy
            dockerfile: docker/caddy/Dockerfile
            context: .
          - service: l4-port-manager
            dockerfile: docker/l4-port-manager/Dockerfile
            context: .

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

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

      - name: Extract metadata (tags, labels)
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.service }}
          tags: |
            type=ref,event=pr

      - name: Build Docker image (no push)
        id: build
        uses: docker/build-push-action@v7
        with:
          context: ${{ matrix.context }}
          file: ${{ matrix.dockerfile }}
          push: false
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          platforms: linux/amd64,linux/arm64
