From 6ea9b83565d9e4deb9a562a2cb8bc004ff1bf763 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Thu, 11 Apr 2024 16:36:08 -0300 Subject: [PATCH] improve dockerfile, refactor turbo tasks --- .dockerignore | 5 +-- packages/components/package.json | 2 +- packages/components/turbo.json | 1 - packages/components/vercel.json | 2 +- packages/hub/dockerfile | 62 +++++++++++++++++++------------- packages/hub/package.json | 2 ++ packages/hub/turbo.json | 4 +++ packages/ui/package.json | 2 +- packages/ui/vercel.json | 2 +- pnpm-lock.yaml | 3 ++ turbo.json | 10 +++--- 11 files changed, 57 insertions(+), 38 deletions(-) diff --git a/.dockerignore b/.dockerignore index e35505895f..663c7f2c6c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -33,9 +33,6 @@ node_modules *.tsbuildinfo -# Ignore dist directory in the current directory -dist - # Ignore .env files .env .env.test @@ -49,4 +46,4 @@ dist # Ignore .vscode directory in the current directory /.vscode -/.git \ No newline at end of file +/.git diff --git a/packages/components/package.json b/packages/components/package.json index 1449287c51..e70df49364 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -105,8 +105,8 @@ "build:ts": "tsc -b", "build:css": "postcss ./src/styles/full.css -o ./dist/full.css && postcss ./src/styles/common.css -o ./dist/common.css", "build:lezer": "cd ./src/components/CodeEditor/languageSupport; mkdir -p generated; lezer-generator ./squiggle.grammar --output generated/squiggle.ts", + "build": "pnpm run build:lezer && pnpm run build:ts && pnpm run build:css", "build:storybook": "storybook build", - "build": "pnpm run build:lezer && pnpm run build:ts && pnpm run build:css && pnpm run build:storybook", "lint": "pnpm lint:prettier && pnpm eslint", "lint:prettier": "prettier --check .", "eslint": "eslint --ignore-path .gitignore .", diff --git a/packages/components/turbo.json b/packages/components/turbo.json index b18a99e79d..176fee97b4 100644 --- a/packages/components/turbo.json +++ b/packages/components/turbo.json @@ -4,7 +4,6 @@ "build": { "outputs": [ "dist/**", - "storybook-static/**", "src/components/CodeEditor/languageSupport/generated/**" ] }, diff --git a/packages/components/vercel.json b/packages/components/vercel.json index 9ff10a49c8..3945cd75c0 100644 --- a/packages/components/vercel.json +++ b/packages/components/vercel.json @@ -1,5 +1,5 @@ { - "buildCommand": "PLATFORM=vercel npx turbo build", + "buildCommand": "PLATFORM=vercel npx turbo build:storybook", "outputDirectory": "storybook-static", "ignoreCommand": "npx turbo-ignore || ../../skip-dependabot.sh" } diff --git a/packages/hub/dockerfile b/packages/hub/dockerfile index ad92e68403..83f2bb821b 100644 --- a/packages/hub/dockerfile +++ b/packages/hub/dockerfile @@ -1,35 +1,49 @@ -# Use the official Node.js 20 image as the base -FROM node:20 -WORKDIR /app +# Should be invoked from monorepo's root. -# Copy the entire monorepo to the working directory +# Use the official Node.js 20 image as the base +ARG DOCKER_NODE_VERSION=20-bookworm-slim + +FROM node:$DOCKER_NODE_VERSION AS node-with-openssl + +# Install OpenSSL - necessary for Prisma client +# Mount caches via https://stackoverflow.com/a/72851168 +RUN --mount=type=cache,id=apt-lists,target=/var/lib/apt/lists,sharing=locked \ + --mount=type=cache,id=apt-cache,target=/var/cache/apt,sharing=locked \ + rm -f /etc/apt/apt.conf.d/docker-clean \ + && apt-get update \ + && apt-get install -y \ + openssl +# If we ever need canvas for components, uncomment these: +# build-essential \ +# libcairo2-dev \ +# libpango1.0-dev \ +# libjpeg-dev \ +# libgif-dev \ +# librsvg2-dev + +# Build +FROM node-with-openssl AS build-stage +WORKDIR /build -RUN apt-get update && apt-get install -y apt -# Install necessary dependencies for canvas -RUN apt-get update && \ - apt-get install -y \ - build-essential \ - libcairo2-dev \ - libpango1.0-dev \ - libjpeg-dev \ - libgif-dev \ - librsvg2-dev +# Install pnpm and turbo globally +RUN npm install -g pnpm +# Copy the entire monorepo to the working directory COPY . . -# Install pnpm and turbo globally -RUN npm install -g pnpm turbo # Install monorepo dependencies -RUN pnpm install +RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store/v3 pnpm install --frozen-lockfile -# run turbo build +WORKDIR /build/packages/hub +ENV NODE_ENV=production -# WORKDIR /app/packages/hub +# TODO - turbo cache, either as volume or remote +RUN npx turbo run bundle --concurrency=1 -# RUN pnpm install +FROM node-with-openssl AS run-stage +WORKDIR /app -# Expose the port on which the application will run (adjust if necessary) -# EXPOSE 3001 +COPY --from=build-stage /build/packages/hub/dist /app/dist +COPY --from=build-stage /build/node_modules/.pnpm/@prisma+client*/node_modules/.prisma/client/*.so.node /app/dist -# Set the command to run the application -# CMD ["pnpm", "run", "build-last-revision"] \ No newline at end of file +CMD ["node", "dist/bundle/buildRecentModelRevision.js"] diff --git a/packages/hub/package.json b/packages/hub/package.json index c9b478d235..f04964f5f4 100644 --- a/packages/hub/package.json +++ b/packages/hub/package.json @@ -16,6 +16,7 @@ "gen:schema": "tsx ./src/graphql/print-schema.ts", "gen": "pnpm gen:prisma && pnpm gen:schema && pnpm gen:relay", "build:ts": "pnpm gen && tsc", + "bundle": "esbuild ./src/scripts/buildRecentModelRevision.ts --format=cjs --platform=node --sourcemap --minify --bundle --outdir=./dist/bundle", "build": "pnpm gen && __NEXT_PRIVATE_PREBUNDLED_REACT=next next build", "lint": "prettier --check . && next lint", "format": "prettier --write .", @@ -80,6 +81,7 @@ "@types/relay-runtime": "^14.1.23", "babel-plugin-relay": "^16.2.0", "dotenv-cli": "^7.3.0", + "esbuild": "^0.20.1", "eslint": "^8.57.0", "eslint-config-next": "^14.1.0", "graphql": "^16.8.1", diff --git a/packages/hub/turbo.json b/packages/hub/turbo.json index 390cd7deca..ec40ced34c 100644 --- a/packages/hub/turbo.json +++ b/packages/hub/turbo.json @@ -3,6 +3,10 @@ "pipeline": { "build": { "env": ["DATABASE_URL", "VERCEL_ENV"] + }, + "bundle": { + "dependsOn": ["^build", "build:ts"], + "outputs": ["dist/bundle*"] } } } diff --git a/packages/ui/package.json b/packages/ui/package.json index d6e68c4cfc..a193bd8d98 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -16,7 +16,7 @@ "build:css": "postcss ./src/styles/full.css -o ./dist/full.css", "build:storybook": "storybook build", "clean:ts": "rm -rf ./dist ./tsconfig.tsbuildinfo", - "build": "pnpm clean:ts && pnpm build:ts && pnpm build:css && pnpm build:storybook", + "build": "pnpm clean:ts && pnpm build:ts && pnpm build:css", "lint": "pnpm lint:prettier && pnpm eslint", "lint:prettier": "prettier --check .", "eslint": "eslint --ignore-path .gitignore ./src", diff --git a/packages/ui/vercel.json b/packages/ui/vercel.json index 9ff10a49c8..3945cd75c0 100644 --- a/packages/ui/vercel.json +++ b/packages/ui/vercel.json @@ -1,5 +1,5 @@ { - "buildCommand": "PLATFORM=vercel npx turbo build", + "buildCommand": "PLATFORM=vercel npx turbo build:storybook", "outputDirectory": "storybook-static", "ignoreCommand": "npx turbo-ignore || ../../skip-dependabot.sh" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dc92749f84..4794385981 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -454,6 +454,9 @@ importers: dotenv-cli: specifier: ^7.3.0 version: 7.3.0 + esbuild: + specifier: ^0.20.1 + version: 0.20.1 eslint: specifier: ^8.57.0 version: 8.57.0 diff --git a/turbo.json b/turbo.json index d4b03285c7..ab97587872 100644 --- a/turbo.json +++ b/turbo.json @@ -6,7 +6,7 @@ "pipeline": { // dev target for pre-push checks "build:ts": { - "dependsOn": ["^build:ts"], + "dependsOn": ["^build"], "outputs": ["dist/**", "*.tsbuildinfo"] }, "build": { @@ -19,15 +19,15 @@ "!.next/cache/**" ] }, + "build:storybook": { + "dependsOn": ["build"], + "outputs": ["storybook-static/**"] + }, "lint": {}, "eslint": {}, "test": { "dependsOn": ["build"] }, - "bundle": { - "dependsOn": ["build"], - "outputs": ["dist/bundle*"] - }, "coverage": { "dependsOn": ["build"], "outputs": ["coverage/**"]