Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rework dockerfile #183

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
# Adapted from Remix's Indie Stack
FROM node:20-bullseye-slim AS base

ENV NODE_ENV=production
FROM node:20-alpine AS development-dependencies-env
COPY . /app
WORKDIR /app
RUN npm ci

# Install system dependencies
RUN apt-get update && apt-get install -y openssl git
FROM node:20-alpine AS production-dependencies-env
COPY ./package.json package-lock.json prisma /app/
WORKDIR /app
RUN npm ci --omit=dev && npx prisma generate

# Install dependencies
FROM base AS deps

WORKDIR /myapp
COPY package.json package-lock.json ./
RUN npm ci --include=dev

# Generate Prisma client, add commit hash, and build the app
FROM deps AS build

ARG SOURCE_COMMIT
ENV SOURCE_COMMIT=${SOURCE_COMMIT}

COPY prisma ./prisma
RUN npx prisma generate

COPY . .
RUN if [ -d .git ]; then \
git log -n 1 --pretty=format:%H > .build-last-commit; \
elif [ -n "${SOURCE_COMMIT:-}" ] && [ "${SOURCE_COMMIT:-}" != "unknown" ]; then \
echo "${SOURCE_COMMIT:-}" > .build-last-commit; \
else \
touch .build-last-commit; \
fi
FROM node:20-alpine AS build-env
COPY . /app/
COPY --from=development-dependencies-env /app/node_modules /app/node_modules
WORKDIR /app
RUN npm run build
RUN rm -rf .git

# Production image with minimal footprint
FROM base

WORKDIR /myapp

COPY --from=deps /myapp/node_modules /myapp/node_modules
COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma
COPY --from=build /myapp/build /myapp/build
COPY --from=build /myapp/public /myapp/public
COPY --from=build /myapp/package.json /myapp/package.json
COPY --from=build /myapp/start.sh /myapp/.build-last-commit /myapp
COPY --from=build /myapp/prisma /myapp/prisma

ENTRYPOINT [ "./start.sh" ]
FROM node:20-alpine
COPY ./package.json package-lock.json start.sh prisma /app/
COPY --from=production-dependencies-env /app/node_modules /app/node_modules
COPY --from=build-env /app/build /app/build
WORKDIR /app
ENTRYPOINT ["./start.sh"]
8 changes: 4 additions & 4 deletions app/components/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isOurHostname } from "~/utils/misc";
import { useRules } from "./app-context";

export function Footer() {
const { buildLastCommit, appFooterName } = useRules();
const { sourceCommit, appFooterName } = useRules();

return (
<footer className="my-8 text-sm text-neutral-400 drop-shadow-xs select-none">
Expand All @@ -23,14 +23,14 @@ export function Footer() {
children={() =>
isOurHostname() ? (
<div className="flex items-center justify-center gap-2 text-xs">
{buildLastCommit !== undefined && (
{sourceCommit !== undefined && (
<>
<a
className="transition-all hover:text-blue-500"
href={`https://github.com/ianlucas/cs2-inventory-simulator/commit/${buildLastCommit}`}
href={`https://github.com/ianlucas/cs2-inventory-simulator/commit/${sourceCommit}`}
target="_blank"
>
Ver. {buildLastCommit?.substring(0, 7)}
Ver. {sourceCommit?.substring(0, 7)}
</a>
&middot;
</>
Expand Down
12 changes: 1 addition & 11 deletions app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@

import { assert } from "@ianlucas/cs2-lib";
import "dotenv/config";
import { existsSync, readFileSync } from "fs";
import { resolve } from "path";
import { trim } from "./utils/misc";

assert(process.env.SESSION_SECRET, "SESSION_SECRET must be set");
export const SESSION_SECRET = process.env.SESSION_SECRET;

export const BUILD_LAST_COMMIT_PATH = resolve(
process.cwd(),
".build-last-commit"
);
export const BUILD_LAST_COMMIT = existsSync(BUILD_LAST_COMMIT_PATH)
? trim(readFileSync(BUILD_LAST_COMMIT_PATH, "utf-8"))
: undefined;

export const {
ASSETS_BASE_URL,
CLOUDFLARE_ANALYTICS_TOKEN,
CS2_CSGO_PATH,
SOURCE_COMMIT,
STEAM_API_KEY,
STEAM_CALLBACK_URL
} = process.env;
6 changes: 3 additions & 3 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { SyncIndicator } from "./components/sync-indicator";
import { SyncWarn } from "./components/sync-warn";
import {
ASSETS_BASE_URL,
BUILD_LAST_COMMIT,
CLOUDFLARE_ANALYTICS_TOKEN
CLOUDFLARE_ANALYTICS_TOKEN,
SOURCE_COMMIT
} from "./env.server";
import { middleware } from "./http.server";
import { getLocalizationChecksum } from "./localization.server";
Expand Down Expand Up @@ -89,8 +89,8 @@ export async function loader({ request }: LoaderFunctionArgs) {
rules: {
...(await getClientRules(user?.id)),
assetsBaseUrl: noempty(ASSETS_BASE_URL),
buildLastCommit: BUILD_LAST_COMMIT,
cloudflareAnalyticsToken: CLOUDFLARE_ANALYTICS_TOKEN,
sourceCommit: SOURCE_COMMIT,
meta: { appUrl, appSiteName }
},
preferences: {
Expand Down