Skip to content

Commit

Permalink
feat: simplify build step (#2) (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
webbertakken authored Aug 13, 2024
1 parent 13503f9 commit eafee63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 98 deletions.
18 changes: 4 additions & 14 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
96 changes: 15 additions & 81 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>" \
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"]
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit eafee63

Please sign in to comment.