Skip to content

Commit

Permalink
build(site): Nouvelle image Docker adaptée pour la production
Browse files Browse the repository at this point in the history
  • Loading branch information
farnoux committed Oct 5, 2024
1 parent 5d352b8 commit ea13cb9
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 38 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
80 changes: 44 additions & 36 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,40 @@ business-parse:
SAVE ARTIFACT /content AS LOCAL ./data_layer/content
SAVE ARTIFACT /content AS LOCAL $BUSINESS_DIR/tests/data/dl_content

node-alpine:
FROM node:20-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 Down Expand Up @@ -455,37 +489,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 @@ -926,11 +939,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
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
RUN ./koyeb services update $ENV_NAME-site/front --docker $SITE_IMG_NAME

4 changes: 4 additions & 0 deletions 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 Down

0 comments on commit ea13cb9

Please sign in to comment.