# syntax=docker/dockerfile:1.7
#
# Single-shot build image for the Astro site. The production deployment uses
# nginx:1.27-alpine with a bind mount to apps/web/dist (see docker-compose.yml),
# so the webhook can rewrite the site without rebuilding any image.
#
# You only need this Dockerfile if you want a self-contained static site
# image (e.g. for a registry). For the normal homeserver flow, this image
# is never built.
#
# We install pnpm globally via npm to avoid corepack's strict version
# negotiation across Node 20 minor versions.

FROM node:20-alpine AS base
RUN npm install -g pnpm@latest
WORKDIR /repo

FROM base AS deps
COPY pnpm-workspace.yaml package.json ./
COPY apps/web/package.json ./apps/web/package.json
COPY apps/cms/package.json ./apps/cms/package.json
COPY pnpm-lock.yaml* ./
RUN if [ -f pnpm-lock.yaml ]; then \
      pnpm install --frozen-lockfile; \
    else \
      echo "No pnpm-lock.yaml found; running pnpm install (generates one)"; \
      pnpm install; \
    fi

FROM deps AS build
ENV PUBLIC_PAYLOAD_API=https://cms.softvowels.example
ENV PUBLIC_SITE_URL=https://softvowels.example
ENV PAYLOAD_API_INTERNAL=http://cms:3000
COPY apps/web ./apps/web
WORKDIR /repo/apps/web
RUN pnpm run build

FROM nginx:1.27-alpine AS runtime
COPY --from=build /repo/apps/web/dist /usr/share/nginx/html
COPY apps/web/nginx-default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
