From 8da4d0085e4c05648fa02e99de57f2d878989f72 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Sun, 6 Apr 2025 16:23:02 +0100 Subject: [PATCH] revert --- Dockerfile | 75 ++++++++++++++++++------------------------------ lib/git-utils.ts | 44 ++++++---------------------- next.config.mjs | 2 -- 3 files changed, 36 insertions(+), 85 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2fcbf14..cd603e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,67 +1,48 @@ -# Base Node.js image -FROM node:20-alpine as builder - -# Set working directory +# Use official Node.js image as base +FROM node:18-alpine AS builder WORKDIR /app - -# Copy package files for better caching COPY package.json package-lock.json ./ -# Install dependencies (using install instead of ci to handle mismatched lock file) RUN npm install --force -# Install git and prepare for git hash capture -RUN apk add --no-cache git - -# Copy source code COPY . . -# Get git commit hash or use "unknown" if not available -RUN if [ -d .git ]; then \ - git rev-parse --short HEAD > /app/git_commit_sha; \ - else \ - echo "unknown" > /app/git_commit_sha; \ - fi +# Install git, get commit hash +RUN apk add --no-cache git \ + && git rev-parse --short HEAD > /app/git_commit_sha -# Set environment variables ENV NEXT_PUBLIC_API_URL=/api -ENV NODE_ENV=production -# Build application with output standalone option -RUN echo "Building with commit $(cat /app/git_commit_sha)" && \ - npm run build +# Build the Next.js application +# The environment variable will be available during build if needed +# 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 -FROM node:20-alpine +# ---- Production Stage ---- +FROM node:18-alpine AS runner +# Set working directory inside the container WORKDIR /app -# Set production env vars -ENV NODE_ENV=production -ENV NEXT_PUBLIC_API_URL=/api -ENV SERVER_API_URL=https://internal-api.inboxi.ng +RUN mkdir -p /app/public -# Create non-root user -RUN addgroup --system --gid 1001 nodejs && \ - adduser --system --uid 1001 nextjs +# Copy only necessary files from builder +COPY --from=builder /app/package.json /app/package-lock.json ./ +COPY --from=builder /app/.next ./.next +COPY --from=builder /app/node_modules ./node_modules -# Copy only necessary files -COPY --from=builder /app/public ./public -COPY --from=builder /app/next.config.mjs ./ +# Copy commit hash file from builder stage 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 -# Use the wrapper script to set the git hash properly -CMD ["/app/start.sh"] \ No newline at end of file +ENV NODE_ENV=production +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"] \ No newline at end of file diff --git a/lib/git-utils.ts b/lib/git-utils.ts index 71de643..72fc3d0 100644 --- a/lib/git-utils.ts +++ b/lib/git-utils.ts @@ -11,8 +11,7 @@ let cachedGitInfo: GitInfo | null = null; /** * Gets the current git commit hash from various sources - * - First tries to read directly from the Git hash file in Docker - * - Then tries environment variable (from Docker) + * - First tries environment variable (from Docker) * - Then tries the JSON file created by our build script * - Returns "dev" if nothing is available */ @@ -22,41 +21,14 @@ export async function getGitCommitInfo(): Promise { return cachedGitInfo; } - // In Docker or server, try to read the hash file directly first (most reliable method) - 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 + // First, check if we have the env variable (from Docker) 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 = { - commitHash: process.env.GIT_COMMIT_SHA, - buildTime: new Date().toISOString(), - }; - cachedGitInfo = gitInfo; - return gitInfo; - } + const gitInfo: GitInfo = { + commitHash: process.env.GIT_COMMIT_SHA, + buildTime: new Date().toISOString(), + }; + cachedGitInfo = gitInfo; + return gitInfo; } // In the browser, fetch from the public directory diff --git a/next.config.mjs b/next.config.mjs index a7159e5..a15c1f9 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -24,8 +24,6 @@ const nextConfig = { }, ]; }, - // Output mode for Docker builds - output: 'standalone', // Reduce memory usage during builds onDemandEntries: { // Period (in ms) where the server will keep pages in the buffer