From 99c33729831e70742d707ffad30be622075cebb6 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Fri, 19 May 2023 16:45:41 +1000 Subject: [PATCH] re-optimize node builds post prisma fixes --- go.mod | 2 +- pkg/run/function.go | 1 + pkg/runtime/generate_test.go | 39 +++++++++++++++++-------------- pkg/runtime/typescript.dockerfile | 39 +++++++++++++++++-------------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index fb170cc75..67158489d 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( github.com/docker/distribution v2.7.1+incompatible github.com/nitrictech/nitric/cloud/common v0.0.0-20230509232736-48f59463930d github.com/olahol/melody v1.1.3 + github.com/samber/lo v1.38.1 github.com/seaweedfs/seaweedfs v0.0.0-20230109022022-51d4a4b28d0e go.etcd.io/bbolt v1.3.6 google.golang.org/protobuf v1.30.0 @@ -319,7 +320,6 @@ require ( github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect github.com/ryancurrah/gomodguard v1.3.0 // indirect github.com/ryanrolds/sqlclosecheck v0.4.0 // indirect - github.com/samber/lo v1.38.1 // indirect github.com/sanposhiho/wastedassign/v2 v2.0.7 // indirect github.com/sashamelentyev/interfacebloat v1.1.0 // indirect github.com/sashamelentyev/usestdlibvars v1.23.0 // indirect diff --git a/pkg/run/function.go b/pkg/run/function.go index 435767578..0c07cc6dc 100644 --- a/pkg/run/function.go +++ b/pkg/run/function.go @@ -23,6 +23,7 @@ import ( "time" "github.com/docker/docker/api/types/container" + "github.com/nitrictech/cli/pkg/containerengine" "github.com/nitrictech/cli/pkg/project" "github.com/nitrictech/cli/pkg/runtime" diff --git a/pkg/runtime/generate_test.go b/pkg/runtime/generate_test.go index 672b21d03..5338c109f 100644 --- a/pkg/runtime/generate_test.go +++ b/pkg/runtime/generate_test.go @@ -37,27 +37,30 @@ FROM node:alpine as build ARG HANDLER -WORKDIR /usr/app - # Python and make are required by certain native package build processes in NPM packages. -ENV PYTHONUNBUFFERED=1 -RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python -RUN python3 -m ensurepip -RUN pip3 install --no-cache --upgrade pip setuptools +RUN apk add g++ make py3-pip RUN yarn global add typescript @vercel/ncc -COPY . . +WORKDIR /usr/app -RUN yarn import || echo Lockfile already exists +COPY package.json *.lock *-lock.json / -RUN set -ex; yarn install --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache; +RUN yarn import || echo "" + +RUN set -ex && \ + yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \ + rm -rf /tmp/.cache RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json +COPY . . + # make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \ # TODO: remove when custom dockerfile support is available -RUN ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client +RUN \ + --mount=type=cache,target=/tmp/ncc-cache \ + ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client -t FROM node:alpine as final @@ -67,17 +70,19 @@ RUN apk update && \ apk add --no-cache ca-certificates && \ update-ca-certificates -COPY --from=build /usr/app/lib/ ./lib/ +COPY package.json *.lock *-lock.json ./ + +RUN set -ex && \ + yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \ + rm -rf /tmp/.cache COPY . . -RUN set -ex; \ - yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \ - rm -rf /tmp/.cache; \ - # prisma fix for docker installs: https://github.com/prisma/docs/issues/4365 - # TODO: remove when custom dockerfile support is available - test -d ./prisma && npx prisma generate || echo ""; +COPY --from=build /usr/app/lib/ ./lib/ +# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365 +# TODO: remove when custom dockerfile support is available +RUN test -d ./prisma && npx prisma generate || echo ""; ENTRYPOINT ["node", "lib/index.js"]`, }, diff --git a/pkg/runtime/typescript.dockerfile b/pkg/runtime/typescript.dockerfile index 9c690ac67..6d2ca4df2 100644 --- a/pkg/runtime/typescript.dockerfile +++ b/pkg/runtime/typescript.dockerfile @@ -3,27 +3,30 @@ FROM node:alpine as build ARG HANDLER -WORKDIR /usr/app - # Python and make are required by certain native package build processes in NPM packages. -ENV PYTHONUNBUFFERED=1 -RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python -RUN python3 -m ensurepip -RUN pip3 install --no-cache --upgrade pip setuptools +RUN apk add g++ make py3-pip RUN yarn global add typescript @vercel/ncc -COPY . . +WORKDIR /usr/app + +COPY package.json *.lock *-lock.json / -RUN yarn import || echo Lockfile already exists +RUN yarn import || echo "" -RUN set -ex; yarn install --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache; +RUN set -ex && \ + yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \ + rm -rf /tmp/.cache RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json +COPY . . + # make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \ # TODO: remove when custom dockerfile support is available -RUN ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client +RUN \ + --mount=type=cache,target=/tmp/ncc-cache \ + ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client -t FROM node:alpine as final @@ -33,16 +36,18 @@ RUN apk update && \ apk add --no-cache ca-certificates && \ update-ca-certificates -COPY --from=build /usr/app/lib/ ./lib/ +COPY package.json *.lock *-lock.json ./ + +RUN set -ex && \ + yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \ + rm -rf /tmp/.cache COPY . . -RUN set -ex; \ - yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \ - rm -rf /tmp/.cache; \ - # prisma fix for docker installs: https://github.com/prisma/docs/issues/4365 - # TODO: remove when custom dockerfile support is available - test -d ./prisma && npx prisma generate || echo ""; +COPY --from=build /usr/app/lib/ ./lib/ +# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365 +# TODO: remove when custom dockerfile support is available +RUN test -d ./prisma && npx prisma generate || echo ""; ENTRYPOINT ["node", "lib/index.js"] \ No newline at end of file