# ---- Build ----
FROM node:20-alpine AS build
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci
COPY . .
RUN npm run build

# ---- Production ----
FROM node:20-alpine AS runner
# git is needed for the Publish page (commit + push of the mounted checkout).
RUN apk add --no-cache git
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV HOSTNAME=0.0.0.0
ENV PORT=3051
ENV REPO_DIR=/repo
# The container may run as an arbitrary host uid (see docker-compose.yml)
# with no home directory; git needs a writable HOME.
ENV HOME=/tmp

COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/scripts ./scripts
RUN chmod 755 scripts/git-askpass.sh

# Overridden by `user:` in docker-compose.yml to match the checkout's owner.
USER node
EXPOSE 3051

CMD ["node", "server.js"]
