perf: streamline cached production builds

This commit is contained in:
2026-09-16 10:20:14 +03:30
parent f45efdc667
commit b90161e504
2 changed files with 14 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ FROM image-build AS dependencies
WORKDIR /app
ARG NPM_REGISTRY=https://package-mirror.liara.ir/repository/npm/
ARG NPM_REGISTRY=https://registry.npmjs.org/
ENV NEXT_TELEMETRY_DISABLED=1 \
PKG_CONFIG_PATH=/opt/vips/lib/pkgconfig \
LD_LIBRARY_PATH=/opt/vips/lib \
@@ -40,21 +40,21 @@ RUN --mount=type=cache,id=portfolio-npm-node22,target=/root/.npm,sharing=locked
RUN if [ "$(uname -m)" = x86_64 ]; then export CFLAGS='-O2 -march=x86-64 -mtune=generic' CXXFLAGS='-O2 -march=x86-64 -mtune=generic'; fi \
&& npm explore sharp -- npm run build
COPY scripts/check-image-runtime.mjs ./scripts/check-image-runtime.mjs
RUN node scripts/check-image-runtime.mjs --require-source
FROM dependencies AS verification
FROM dependencies AS build
COPY . .
RUN npm run lint
RUN npm run typecheck
# Only application inputs invalidate compilation. Tests and docs stay outside.
COPY next.config.mjs tsconfig.json next-env.d.ts ./
COPY src ./src
COPY public ./public
COPY migrations ./migrations
COPY scripts/maintenance.ts scripts/build-maintenance.mjs scripts/prepare-standalone.mjs ./scripts/
RUN --mount=type=cache,id=portfolio-next-node22,target=/app/.next/cache,sharing=locked \
npm run build
RUN npm test
# Carry the exact shared-library dependency closure from the working build stage.
# Development packages can provide libraries that are absent from image-runtime.
FROM verification AS native-runtime-libraries
FROM dependencies AS native-runtime-libraries
RUN mkdir -p /runtime-libs \
&& (ldd /app/node_modules/sharp/src/build/Release/*.node /opt/vips/lib/libvips*.so* 2>/dev/null || true) \
| awk '/=> \// { print $3 } /^[[:space:]]*\// { print $1 }' \
@@ -77,15 +77,11 @@ ENV NODE_ENV=production \
RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs
COPY --from=verification --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=verification --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=verification --chown=nextjs:nodejs /app/public ./public
COPY --from=build --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=image-build /opt/vips/lib /opt/vips/lib
# Preserve the package and its native dependency closure beyond Next's tracing.
COPY --from=dependencies --chown=nextjs:nodejs /app/node_modules/sharp/ ./node_modules/sharp/
COPY --from=native-runtime-libraries /runtime-libs/ /
COPY scripts/check-image-runtime.mjs ./scripts/check-image-runtime.mjs
RUN node scripts/check-image-runtime.mjs --require-source
RUN mkdir -p .next/cache /data && chown -R nextjs:nodejs .next/cache /data
USER nextjs