Skip to content

Commit

Permalink
FIX: Test build phase of migrations in docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
koechkevin committed Jan 17, 2024
1 parent 636df83 commit 6fc4642
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 7 deletions.
108 changes: 108 additions & 0 deletions Dockerfile.dev.charterafrica
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
FROM node:18-alpine as node-alpine

# Always install security updated e.g. https://pythonspeed.com/articles/security-updates-in-docker/
# Update local cache so that other stages don't need to update cache
RUN apk update \
&& apk upgrade


FROM node-alpine as base

# 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

ARG PNPM_VERSION=8.5.0

RUN corepack enable && corepack prepare pnpm@${PNPM_VERSION} --activate

WORKDIR /workspace

COPY pnpm-lock.yaml .

RUN pnpm fetch


FROM base as builder


WORKDIR /workspace

COPY *.yaml *.json ./
COPY packages ./packages
COPY apps/charterafrica ./apps/charterafrica

# Use virtual store: https://pnpm.io/cli/fetch#usage-scenario
RUN pnpm install --recursive --offline --frozen-lockfile

# NOTE: ARG values are only available **after** ARG statement & hence we need
# to separate NEXT_PUBLIC_APP_URL and PAYLOAD_PUBLIC_APP_URL into
# multiple ARG statements so that PAYLOAD can use the value defined
# in NEXT.
ARG NEXT_TELEMETRY_DISABLED=1 \
# Needed by Next.js at build time
NEXT_PUBLIC_APP_NAME=charterAFRICA \
NEXT_PUBLIC_APP_URL=http://localhost:3000 \
NEXT_PUBLIC_GA_MEASUREMENT_ID="" \
NEXT_PUBLIC_SENTRY_DSN="" \
NEXT_PUBLIC_SEO_DISABLED="true" \
# Needed by Next.js and server.ts at build time
PORT=3000 \
# Sentry config for source maps upload (needed at build time only)
SENTRY_AUTH_TOKEN="" \
SENTRY_ENV="" \
SENTRY_ORG="" \
SENTRY_PROJECT="" \
# Needed by Payload at Next.js build time (see below for Payload's own
# build time vars)
MONGO_URL \
# When building Next.js, point to the local Payload instance
PAYLOAD_PUBLIC_APP_URL=http://localhost:3000 \
PAYLOAD_SECRET_KEY
RUN cd apps/charterafrica && npx payload migrate && cd ../../
RUN pnpm build-next --filter=charterafrica

# Needed by Payload at Payload build time
# When building Payload, set its public URL (same as Next.js)
ENV PAYLOAD_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL}

RUN pnpm build-payload --filter=charterafrica

# TODO(kilemensi): Optimize and improve this default runner
# i) Start with node-alpine or base and only copy/install
# what's needed in prod
# ii) Add user:group for security (nextjs:node)
# iii) ?
FROM builder as runner

# Remember to remove local cache from runner
RUN rm -rf /var/cache/apk/*

# We've already set defaults values for most of these ARGs; don't repeat
ARG NEXT_TELEMETRY_DISABLED \
NEXT_PUBLIC_APP_NAME \
NEXT_PUBLIC_APP_URL \
NEXT_PUBLIC_GA_MEASUREMENT_ID \
NEXT_PUBLIC_SENTRY_DSN \
NEXT_PUBLIC_SEO_DISABLED \
PAYLOAD_CONFIG_PATH="dist/payload.config.js" \
PAYLOAD_PUBLIC_APP_URL \
PORT \
SENTRY_ENV

ENV NODE_ENV=production \
NEXT_PUBLIC_APP_NAME=${NEXT_PUBLIC_APP_NAME} \
NEXT_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL} \
NEXT_PUBLIC_GA_MEASUREMENT_ID=${NEXT_PUBLIC_GA_MEASUREMENT_ID} \
NEXT_PUBLIC_SENTRY_DSN=${NEXT_PUBLIC_SENTRY_DSN} \
NEXT_PUBLIC_SEO_DISABLED=${NEXT_PUBLIC_SEO_DISABLED} \
NEXT_TELEMETRY_DISABLED=${NEXT_TELEMETRY_DISABLED} \
PAYLOAD_CONFIG_PATH=${PAYLOAD_CONFIG_PATH} \
PAYLOAD_PUBLIC_APP_URL=${PAYLOAD_PUBLIC_APP_URL} \
PORT=${PORT} \
SENTRY_ENV=${SENTRY_ENV}

WORKDIR /workspace/apps/charterafrica

EXPOSE ${PORT}

CMD [ "node", "dist/server.js" ]
14 changes: 7 additions & 7 deletions apps/charterafrica/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default buildConfig({
editor: slateEditor({}),
db: mongooseAdapter({
url: process.env.MONGO_URL,
migrationDir: "./src/migrations",
migrationDir: "./migrations",
}),
collections: [
Authors,
Expand Down Expand Up @@ -111,12 +111,12 @@ export default buildConfig({
] as GlobalConfig[],
...(locales?.length
? {
localization: {
locales,
defaultLocale,
fallback: true,
},
}
localization: {
locales,
defaultLocale,
fallback: true,
},
}
: undefined),

admin: {
Expand Down
19 changes: 19 additions & 0 deletions charterafrica-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3.8"
services:
charterafrica:
build:
context: .
dockerfile: Dockerfile.dev.charterafrica
args:
MONGO_URL: ${MONGO_URL}
PAYLOAD_SECRET_KEY: ${PAYLOAD_SECRET_KEY}
SENTRY_AUTH_TOKEN: ${SENTRY_AUTH_TOKEN}
SENTRY_ORG: ${SENTRY_ORG}
NEXT_PUBLIC_SENTRY_DSN: ${CHARTERAFRICA_SENTRY_DSN}
SENTRY_ENV: ${SENTRY_ENV}
SENTRY_PROJECT: ${SENTRY_PROJECT}
ports:
- 3000:3000
container_name: charterafrica
env_file:
- apps/charterafrica/.env.local

0 comments on commit 6fc4642

Please sign in to comment.