FROM node:20-alpine AS base
WORKDIR /repo
# Pin pnpm 9 to match pnpm-lock.yaml (lockfileVersion 9.0).
RUN corepack enable && corepack prepare pnpm@9.15.9 --activate

FROM base AS deps
COPY pnpm-lock.yaml pnpm-workspace.yaml package.json ./
COPY apps/api/package.json apps/api/package.json
COPY apps/web/package.json apps/web/package.json
COPY packages/database/package.json packages/database/package.json
# node-linker=hoisted -> single flat /repo/node_modules copied into runtime.
RUN pnpm install --frozen-lockfile --config.node-linker=hoisted

FROM deps AS build
COPY apps/web apps/web
RUN pnpm --filter @jorgecuadros/web build

FROM node:20-alpine AS runtime
WORKDIR /repo
ENV NODE_ENV=production
COPY --from=build /repo/node_modules node_modules
COPY --from=build /repo/apps/web apps/web

# Build/version metadata baked in at image build time (see .gitea/workflows/build.yml).
ARG APP_VERSION=dev
ARG GIT_SHA=unknown
ARG BUILD_DATE=unknown
ENV APP_VERSION=$APP_VERSION \
    GIT_SHA=$GIT_SHA \
    BUILD_DATE=$BUILD_DATE

EXPOSE 3000
WORKDIR /repo/apps/web
CMD ["npx", "next", "start"]
