diff --git a/Dockerfile b/Dockerfile index 6d6ebbc4c3..6f7012f94c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,9 @@ # If you update this base image, make sure to update the base runners # in .github/workflows/ to the corresponding Ubuntu version + +###################### +# Python build stage # +###################### FROM ghcr.io/astral-sh/uv:python3.10-bookworm AS build-python @@ -11,7 +15,50 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH" COPY ./dependencies/pip/requirements.txt "${TMP_DIR}/pip_dependencies.txt" RUN uv pip sync "${TMP_DIR}/pip_dependencies.txt" 1>/dev/null +#################### +# Node build stage # +#################### +FROM node:20.19-bookworm-slim AS build-node +ENV KPI_SRC_DIR=/srv/src/kpi \ + TMP_DIR=/srv/tmp +WORKDIR "${KPI_SRC_DIR}" + +# Use a non-root user +RUN addgroup -S kobo && adduser -S -G kobo kobo + +# Install npm dependencies & run post-install steps +COPY --chown=kobo:kobo scripts ./scripts +COPY --chown=kobo:kobo patches ./patches +COPY --chown=kobo:kobo \ + .browserslistrc \ + package.json \ + package-lock.json \ + ./ +RUN mkdir -p "${TMP_DIR}/.npm" && \ + npm config set cache "${TMP_DIR}/.npm" --global && \ + mkdir -p "./jsapp/fonts" && \ + npm ci && \ + npm cache clean --force +# Build the frontend +COPY --chown=kobo:kobo test ./test +COPY --chown=kobo:kobo static ./static +COPY --chown=kobo:kobo webpack ./webpack +COPY --chown=kobo:kobo jsapp ./jsapp +COPY --chown=kobo:kobo \ + .browserslistrc \ + .babelrc.json \ + .swcrc \ + package.json \ + package-lock.json \ + tsconfig.json \ + ./ +RUN SKIP_TS_CHECK=true \ + npm run build:app + +###################### +# KPI dev/prod image # +###################### FROM ghcr.io/astral-sh/uv:python3.10-bookworm-slim ENV DEBIAN_FRONTEND=noninteractive @@ -103,6 +150,8 @@ COPY --from=build-python "$VIRTUAL_ENV" "$VIRTUAL_ENV" # Install `npm` packages. # ########################### +# TODO: copy from npm build stage + WORKDIR ${KPI_SRC_DIR}/ RUN rm -rf ${KPI_NODE_PATH} && \ @@ -121,6 +170,8 @@ ENV PATH=$PATH:${KPI_NODE_PATH}/.bin # Build client code. # ###################### +# TODO: copy from npm build stage + RUN npm run build:app ########################### diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 6cf69a6fec..9e4b5fde3b 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -66,6 +66,7 @@ if [[ ! -d "${KPI_SRC_DIR}/staticfiles" ]] || ! python "${KPI_SRC_DIR}/docker/ch # Create folder to be sure following `rsync` command does not fail mkdir -p "${KPI_SRC_DIR}/staticfiles" else + # TODO - revisit this in context of separate node build stage echo "Cleaning old build…" rm -rf "${KPI_SRC_DIR}/jsapp/fonts" && \ rm -rf "${KPI_SRC_DIR}/jsapp/compiled" diff --git a/package.json b/package.json index e96f868d0e..26d5b78e2e 100644 --- a/package.json +++ b/package.json @@ -245,7 +245,7 @@ "fix:css": "npm run lint:css -- --fix", "show-icons": "opn jsapp/fonts/k-icons.html", "generate-icons": "node ./scripts/generate_icons.js", - "copy-fonts": "python3 ./scripts/copy_fonts.py && npm run generate-icons", + "copy-fonts": "./scripts/copy_fonts.sh && npm run generate-icons", "hint": "node ./scripts/hints.js", "storybook": "npm run storybook:watch", "storybook:launch": "npx http-server storybook-static --port 6006 --silent", diff --git a/scripts/copy_fonts.py b/scripts/copy_fonts.py deleted file mode 100644 index a1a588078e..0000000000 --- a/scripts/copy_fonts.py +++ /dev/null @@ -1,35 +0,0 @@ -# coding: utf-8 -import glob -import os -import shutil - -dest_dir = "./jsapp/fonts/" - - -def create_folder_if_not_exists(): - if not os.path.exists(dest_dir): - try: - os.makedirs(dest_dir) - print("Destination folder has been created!") - except Exception as e: - print("Could not create fonts folder - Error: {}".format(str(e))) - - -def copy_fonts(): - - create_folder_if_not_exists() - - print("Copying fonts...") - - for file in glob.glob("./node_modules/@fontsource/roboto/files/roboto-latin-ext-*.wof*"): - print(file) - shutil.copy(file, dest_dir) - - for file in glob.glob("./node_modules/@fontsource/roboto-mono/files/roboto-mono-latin-ext-*.wof*"): - print(file) - shutil.copy(file, dest_dir) - - print("DONE") - - -copy_fonts() diff --git a/scripts/copy_fonts.sh b/scripts/copy_fonts.sh new file mode 100755 index 0000000000..2c9f95f3e0 --- /dev/null +++ b/scripts/copy_fonts.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -euo pipefail + +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) +cd "$script_dir/.." # go up above script folder + +dest_dir='./jsapp/fonts' + +# create folder if it doesn't exist +mkdir -p $dest_dir + +# copy fonts +shopt -s failglob +echo "Copying fonts from node_modules to jsapp/fonts..." +cp -v ./node_modules/@fontsource/roboto/files/roboto-latin-ext-*.wof* $dest_dir +cp -v ./node_modules/@fontsource/roboto-mono/files/roboto-mono-latin-ext-*.wof* $dest_dir +echo "DONE copying fonts."