Next.js Discord

Discord Forum

CI/CD with GitHub Actions & Standalone app

Unanswered
Citrine Wagtail posted this in #help-forum
Open in Discord
Avatar
Citrine WagtailOP
I've got a GitHub Actions pipeline setup that builds a Docker image (for linux/arm64), deploys on GitHub Container Registry, then SSHs on the VPS and pulls the latest image. The deployment step approximately 7 minutes, and the deployment finishes in around 20 seconds.

I am thinking my problem is that I am building for linux/arm64, although I really don't have any alternatives - since that's the server architecture.

Here's the build part of the workflow:

build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Publish to Registry
        uses: elgohr/Publish-Docker-Github-Action@v5
        with:
          name: user/project/image
          registry: ghcr.io
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets. GITHUB_TOKEN }}
          dockerfile: Dockerfile
          platforms: linux/arm64
          #buildargs: NEXT_PUBLIC_BACKEND_URL,NEXT_PUBLIC_META_API_KEY
          tags: latest


7 minutes is taken by elgohr/Publish-Docker-Github-Action@v5, although there aren't any obvious errors. Are there any alternatives that could speed up my deployment process?

The RUN npm ci step is taking 100 seconds, RUN npx prisma generate takes 25 seconds, npm run build takes 263 seconds. Is this normal? Is my Dockerfile wrong? Is it normal for NextJS to take so long to create a build (for a small, hello world app)?

2 Replies

Avatar
Citrine WagtailOP
Here's my Dockerfile

FROM node:20-alpine AS deps

#https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine
RUN apk update && apk add --no-cache libc6-compat && apk add git

WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# Rebuild the source code only when needed
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
ARG NODE_ENV=production
RUN echo ${NODE_ENV}
RUN npx prisma generate
RUN npm run build

FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

#copy next config js if not using default config
COPY --from=builder /app/next.config.js ./next.config.js
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY --chown=nextjs:nodejs prisma ./prisma/
COPY --chown=nextjs:nodejs bootstrap.sh ./

RUN apk add --no-cache ca-certificates openssl bash

RUN chmod +x bootstrap.sh

USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["./bootstrap.sh"]
Bootstrap.sh contains
#!/bin/sh
# Run migrations
DATABASE_URL="file:./dev.db" npx --yes prisma migrate deploy
# start app
DATABASE_URL="file:./dev.db" node server.js


so nothing special.
Build command runs: next build