From 95841ab09a98c4365b30956f7cbda997b5595195 Mon Sep 17 00:00:00 2001 From: Darian Date: Thu, 24 Sep 2026 15:35:51 +0330 Subject: [PATCH] Refactor Dockerfile.prebuilt and GitHub Actions workflow for streamlined Next.js deployment. Consolidated file copying in Dockerfile to simplify the build context. Updated workflow to assemble a complete standalone tree and verify artifact integrity before building the Docker image. --- .github/workflows/deploy.yml | 55 +++++++++++++++++++++++++----------- Dockerfile.prebuilt | 17 ++++------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 205f2f3..c90a450 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -12,11 +12,9 @@ env: CONTAINER_NAME: aut-management-faculty NETWORK_NAME: shared_web_network PUBLIC_HOST: aut.insaight.net - # Self-hosted runner has no docker-buildx; force classic builder DOCKER_BUILDKIT: "0" jobs: - # Heavy Next.js compile on GitHub-hosted runners (not the InsAIght box) build: name: Build (ubuntu) runs-on: ubuntu-latest @@ -37,30 +35,49 @@ jobs: npm ci --no-audit --no-fund npm run build test -f .next/standalone/server.js + test -f .next/BUILD_ID - - name: Stage standalone artifact + - name: Assemble runnable standalone tree run: | set -euo pipefail + rm -rf prebuilt mkdir -p prebuilt - cp -a .next/standalone prebuilt/standalone - cp -a .next/static prebuilt/static - cp -a public prebuilt/public + + # Flatten standalone to artifact root (includes .next + node_modules) + cp -a .next/standalone/. prebuilt/ + + # Static assets + public (required next to standalone) + cp -a .next/static prebuilt/.next/static + if [ -d public ]; then + cp -a public prebuilt/public + else + mkdir -p prebuilt/public + fi + + # Dockerfile travels with the artifact so deploy context is self-contained + cp Dockerfile.prebuilt prebuilt/Dockerfile + + echo "Assembled tree:" + ls -la prebuilt + ls -la prebuilt/.next + test -f prebuilt/server.js + test -f prebuilt/.next/BUILD_ID + test -d prebuilt/.next/static + test -d prebuilt/node_modules - uses: actions/upload-artifact@v4 with: name: next-standalone path: prebuilt/ retention-days: 1 + include-hidden-files: true - # Server only packs a tiny image + restarts the container deploy: name: Deploy (self-hosted) runs-on: [self-hosted, linux] needs: build if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' steps: - - uses: actions/checkout@v4 - - name: Download artifact uses: actions/download-artifact@v4 with: @@ -72,16 +89,20 @@ jobs: docker network inspect "$NETWORK_NAME" >/dev/null 2>&1 \ || docker network create "$NETWORK_NAME" - - name: Build image from prebuilt + - name: Verify artifact & build image run: | set -euo pipefail - test -f prebuilt/standalone/server.js - test -f prebuilt/standalone/.next/BUILD_ID - test -d prebuilt/standalone/node_modules - test -d prebuilt/static - # Context = prebuilt/ so root .dockerignore cannot strip standalone/.next - docker build -f Dockerfile.prebuilt -t "$IMAGE_NAME" prebuilt + test -f prebuilt/server.js + test -f prebuilt/.next/BUILD_ID + test -d prebuilt/.next/static + test -d prebuilt/node_modules + test -f prebuilt/Dockerfile + docker build -t "$IMAGE_NAME" prebuilt + + # Confirm BUILD_ID is actually inside the image + docker run --rm --entrypoint sh "$IMAGE_NAME" -c \ + 'test -f /app/.next/BUILD_ID && test -f /app/server.js && ls -la /app/.next' - name: Restart container env: @@ -106,6 +127,8 @@ jobs: fi docker run "${RUN_ARGS[@]}" "$IMAGE_NAME" + sleep 2 + docker logs "$CONTAINER_NAME" 2>&1 | tail -n 30 echo "Deployed ${CONTAINER_NAME} on ${NETWORK_NAME}" echo "Point Nginx Proxy Manager: ${PUBLIC_HOST} → http://${CONTAINER_NAME}:3000" diff --git a/Dockerfile.prebuilt b/Dockerfile.prebuilt index 32dacf8..c5db2a1 100644 --- a/Dockerfile.prebuilt +++ b/Dockerfile.prebuilt @@ -1,10 +1,8 @@ -# Packs a pre-built Next.js standalone output — no npm / next build on the server. -# Build context must be the `prebuilt/` directory: -# standalone/ ← contents of .next/standalone (includes .next/BUILD_ID + node_modules) -# static/ ← contents of .next/static -# public/ ← public/ +# Runtime image for a fully assembled Next.js standalone tree. +# Build context = prebuilt/ and must already contain: +# server.js, .next/BUILD_ID, .next/static/, public/, node_modules/, … -FROM node:22-alpine AS runner +FROM node:22-alpine WORKDIR /app ENV NODE_ENV=production ENV NEXT_TELEMETRY_DISABLED=1 @@ -14,12 +12,9 @@ ENV HOSTNAME=0.0.0.0 RUN addgroup --system --gid 1001 nodejs \ && adduser --system --uid 1001 nextjs -COPY --chown=nextjs:nodejs standalone/ ./ -COPY --chown=nextjs:nodejs public ./public -COPY --chown=nextjs:nodejs static ./.next/static +# Single COPY of an already-complete app root (avoids missing hidden .next) +COPY --chown=nextjs:nodejs . ./ USER nextjs - EXPOSE 3000 - CMD ["node", "server.js"]