From a8bd09ddf95e99111da0d446921b8a68c942d3dc Mon Sep 17 00:00:00 2001 From: Webber Takken Date: Tue, 13 Aug 2024 03:46:44 +0200 Subject: [PATCH] feat: simplify build step (#2) --- .dockerignore | 18 +++------- Dockerfile | 96 ++++++++------------------------------------------- package.json | 6 ++-- 3 files changed, 22 insertions(+), 98 deletions(-) diff --git a/.dockerignore b/.dockerignore index 94549d0..3ff3cd2 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,14 +1,4 @@ -.dockerignore -.editorconfig -.env -.git -.github -.gitignore -.idea -.yarn -.vscode -coverage* -tools -LICENSE -README.md -node_modules +* +!dist +!package.json +!yarn.lock diff --git a/Dockerfile b/Dockerfile index 94be6ed..829d16e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,95 +1,29 @@ -########################################################### -## ## -## Global variables ## -## ## -########################################################### +# Can not go higher than 3.15 until this answer is updated: https://stackoverflow.com/a/38433396/3593896 +FROM alpine:3.15 -# see all versions at https://hub.docker.com/r/oven/bun/tags -ARG BUN_VERSION="1" - -# see all versions at https://hub.docker.com/r/bitnami/node/tags -ARG NODE_VERSION="20" - -# Build time secrets -ARG HASS_TOKEN -ARG HASS_BASE_URL - -########################################################### -## ## -## BUILDER ## -## ## -########################################################### - -FROM bitnami/node:${NODE_VERSION} as builder - -# Allow build time secrets in this image -ARG HASS_TOKEN -ARG HASS_BASE_URL - -# Act as CI system: no interactive tty, no stdin/stdout, no watch processes -ENV CI="true" - -# Ensure we treat everything as production -ENV NODE_ENV="production" - -# Pacakage management happens through Node and Yarn -RUN curl https://get.volta.sh | bash -ENV VOLTA_HOME "/root/.volta" -ENV PATH "$VOLTA_HOME/bin:$PATH" - -# Copy only relevant files -COPY package.json yarn.lock .yarnrc.yml tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/ -COPY src/ /app/src/ - -# Build and pre-flight checks -RUN cd /app/ \ - && echo "node version: $(node --version)" \ - && echo "yarn version: $(yarn --version)" \ - && echo -e "/app/ folder:\n$(ls -alh)" \ - && yarn install --immutable \ - && yarn test \ - && yarn prettier --check . \ - && yarn lint \ - && if [ -n "$HASS_TOKEN" ] && [ -n "$HASS_BASE_URL" ]; then \ - echo "HASS_TOKEN provided, running yarn typecheck"; \ - yarn type-writer; \ - yarn typecheck; \ - else \ - echo "HASS_TOKEN not provided, skipping yarn typecheck"; \ - fi \ - && yarn build:dist \ - && echo -e "dist files:\n$(ls -alh dist | tail -n +4)" \ - && echo "dist size: $(du -sh dist)" \ - && echo "node_modules size including dev dependencies: $(du -sh node_modules)" \ - && yarn workspaces focus --production \ - && echo "node modules size after production focus: $(du -sh node_modules)" - -########################################################### -## ## -## PRODUCTION ## -## ## -########################################################### - -# Must be compatible with executable from Builder -FROM debian:buster-slim +# Install glibc. Alpine comes with `musl libc`, but Bun builds for `glibc` +RUN apk --no-cache add ca-certificates wget \ + && wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \ + && wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk \ + && apk add glibc-2.28-r0.apk \ + && rm -f glibc-2.28-r0.apk # Open Container Initiative (OCI) labels -LABEL org.opencontainers.image.title="Automation Standalone" \ +LABEL org.opencontainers.image.title="Docker Standalone" \ org.opencontainers.image.description="This image contains an end-users automations application that communicates directly with a HomeAssistant instance" \ org.opencontainers.image.version="1.0.0" \ - org.opencontainers.image.url="https://github.com/digital-alchemy/automation-standalone" \ + org.opencontainers.image.url="https://github.com/digital-alchemy/docker-standalone" \ org.opencontainers.image.documentation="https://docs.digital-alchemy.app/" \ - org.opencontainers.image.source="https://github.com/digital-alchemy/automation-standalone" \ + org.opencontainers.image.source="https://github.com/digital-alchemy/docker-standalone" \ org.opencontainers.image.vendor="Digital Alchemy" \ org.opencontainers.image.authors="Webber Takken " \ org.opencontainers.image.licenses="MIT" -# Simplicity first +# Install the application +ENV NODE_ENV="production" + WORKDIR /app -# Copy the distributable files and production specific dependencies -COPY --from=builder /app/dist /app/package.json ./ +COPY package.json yarn.lock dist/server /app/ -# Run the app -EXPOSE 3000 CMD ["./server"] diff --git a/package.json b/package.json index b7be855..c9a35ff 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,9 @@ "play": "docker-compose -f playground/docker-compose.yml up", "endplay": "docker-compose -f playground/docker-compose.yml down", "type-writer": "type-writer", - "build": "bun --env-file .env build:docker", - "build:dist": "bun build src/main.ts --compile --minify --outfile dist/server", - "build:docker": "docker build . --build-arg HASS_TOKEN=$HASS_TOKEN --build-arg HASS_BASE_URL=$HASS_BASE_URL -t automation-prod", + "build": "yarn build:dist && yarn build:docker", + "build:dist": "bun build --compile --target=bun-linux-x64-modern src/main.ts --outfile dist/server", + "build:docker": "docker build . -t automation-prod", "upgrade": "yarn up \"@digital-alchemy/*\"", "start": "docker run --env-file .env automation-prod", "test": "vitest",