Skip to content

Instantly share code, notes, and snippets.

@mogeko
Last active August 16, 2024 08:30
Show Gist options
  • Save mogeko/db936676e51e6985d6148c63a8f91df9 to your computer and use it in GitHub Desktop.
Save mogeko/db936676e51e6985d6148c63a8f91df9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
(async () => {
const response = await fetch("http://localhost:3000/api/ping");
const result = await response.text();
if (!response.ok) {
throw new Error(`Healthcheck failed: ${response.status}`);
} else if (result !== "pong") {
throw new Error(`Healthcheck failed: ${result} !== pong`);
} else {
console.info("\x1b[32m", "Healthcheck passed", "\x1b[0m");
}
})().catch((error) => {
console.error(error.message);
process.exit(1);
});
FROM docker.io/library/node:22-bookworm AS base
ENV CI="true" PNPM_HOME="/opt/pnpm" PATH="/opt/pnpm:$PATH"
RUN corepack enable pnpm
WORKDIR /app
FROM base AS preprocess
COPY . /app
RUN --mount=type=cache,id=pnpm,target=/opt/pnpm/store <<EOF
turbo_version=$(node -p "require('./package.json').devDependencies.turbo")
pnpm install --global "turbo@${turbo_version}"
EOF
RUN turbo prune web --docker
FROM base AS builder
COPY --from=preprocess /app/out/json /app
RUN --mount=type=cache,id=pnpm,target=/opt/pnpm/store \
pnpm install --frozen-lockfile
COPY --from=preprocess /app/out/full /app
RUN pnpm run build --filter=web
FROM gcr.io/distroless/nodejs22-debian12
COPY --from=builder /app/apps/web/.next/standalone /app
COPY --from=builder /app/apps/web/.next/static /app/apps/web/.next/static
COPY --from=builder /app/apps/web/public /app/apps/web/public
ENV HOSTNAME="0.0.0.0" PORT="3000"
EXPOSE 3000
CMD ["/app/apps/web/server.js"]
COPY --from=preprocess /app/scripts/healthcheck.cjs /app/healthcheck.cjs
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD ["/nodejs/bin/node", "/app/healthcheck.cjs"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment