Skip to content

Commit

Permalink
build(site): Nouvelle image Docker mieux adaptée à la poduction (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnoux authored and elisfainstein committed Oct 8, 2024
1 parent 9f24b6e commit a080e47
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules
dist
.git
.env*
.nx
.vscode
.github
4 changes: 2 additions & 2 deletions .github/workflows/cd-site.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Déploiement à la demande du site
name: Déploiement du site
run-name: Déploiement du site sur ${{ github.event.inputs.target }}

on:
Expand Down Expand Up @@ -32,7 +32,7 @@ jobs:
earthly
--use-inline-cache --save-inline-cache --push
--platform=linux/amd64
+site-build
+site-docker
--ENV_NAME=${{ github.event.inputs.target }}
--ANON_KEY=${{ secrets.ANON_KEY }} --API_URL=${{ secrets.API_URL }}
--STRAPI_KEY=${{ secrets.STRAPI_KEY }} --STRAPI_URL=${{ secrets.STRAPI_URL }}
Expand Down
102 changes: 62 additions & 40 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,43 @@ business-parse:
SAVE ARTIFACT /content AS LOCAL ./data_layer/content
SAVE ARTIFACT /content AS LOCAL $BUSINESS_DIR/tests/data/dl_content

node-alpine:
# Pinning the docker image version to node:20.15.1-alpine
# because of existing memory leaks from using the fetch() API in node 20.16.0
# https://www.reddit.com/r/node/comments/1ejzn64/sudden_inexplicable_memory_leak_on_new_builds/
FROM node:20.15.1-alpine

# 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
WORKDIR /app

node-alpine-with-prod-deps:
FROM +node-alpine

COPY pnpm-lock.yaml ./

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable pnpm

RUN pnpm fetch --prod

COPY package.json ./
RUN pnpm install -r --offline --prod

node-alpine-with-all-deps:
FROM +node-alpine-with-prod-deps

RUN pnpm install --frozen-lockfile

COPY *.json ./
COPY jest.* ./
COPY vitest.* ./

# Copy shared libraries
COPY $API_DIR $API_DIR
COPY $UI_DIR $UI_DIR


# construit l'image de base pour les images utilisant node
node-fr:
Expand All @@ -272,27 +309,38 @@ node-fr:
# même que celle sur laquelle le build est fait
ARG PLATFORM=$TARGETPLATFORM
FROM --platform=$PLATFORM node:20-slim

# locale FR pour que les tests e2e relatifs au formatage localisés des dates et des valeurs numériques puissent passer
ENV LANG fr_FR.UTF-8
RUN apt-get update && apt-get install -y locales dumb-init && rm -rf /var/lib/apt/lists/* && locale-gen "fr_FR.UTF-8"

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN corepack enable pnpm

WORKDIR /app

prod-deps:
FROM +node-fr

# See https://pnpm.io/cli/fetch
COPY pnpm-lock.yaml ./
RUN pnpm fetch --prod

COPY package.json ./
RUN pnpm install -r --offline --prod

# construit l'image contenant les dépendances des modules front
front-deps:
FROM +node-fr
FROM +prod-deps

COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

COPY *.json ./
COPY jest.* ./
COPY vitest.* ./

# Only copy libraries
# Copy only shared libraries
COPY $API_DIR $API_DIR
COPY $UI_DIR $UI_DIR

Expand Down Expand Up @@ -444,37 +492,16 @@ panier-run: ## construit et lance l'image du panier en local
--publish 3001:80 \
$PANIER_IMG_NAME

site-build: ## construit l'image du site
ARG PLATFORM
ARG --required ANON_KEY
ARG --required API_URL
ARG --required STRAPI_KEY
ARG --required STRAPI_URL
ARG POSTHOG_HOST
ARG POSTHOG_KEY
ARG AXEPTIO_ID
ARG CRISP_WEBSITE_ID
ARG vars
FROM +front-deps
ENV NEXT_PUBLIC_STRAPI_KEY=$STRAPI_KEY
ENV NEXT_PUBLIC_STRAPI_URL=$STRAPI_URL
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$ANON_KEY
ENV NEXT_PUBLIC_SUPABASE_URL=$API_URL
ENV NEXT_PUBLIC_POSTHOG_HOST=$POSTHOG_HOST
ENV NEXT_PUBLIC_POSTHOG_KEY=$POSTHOG_KEY
ENV NEXT_PUBLIC_AXEPTIO_ID=$AXEPTIO_ID
ENV NEXT_PUBLIC_CRISP_WEBSITE_ID=$CRISP_WEBSITE_ID
ENV NEXT_TELEMETRY_DISABLED=1
ENV PUBLIC_PATH="/app/packages/site/public"
ENV PORT=80
EXPOSE $PORT
# copie les sources des modules à construire
COPY $SITE_DIR $SITE_DIR
COPY $UI_DIR $UI_DIR
COPY $API_DIR $API_DIR
RUN pnpm run build:site
CMD ["dumb-init", "./node_modules/.bin/next", "start", "./packages/site/"]
SAVE IMAGE --cache-from=$SITE_IMG_NAME --push $SITE_IMG_NAME


site-docker:
BUILD --pass-args ./packages/site+docker

site-deploy:
ARG --required KOYEB_API_KEY
BUILD --pass-args ./packages/site+deploy



site-run: ## construit et lance l'image du site en local
ARG network=supabase_network_tet
Expand Down Expand Up @@ -915,11 +942,6 @@ koyeb:
COPY +koyeb-bin/koyeb ./
RUN echo "token: $KOYEB_API_KEY" > ~/.koyeb.yaml

site-deploy:
ARG --required KOYEB_API_KEY
FROM +koyeb
RUN ./koyeb services update $ENV_NAME-site/front --docker $SITE_IMG_NAME

auth-deploy:
ARG --required KOYEB_API_KEY
FROM +koyeb
Expand Down
12 changes: 2 additions & 10 deletions backend/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ build:
FROM ../+front-deps

COPY . ./backend
# COPY ../packages/api ./packages/api
# COPY --chown=node:node root+$BACKEND_DIR ./
# COPY --chown=node:node ../packages/api/. $API_DIR

RUN pnpm build:backend
RUN pnpm prune --prod # Remove packages from devDependencies


SAVE ARTIFACT dist /dist AS LOCAL dist
SAVE ARTIFACT node_modules /node_modules AS LOCAL node_modules
SAVE ARTIFACT dist /dist AS LOCAL earthly-build/dist

docker:
FROM ../+node-fr
FROM ../+prod-deps

ARG DOCKER_IMAGE=$BACKEND_IMG_NAME

Expand All @@ -28,9 +22,7 @@ docker:

EXPOSE ${PORT}

# COPY --chown=node:node +backend-pre-build/app/package*.json .
COPY +build/dist ./dist
COPY +build/node_modules ./node_modules

CMD ["node", "dist/backend/main.js"]
SAVE IMAGE --push $DOCKER_IMAGE
Expand Down
1 change: 1 addition & 0 deletions packages/site/.env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NEXT_PUBLIC_SUPABASE_URL=http://localhost:8000
NEXT_PUBLIC_SUPABASE_ANON_KEY=${SUPABASE_ANON_KEY}
NEXT_PUBLIC_CRISP_WEBSITE_ID="// Crisp integration (warning: use different ID by deployment env.)"
NEXT_PUBLIC_AXEPTIO_ID=${AXEPTIO_ID}
91 changes: 91 additions & 0 deletions packages/site/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
VERSION 0.8

build:
FROM ../../+node-alpine-with-all-deps

ARG --required ANON_KEY
ARG --required API_URL
ARG --required STRAPI_KEY
ARG --required STRAPI_URL
ARG --required AXEPTIO_ID

COPY . ./packages/site

# Disable telemetry during build
ENV NEXT_TELEMETRY_DISABLED=1

ENV NEXT_PUBLIC_STRAPI_KEY=$STRAPI_KEY
ENV NEXT_PUBLIC_STRAPI_URL=$STRAPI_URL
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$ANON_KEY
ENV NEXT_PUBLIC_SUPABASE_URL=$API_URL
ENV NEXT_PUBLIC_POSTHOG_HOST=$POSTHOG_HOST
ENV NEXT_PUBLIC_POSTHOG_KEY=$POSTHOG_KEY
ENV NEXT_PUBLIC_AXEPTIO_ID=$AXEPTIO_ID
ENV NEXT_PUBLIC_CRISP_WEBSITE_ID=$CRISP_WEBSITE_ID

RUN pnpm build:site

SAVE ARTIFACT ./packages/site/.next
SAVE ARTIFACT ./packages/site/public

# See official Dockerfile at https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
docker:
FROM ../../+node-alpine

ARG --required ANON_KEY
ARG --required API_URL
ARG --required STRAPI_KEY
ARG --required STRAPI_URL
ARG --required AXEPTIO_ID
ARG POSTHOG_HOST
ARG POSTHOG_KEY

ENV NEXT_PUBLIC_STRAPI_KEY=$STRAPI_KEY
ENV NEXT_PUBLIC_STRAPI_URL=$STRAPI_URL
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$ANON_KEY
ENV NEXT_PUBLIC_SUPABASE_URL=$API_URL
ENV NEXT_PUBLIC_POSTHOG_HOST=$POSTHOG_HOST
ENV NEXT_PUBLIC_POSTHOG_KEY=$POSTHOG_KEY
ENV NEXT_PUBLIC_AXEPTIO_ID=$AXEPTIO_ID
ENV NEXT_PUBLIC_CRISP_WEBSITE_ID=$CRISP_WEBSITE_ID

ARG DOCKER_IMAGE=$SITE_IMG_NAME

# ENV PUBLIC_PATH="/app/public"

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY +build/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce docker image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --chown=nextjs:nodejs +build/.next/standalone ./
COPY --chown=nextjs:nodejs +build/.next/static ./packages/site/.next/static

USER nextjs


# Prepare the app for production
ENV NEXT_TELEMETRY_DISABLED=1
ENV HOSTNAME="0.0.0.0"
ENV NODE_ENV production

ENV PORT=3000
EXPOSE 3000

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "packages/site/server.js"]

SAVE IMAGE --push $DOCKER_IMAGE

deploy:
ARG --required KOYEB_API_KEY
FROM ../../+koyeb --KOYEB_API_KEY=$KOYEB_API_KEY
RUN ./koyeb services update $ENV_NAME-site/front --docker $SITE_IMG_NAME

5 changes: 4 additions & 1 deletion packages/site/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const nextConfig = {
svgr: false,
},

// Useful for self-hosting in a Docker container
// See https://nextjs.org/docs/app/api-reference/next-config-js/output#automatically-copying-traced-files
output: 'standalone',

// active le mode strict pour détecter le problèmes en dev
reactStrictMode: true,
// active la minification
Expand All @@ -23,7 +27,6 @@ const nextConfig = {
esmExternals: 'loose',
},
images: {
unoptimized: true,
remotePatterns: [
{ protocol: 'https', hostname: '**.strapiapp.com' },
{ protocol: 'http', hostname: '127.0.0.1' },
Expand Down

0 comments on commit a080e47

Please sign in to comment.