diff --git a/.github/workflows/deploy-image.yml b/.github/workflows/deploy-image.yml new file mode 100644 index 0000000..99cd27f --- /dev/null +++ b/.github/workflows/deploy-image.yml @@ -0,0 +1,38 @@ +name: Build and Push Docker Image +on: + push: + branches: + - develop +env: + REGISTRY: ghcr.io +jobs: + build-and-push-image: + runs-on: ubuntu-latest + strategy: + matrix: + include: + - image: pwrcode-frontend + dockerfile: apps/pwrcode-frontend + - image: pwrcode-backend + dockerfile: apps/pwrcode-backend + steps: + - name: checkout repository + uses: actions/checkout@v4 + - name: Login in to Container registry + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.IMHOHEONG_GITHUB_ACCESS_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ matrix.image }} + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/apps/pwrcode-backend/Dockerfile b/apps/pwrcode-backend/Dockerfile new file mode 100644 index 0000000..485a503 --- /dev/null +++ b/apps/pwrcode-backend/Dockerfile @@ -0,0 +1,61 @@ +################### +# BUILD FOR LOCAL DEVELOPMENT +################### + +FROM node:18-alpine As development + +# Create app directory +WORKDIR /usr/src/app + +# Copy application dependency manifests to the container image. +# A wildcard is used to ensure copying both package.json AND package-lock.json (when available). +# Copying this first prevents re-running npm install on every code change. +COPY --chown=node:node package*.json ./ + +# Install app dependencies using the `npm ci` command instead of `npm install` +RUN npm ci + +# Bundle app source +COPY --chown=node:node . . + +# Use the node user from the image (instead of the root user) +USER node + +################### +# BUILD FOR PRODUCTION +################### + +FROM node:18-alpine As build + +WORKDIR /usr/src/app + +COPY --chown=node:node package*.json ./ + +# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development image +COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules + +COPY --chown=node:node . . + +# Run the build command which creates the production bundle +RUN npm run build + +# Set NODE_ENV environment variable +ENV NODE_ENV production + +# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possible +RUN npm ci --only=production && npm cache clean --force + +USER node + +################### +# PRODUCTION +################### + +FROM node:18-alpine As production + +# Copy the bundled code from the build stage to the production image +COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules +COPY --chown=node:node --from=build /usr/src/app/dist ./dist + +# Start the server using the production build +CMD [ "node", "dist/main.js" ] diff --git a/apps/pwrcode-frontend/Dockerfile b/apps/pwrcode-frontend/Dockerfile new file mode 100644 index 0000000..2851481 --- /dev/null +++ b/apps/pwrcode-frontend/Dockerfile @@ -0,0 +1,65 @@ +FROM node:18-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +# 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 + +# 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 yarn global add pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +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 + +RUN yarn build + +# If using npm comment out above and use below instead +# RUN npm run build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +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 + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next +RUN chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT 3000 +# set hostname to localhost +ENV HOSTNAME "0.0.0.0" + +CMD ["node", "server.js"]