From fca5b837171925325b7345b328d2b269cdac7cac Mon Sep 17 00:00:00 2001 From: David Moore Date: Thu, 11 Jul 2024 16:31:50 +1000 Subject: [PATCH 1/4] add docs for monorepos --- src/pages/faq.mdx | 4 ++ src/pages/reference/custom-containers.mdx | 70 +++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/pages/faq.mdx b/src/pages/faq.mdx index 6e941b4f4..c90d96883 100644 --- a/src/pages/faq.mdx +++ b/src/pages/faq.mdx @@ -33,6 +33,10 @@ Add the environment variable to your `~/.zshrc` or `~/.bashrc` as: PULUMI_ACCESS_TOKEN=access_token ``` +## Does Nitric support monorepos? + +Yes, Nitric supports monorepos through the custom runtime feature, which allows you to change the build context of your Docker build. For more information, see [custom containers](/reference/custom-containers). Alternatively, you can move your `nitric.yaml` to the root of your repository. + ## Will I be locked-in to Nitric? Nitric is designed with flexibility to avoid lock-in, including to Nitric. If the framework no longer serves you, you'll simply need to choose a new IaC and migrate your provisioning code. The Nitric framework and CLI are written in Go, and use the Pulumi Go Providers, so you may be able to avoid rewriting all of the provisioning code by lifting the provisioning code which Nitric has already built for you. If relevant, you'll also need to rebuild your CI pipelines to leverage the new IaC tooling you've chosen. Nitric doesn't have access to your data, so no data migration is needed. diff --git a/src/pages/reference/custom-containers.mdx b/src/pages/reference/custom-containers.mdx index fe2d034f6..42bf478d7 100644 --- a/src/pages/reference/custom-containers.mdx +++ b/src/pages/reference/custom-containers.mdx @@ -176,3 +176,73 @@ ENTRYPOINT ["/bin/main"] ### Create an ignore file Custom dockerfile templates also support co-located dockerignore files. If your custom docker template is at path `./docker/node.dockerfile` you can create an ignore file at `./docker/node.dockerfile.dockerignore`. + +## Create a monorepo with custom runtimes + +Nitric supports monorepos via the custom runtime feature, this allows you to change the build context of your docker build. To use a custom runtime in a monorepo, you can specify the runtime key per service as shown below. + +Available in Nitric CLI version 1.45.0 and above + +### Example for Turborepo + +[Turborepo](https://turbo.build/) is a monorepo tool that allows you to manage multiple packages in a single repository. In this example, we will use a custom runtime to build a service in a monorepo using a custom dockerfile. + +```yaml {{ tag: "root/backends/guestbook-app/nitric.yaml" }} +name: guestbook-app +services: + - match: services/*.ts + runtime: turbo + type: '' + start: npm run dev:services $SERVICE_PATH +runtimes: + turbo: + dockerfile: ./turbo.dockerfile # the custom dockerfile + context: ../../ # the context of the docker build + args: + TURBO_SCOPE: 'guestbook-api' +``` + +```docker {{ tag: "root/backends/guestbook-app/turbo.dockerfile" }} +FROM node:alpine AS builder +ARG TURBO_SCOPE + +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +RUN apk update +# Set working directory +WORKDIR /app +RUN yarn global add turbo + +# copy from root of the mono-repo +COPY . . +RUN turbo prune --scope=${TURBO_SCOPE} --docker + +# Add lockfile and package.json's of isolated subworkspace +FROM node:alpine AS installer +ARG TURBO_SCOPE +ARG HANDLER +RUN apk add --no-cache libc6-compat +RUN apk update +WORKDIR /app +RUN yarn global add typescript @vercel/ncc turbo + +# First install dependencies (as they change less often) +COPY .gitignore .gitignore +COPY --from=builder /app/out/json/ . +COPY --from=builder /app/out/yarn.lock ./yarn.lock +RUN yarn install --frozen-lockfile --production + +# Build the project and its dependencies +COPY --from=builder /app/out/full/ . +COPY turbo.json turbo.json + +RUN turbo run build --filter=${TURBO_SCOPE} -- ./${HANDLER} -m --v8-cache -o lib/ + +FROM node:alpine AS runner +ARG TURBO_SCOPE +WORKDIR /app + +COPY --from=installer /app/backends/${TURBO_SCOPE}/lib . + +ENTRYPOINT ["node", "index.js"] +``` From 3e10e230a992698b05296fa02b153c5ac8ac660a Mon Sep 17 00:00:00 2001 From: David Moore Date: Thu, 11 Jul 2024 16:39:05 +1000 Subject: [PATCH 2/4] fix spellcheck --- dictionary.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dictionary.txt b/dictionary.txt index cfbd0407a..cb2e58185 100644 --- a/dictionary.txt +++ b/dictionary.txt @@ -196,6 +196,7 @@ todo todos transpiling ARN +monorepos ^.+[-:_]\w+$ [a-z]+([A-Z0-9]|[A-Z0-9]\w+) From 437e3f702a3a413ec40496b90e5de81ca2ca77b8 Mon Sep 17 00:00:00 2001 From: David Moore <4121492+davemooreuws@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:04:56 +1000 Subject: [PATCH 3/4] Update src/pages/reference/custom-containers.mdx Co-authored-by: Ryan Cartwright <39504851+HomelessDinosaur@users.noreply.github.com> --- src/pages/reference/custom-containers.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/reference/custom-containers.mdx b/src/pages/reference/custom-containers.mdx index 42bf478d7..362074ca2 100644 --- a/src/pages/reference/custom-containers.mdx +++ b/src/pages/reference/custom-containers.mdx @@ -185,7 +185,7 @@ Nitric supports monorepos via the custom runtime feature, this allows you to cha ### Example for Turborepo -[Turborepo](https://turbo.build/) is a monorepo tool that allows you to manage multiple packages in a single repository. In this example, we will use a custom runtime to build a service in a monorepo using a custom dockerfile. +[Turborepo](https://turbo.build/) is a monorepo tool for JavaScript and TypeScript that allows you to manage multiple packages in a single repository. In this example, we will use a custom runtime to build a service in a monorepo using a custom dockerfile. ```yaml {{ tag: "root/backends/guestbook-app/nitric.yaml" }} name: guestbook-app From e9ccfc08717625d25a3bd4ea432847f3092e53a1 Mon Sep 17 00:00:00 2001 From: David Moore <4121492+davemooreuws@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:05:10 +1000 Subject: [PATCH 4/4] Update src/pages/reference/custom-containers.mdx Co-authored-by: Jye Cusch --- src/pages/reference/custom-containers.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/reference/custom-containers.mdx b/src/pages/reference/custom-containers.mdx index 362074ca2..41fd48110 100644 --- a/src/pages/reference/custom-containers.mdx +++ b/src/pages/reference/custom-containers.mdx @@ -179,7 +179,7 @@ Custom dockerfile templates also support co-located dockerignore files. If your ## Create a monorepo with custom runtimes -Nitric supports monorepos via the custom runtime feature, this allows you to change the build context of your docker build. To use a custom runtime in a monorepo, you can specify the runtime key per service as shown below. +Nitric supports monorepos via the custom runtime feature, this allows you to change the build context of your docker build. To use a custom runtime in a monorepo, you can specify the `runtime` key per service definition as shown below. Available in Nitric CLI version 1.45.0 and above