diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..5b9c8f930 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,23 @@ +# Do NOT ignore .git - needed in the image for version metadata +# .git +node_modules +**/node_modules +npm-debug.log +yarn-error.log +dist +.DS_Store +.gitignore +.vscode +.env +.env.local +logs +*.log +coverage +tests +docs +.idea +.cache +# Ignore local config backups +server/src/configs/koji_backups/** +# Keep package and source files but ignore nested node_modules +packages/*/node_modules diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index e4eef64a0..1143535cc 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -9,7 +9,9 @@ jobs: release: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4.1.1 + - uses: actions/checkout@v4 + with: + fetch-depth: 1 - name: Set .gitsha run: 'echo ${{ github.sha }} > .gitsha' - name: Set .gitref diff --git a/Dockerfile b/Dockerfile index 3efc6246f..6c8e8b3f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,16 +5,63 @@ # - mount areas.json to /home/node/server/src/configs/areas.json # - Also mount every other configuration file necessary into the according directory. -FROM node:22-alpine +FROM node:22-alpine AS builder ENV NPM_CONFIG_PREFIX=/home/node/.npm-global ENV PATH=$PATH:/home/node/.npm-global/bin WORKDIR /home/node -COPY package.json . -COPY yarn.lock . -RUN apk add git -RUN npm install -g yarn + +# Install minimal build deps +RUN apk add --no-cache git python3 make g++ + +# Install yarn (node:22 includes corepack but ensure yarn v1 available) +RUN npm install -g yarn@1.22.19 + +# Copy package manifests first for better layer caching +COPY package.json yarn.lock ./ +COPY packages ./packages +COPY server ./server +COPY public ./public +COPY ReactMap.js ./ReactMap.js + +# Copy remaining source needed for build (excluding node_modules via .dockerignore) COPY . . -RUN yarn install + +# Capture git metadata for update checks in Docker builds. +RUN if [ -d .git ]; then \ + git rev-parse HEAD > .gitsha; \ + ref="$(git symbolic-ref -q HEAD || true)"; \ + if [ -z "$ref" ]; then ref="refs/heads/main"; fi; \ + printf '%s\n' "$ref" > .gitref; \ + fi + +# Install all deps and build +RUN yarn install --frozen-lockfile RUN yarn build + +# Reinstall only production dependencies to a clean node_modules folder +RUN rm -rf node_modules && HUSKY=0 yarn install --production --frozen-lockfile + + +# Final runtime image +FROM node:22-alpine AS runtime +ENV NODE_ENV=production +ENV NPM_CONFIG_PREFIX=/home/node/.npm-global +ENV PATH=$PATH:/home/node/.npm-global/bin +WORKDIR /home/node + +# Install yarn in runtime if you need it (keeps compatibility). +RUN npm install -g yarn@1.22.19 + +# Copy production node_modules and built assets from builder +COPY --from=builder /home/node/node_modules ./node_modules +COPY --from=builder /home/node/package.json ./package.json +COPY --from=builder /home/node/server ./server +COPY --from=builder /home/node/public ./public +COPY --from=builder /home/node/dist ./dist +COPY --from=builder /home/node/ReactMap.js ./ReactMap.js +COPY --from=builder /home/node/packages ./packages +COPY --from=builder /home/node/config ./config +COPY --from=builder /home/node/.gitsha ./.gitsha +COPY --from=builder /home/node/.gitref ./.gitref