This commit is contained in:
NotII
2025-04-06 16:23:02 +01:00
parent d05d7b4938
commit 8da4d0085e
3 changed files with 36 additions and 85 deletions

View File

@@ -1,67 +1,48 @@
# Base Node.js image # Use official Node.js image as base
FROM node:20-alpine as builder FROM node:18-alpine AS builder
# Set working directory
WORKDIR /app WORKDIR /app
# Copy package files for better caching
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
# Install dependencies (using install instead of ci to handle mismatched lock file)
RUN npm install --force RUN npm install --force
# Install git and prepare for git hash capture
RUN apk add --no-cache git
# Copy source code
COPY . . COPY . .
# Get git commit hash or use "unknown" if not available # Install git, get commit hash
RUN if [ -d .git ]; then \ RUN apk add --no-cache git \
git rev-parse --short HEAD > /app/git_commit_sha; \ && git rev-parse --short HEAD > /app/git_commit_sha
else \
echo "unknown" > /app/git_commit_sha; \
fi
# Set environment variables
ENV NEXT_PUBLIC_API_URL=/api ENV NEXT_PUBLIC_API_URL=/api
ENV NODE_ENV=production
# Build application with output standalone option # Build the Next.js application
RUN echo "Building with commit $(cat /app/git_commit_sha)" && \ # The environment variable will be available during build if needed
npm run build # ENV GIT_COMMIT_SHA=$(cat /app/git_commit_sha)
RUN echo "Building with GIT_COMMIT_SHA=$(cat /app/git_commit_sha)" && npm run build
# Production image # ---- Production Stage ----
FROM node:20-alpine FROM node:18-alpine AS runner
# Set working directory inside the container
WORKDIR /app WORKDIR /app
# Set production env vars RUN mkdir -p /app/public
ENV NODE_ENV=production
ENV NEXT_PUBLIC_API_URL=/api
ENV SERVER_API_URL=https://internal-api.inboxi.ng
# Create non-root user # Copy only necessary files from builder
RUN addgroup --system --gid 1001 nodejs && \ COPY --from=builder /app/package.json /app/package-lock.json ./
adduser --system --uid 1001 nextjs COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
# Copy only necessary files # Copy commit hash file from builder stage
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/git_commit_sha /app/git_commit_sha COPY --from=builder /app/git_commit_sha /app/git_commit_sha
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# Create startup script to set the git commit hash
RUN echo '#!/bin/sh' > /app/start.sh && \
echo 'export GIT_COMMIT_SHA=$(cat /app/git_commit_sha)' >> /app/start.sh && \
echo 'exec node server.js' >> /app/start.sh && \
chmod +x /app/start.sh
# Switch to non-root user
USER nextjs
EXPOSE 3000 EXPOSE 3000
# Use the wrapper script to set the git hash properly ENV NODE_ENV=production
CMD ["/app/start.sh"] ENV NEXT_PUBLIC_API_URL=/api
ENV SERVER_API_URL=https://internal-api.inboxi.ng/api
# Set GIT_COMMIT_SHA environment variable in the final image by reading the file
ENV GIT_COMMIT_SHA="$(cat /app/git_commit_sha)"
# Start Next.js server
CMD ["npm", "run", "start"]

View File

@@ -11,8 +11,7 @@ let cachedGitInfo: GitInfo | null = null;
/** /**
* Gets the current git commit hash from various sources * Gets the current git commit hash from various sources
* - First tries to read directly from the Git hash file in Docker * - First tries environment variable (from Docker)
* - Then tries environment variable (from Docker)
* - Then tries the JSON file created by our build script * - Then tries the JSON file created by our build script
* - Returns "dev" if nothing is available * - Returns "dev" if nothing is available
*/ */
@@ -22,34 +21,8 @@ export async function getGitCommitInfo(): Promise<GitInfo> {
return cachedGitInfo; return cachedGitInfo;
} }
// In Docker or server, try to read the hash file directly first (most reliable method) // First, check if we have the env variable (from Docker)
if (typeof window === 'undefined') {
try {
const fs = require('fs');
// Check for Docker-specific git hash file
const dockerGitHashPath = '/app/git_commit_sha';
if (fs.existsSync(dockerGitHashPath)) {
const hash = fs.readFileSync(dockerGitHashPath, 'utf8').trim();
console.log(`Read git hash from file: ${hash}`);
const gitInfo: GitInfo = {
commitHash: hash,
buildTime: new Date().toISOString(),
};
cachedGitInfo = gitInfo;
return gitInfo;
}
} catch (error) {
console.warn('Could not read git hash from Docker file', error);
}
}
// Then, check if we have the env variable
if (process.env.GIT_COMMIT_SHA) { if (process.env.GIT_COMMIT_SHA) {
// Check if it's the literal string, which is an error case
if (process.env.GIT_COMMIT_SHA.includes('$(cat')) {
console.warn('GIT_COMMIT_SHA contains a shell command instead of the actual hash');
} else {
const gitInfo: GitInfo = { const gitInfo: GitInfo = {
commitHash: process.env.GIT_COMMIT_SHA, commitHash: process.env.GIT_COMMIT_SHA,
buildTime: new Date().toISOString(), buildTime: new Date().toISOString(),
@@ -57,7 +30,6 @@ export async function getGitCommitInfo(): Promise<GitInfo> {
cachedGitInfo = gitInfo; cachedGitInfo = gitInfo;
return gitInfo; return gitInfo;
} }
}
// In the browser, fetch from the public directory // In the browser, fetch from the public directory
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {

View File

@@ -24,8 +24,6 @@ const nextConfig = {
}, },
]; ];
}, },
// Output mode for Docker builds
output: 'standalone',
// Reduce memory usage during builds // Reduce memory usage during builds
onDemandEntries: { onDemandEntries: {
// Period (in ms) where the server will keep pages in the buffer // Period (in ms) where the server will keep pages in the buffer