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.
This commit is contained in:
1 parent
13d122e905
commit
95841ab09a
2 files changed
+44
-26
No files matched your search
@@ -12,11 +12,9 @@ env:
|
|||||||
CONTAINER_NAME: aut-management-faculty
|
CONTAINER_NAME: aut-management-faculty
|
||||||
NETWORK_NAME: shared_web_network
|
NETWORK_NAME: shared_web_network
|
||||||
PUBLIC_HOST: aut.insaight.net
|
PUBLIC_HOST: aut.insaight.net
|
||||||
# Self-hosted runner has no docker-buildx; force classic builder
|
|
||||||
DOCKER_BUILDKIT: "0"
|
DOCKER_BUILDKIT: "0"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Heavy Next.js compile on GitHub-hosted runners (not the InsAIght box)
|
|
||||||
build:
|
build:
|
||||||
name: Build (ubuntu)
|
name: Build (ubuntu)
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -37,30 +35,49 @@ jobs:
|
|||||||
npm ci --no-audit --no-fund
|
npm ci --no-audit --no-fund
|
||||||
npm run build
|
npm run build
|
||||||
test -f .next/standalone/server.js
|
test -f .next/standalone/server.js
|
||||||
|
test -f .next/BUILD_ID
|
||||||
|
|
||||||
- name: Stage standalone artifact
|
- name: Assemble runnable standalone tree
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
rm -rf prebuilt
|
||||||
mkdir -p prebuilt
|
mkdir -p prebuilt
|
||||||
cp -a .next/standalone prebuilt/standalone
|
|
||||||
cp -a .next/static prebuilt/static
|
# 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
|
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
|
- uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: next-standalone
|
name: next-standalone
|
||||||
path: prebuilt/
|
path: prebuilt/
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
|
include-hidden-files: true
|
||||||
|
|
||||||
# Server only packs a tiny image + restarts the container
|
|
||||||
deploy:
|
deploy:
|
||||||
name: Deploy (self-hosted)
|
name: Deploy (self-hosted)
|
||||||
runs-on: [self-hosted, linux]
|
runs-on: [self-hosted, linux]
|
||||||
needs: build
|
needs: build
|
||||||
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
|
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Download artifact
|
- name: Download artifact
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
@@ -72,16 +89,20 @@ jobs:
|
|||||||
docker network inspect "$NETWORK_NAME" >/dev/null 2>&1 \
|
docker network inspect "$NETWORK_NAME" >/dev/null 2>&1 \
|
||||||
|| docker network create "$NETWORK_NAME"
|
|| docker network create "$NETWORK_NAME"
|
||||||
|
|
||||||
- name: Build image from prebuilt
|
- name: Verify artifact & build image
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
test -f prebuilt/standalone/server.js
|
test -f prebuilt/server.js
|
||||||
test -f prebuilt/standalone/.next/BUILD_ID
|
test -f prebuilt/.next/BUILD_ID
|
||||||
test -d prebuilt/standalone/node_modules
|
test -d prebuilt/.next/static
|
||||||
test -d prebuilt/static
|
test -d prebuilt/node_modules
|
||||||
# Context = prebuilt/ so root .dockerignore cannot strip standalone/.next
|
test -f prebuilt/Dockerfile
|
||||||
docker build -f Dockerfile.prebuilt -t "$IMAGE_NAME" prebuilt
|
|
||||||
|
|
||||||
|
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
|
- name: Restart container
|
||||||
env:
|
env:
|
||||||
@@ -106,6 +127,8 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
docker run "${RUN_ARGS[@]}" "$IMAGE_NAME"
|
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 "Deployed ${CONTAINER_NAME} on ${NETWORK_NAME}"
|
||||||
echo "Point Nginx Proxy Manager: ${PUBLIC_HOST} → http://${CONTAINER_NAME}:3000"
|
echo "Point Nginx Proxy Manager: ${PUBLIC_HOST} → http://${CONTAINER_NAME}:3000"
|
||||||
|
|||||||
+6
-11
@@ -1,10 +1,8 @@
|
|||||||
# Packs a pre-built Next.js standalone output — no npm / next build on the server.
|
# Runtime image for a fully assembled Next.js standalone tree.
|
||||||
# Build context must be the `prebuilt/` directory:
|
# Build context = prebuilt/ and must already contain:
|
||||||
# standalone/ ← contents of .next/standalone (includes .next/BUILD_ID + node_modules)
|
# server.js, .next/BUILD_ID, .next/static/, public/, node_modules/, …
|
||||||
# static/ ← contents of .next/static
|
|
||||||
# public/ ← public/
|
|
||||||
|
|
||||||
FROM node:22-alpine AS runner
|
FROM node:22-alpine
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
@@ -14,12 +12,9 @@ ENV HOSTNAME=0.0.0.0
|
|||||||
RUN addgroup --system --gid 1001 nodejs \
|
RUN addgroup --system --gid 1001 nodejs \
|
||||||
&& adduser --system --uid 1001 nextjs
|
&& adduser --system --uid 1001 nextjs
|
||||||
|
|
||||||
COPY --chown=nextjs:nodejs standalone/ ./
|
# Single COPY of an already-complete app root (avoids missing hidden .next)
|
||||||
COPY --chown=nextjs:nodejs public ./public
|
COPY --chown=nextjs:nodejs . ./
|
||||||
COPY --chown=nextjs:nodejs static ./.next/static
|
|
||||||
|
|
||||||
USER nextjs
|
USER nextjs
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
CMD ["node", "server.js"]
|
CMD ["node", "server.js"]
|
||||||
Reference in new issue
Block a user