generated from imperial/impaas-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
devops: modify GH actions and Dockerfile (#100)
- Modify Dockerfile to be faster by not installing Prisma twice and assuming npm - Modify the Dockerfile/GitHub actions workflow to use caching for faster build times - Modify GitHub actions workflow to: - Set the `DATABASE_URL` environment variable properly - Create the upload directories in the volume - Set the EntraID environment variables properly
- Loading branch information
1 parent
d23da08
commit 3f8c94c
Showing
2 changed files
with
52 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,68 @@ | ||
FROM node:18-alpine AS base | ||
|
||
# Install dependencies only when needed | ||
FROM base AS deps | ||
FROM base AS app_base | ||
|
||
# Make UPLOAD_DIRs | ||
ENV UPLOAD_DIR=/uploads | ||
RUN mkdir $UPLOAD_DIR | ||
RUN mkdir $UPLOAD_DIR/banners $UPLOAD_DIR/cvs $UPLOAD_DIR/avatars $UPLOAD_DIR/logos $UPLOAD_DIR/attachments | ||
|
||
# Prisma base image only for installing prisma | ||
FROM base AS prisma_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 | ||
WORKDIR /app | ||
COPY package.json package-lock.json* ./ | ||
RUN npm install prisma --omit=dev | ||
|
||
FROM prisma_base AS deps | ||
WORKDIR /app | ||
|
||
# Install dependencies based on the preferred package manager | ||
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ | ||
RUN \ | ||
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ | ||
elif [ -f package-lock.json ]; then npm ci; \ | ||
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ | ||
else echo "Lockfile not found." && exit 1; \ | ||
fi | ||
# Won't need to re-install prisma, since it's already installed in the prisma_base image | ||
RUN npm ci | ||
|
||
# Rebuild the source code only when needed | ||
FROM base AS builder | ||
WORKDIR /app | ||
|
||
# Add a dummy argument to force rebuild | ||
ARG CACHEBUST=1 | ||
COPY --from=deps /app/node_modules ./node_modules | ||
COPY . . | ||
|
||
# Next.js collects completely anonymous telemetry data about general usage. | ||
# Learn more here: https://nextjs.org/telemetry | ||
# Uncomment the following line in case you want to disable telemetry during the build. | ||
# ENV NEXT_TELEMETRY_DISABLED 1 | ||
# Disable telemetry during the build. | ||
ENV NEXT_TELEMETRY_DISABLED 1 | ||
|
||
RUN \ | ||
if [ -f yarn.lock ]; then yarn run build; \ | ||
elif [ -f package-lock.json ]; then npm run build; \ | ||
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ | ||
else echo "Lockfile not found." && exit 1; \ | ||
fi | ||
RUN npm run build | ||
|
||
# Production image, copy all the files and run next | ||
FROM base AS runner | ||
FROM app_base AS runner | ||
WORKDIR /app | ||
|
||
# Make UPLOAD_DIRs | ||
ENV UPLOAD_DIR=/uploads | ||
RUN mkdir $UPLOAD_DIR | ||
RUN mkdir $UPLOAD_DIR/banners $UPLOAD_DIR/cvs $UPLOAD_DIR/avatars $UPLOAD_DIR/logos $UPLOAD_DIR/attachments | ||
|
||
ENV NODE_ENV=production | ||
# Uncomment the following line in case you want to disable telemetry during runtime. | ||
# ENV NEXT_TELEMETRY_DISABLED 1 | ||
|
||
# RUN addgroup --system --gid 1001 nodejs | ||
# RUN adduser --system --uid 1001 nextjs | ||
# Disable telemetry during runtime. | ||
ENV NEXT_TELEMETRY_DISABLED 1 | ||
|
||
COPY --from=builder /app/public ./public | ||
|
||
|
||
# Set the correct permission for prerender cache | ||
RUN mkdir .next | ||
RUN chown node:node .next | ||
|
||
# Copy prisma schema | ||
COPY --from=builder --chown=node:node /app/prisma ./prisma | ||
|
||
# Copy over the prisma client | ||
COPY --from=prisma_base /app/node_modules ./node_modules | ||
|
||
# Automatically leverage output traces to reduce image size | ||
# https://nextjs.org/docs/advanced-features/output-file-tracing | ||
COPY --from=builder --chown=node:node /app/.next/standalone ./ | ||
COPY --from=builder --chown=node:node /app/.next/static ./.next/static | ||
|
||
|
||
# Take ownershuip of the app folder & upload | ||
# Take ownership of the app folder and uploads | ||
RUN chown node:node /app | ||
RUN chown -R node:node /uploads | ||
RUN chown -R node:node $UPLOAD_DIR | ||
|
||
USER node | ||
|
||
# Install prisma so this user can use it | ||
RUN npm install prisma | ||
|
||
EXPOSE 3000 | ||
|
||
ENV PORT=3000 | ||
|
||
# server.js is created by next build from the standalone output | ||
# https://nextjs.org/docs/pages/api-reference/next-config-js/output | ||
CMD HOSTNAME="0.0.0.0" node server.js | ||
CMD HOSTNAME="0.0.0.0" node server.js |