diff --git a/Dockerfile b/Dockerfile index 47fa8e148..bc9a6d711 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ### Dev Stage -FROM openmrs/openmrs-core:dev as dev +FROM --platform=$BUILDPLATFORM openmrs/openmrs-core:dev as dev WORKDIR /openmrs_distro ARG MVN_ARGS_SETTINGS="-s /usr/share/maven/ref/settings-docker.xml -U" @@ -25,7 +25,7 @@ RUN mvn $MVN_ARGS_SETTINGS clean ### Run Stage # Replace 'nightly' with the exact version of openmrs-core built for production (if available) -FROM openmrs/openmrs-core:nightly +FROM --platform=$BUILDPLATFORM openmrs/openmrs-core:nightly # Do not copy the war if using the correct openmrs-core image version COPY --from=dev /openmrs/distribution/openmrs_core/openmrs.war /openmrs/distribution/openmrs_core/ diff --git a/frontend/Dockerfile b/frontend/Dockerfile index afbe7e8ae..aaef7c97f 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,8 +1,9 @@ # syntax=docker/dockerfile:1.3 -FROM --platform=$BUILDPLATFORM node:18-alpine as dev - +ARG NODE_VERSION=18 ARG APP_SHELL_VERSION=next +FROM --platform=$BUILDPLATFORM node:${NODE_VERSION}-alpine as dev + RUN mkdir -p /app WORKDIR /app @@ -11,23 +12,38 @@ COPY spa-build-config.json . ARG CACHE_BUST RUN npx --legacy-peer-deps openmrs@${APP_SHELL_VERSION:-next} assemble --manifest --mode config --config spa-build-config.json --target ./spa RUN npx --legacy-peer-deps openmrs@${APP_SHELL_VERSION:-next} build --build-config spa-build-config.json --target ./spa -RUN if [ ! -f ./spa/index.html ]; then echo 'Build failed. Please check the logs above for details. This may have happened because of an update to a library that OpenMRS depends on.'; exit 1; fi +RUN if [ ! -f ./spa/index.html ]; then echo 'Build failed. Please check the logs above for details. This may have happened because of an update to a library that OpenMRS depends on.' >&2 ; exit 1; fi -FROM nginx:1.25-alpine +FROM --platform=$BUILDPLATFORM nginx:1.25-alpine RUN apk update && \ apk upgrade && \ # add more utils for sponge to support our startup script - apk add --no-cache moreutils + apk add --no-cache moreutils perl-utils # clear any default files installed by nginx RUN rm -rf /usr/share/nginx/html/* +ENV APP_SHELL_VERSION ${APP_SHELL_VERSION:-next} + +# the default nginx image doesn't have Node or NPM, so we copy it from the dev image +COPY --from=dev /usr/lib /usr/lib +COPY --from=dev /usr/local/lib /usr/local/lib +COPY --from=dev /usr/local/include /usr/local/include +COPY --from=dev /usr/local/bin /usr/local/bin + COPY startup.sh /usr/local/bin/startup.sh RUN chmod +x /usr/local/bin/startup.sh COPY nginx.conf /etc/nginx/nginx.conf +# allow updating by overwriting spa-build-config.json +RUN mkdir -p /openmrs +COPY --from=dev /app/spa-build-config.json /openmrs/spa-build-config.json +WORKDIR /openmrs +RUN shasum -a 512 spa-build-config.json | tee spa-build-config.json.sha512sum +WORKDIR / + COPY --from=dev /app/spa /usr/share/nginx/html CMD ["/usr/local/bin/startup.sh"] diff --git a/frontend/startup.sh b/frontend/startup.sh index 7d170e4cf..98f1b35e6 100644 --- a/frontend/startup.sh +++ b/frontend/startup.sh @@ -1,6 +1,32 @@ #!/bin/sh set -e +rebuild_frontend() { + npx --legacy-peer-deps openmrs@${APP_SHELL_VERSION:-next} assemble --manifest --mode config --config spa-build-config.json --target ./spa + npx --legacy-peer-deps openmrs@${APP_SHELL_VERSION:-next} build --build-config spa-build-config.json --target ./spa + # we exit 0 below so that build failures do not stop the image + if [ ! -f ./spa/index.html ]; then echo 'Rebuild failed. Please check the logs above for details. This may have happened because of an update to a library that OpenMRS depends on.' >&2; popd; exit 0; fi + shasum -a 512 spa-build-config.json > spa-build-config.json.sha512sum + + rm -rf /usr/share/nginx/html/* + cp -r spa/* /usr/share/nginx/html +} + +if [ -d "/openmrs" ]; then + if [ -f "/openmrs/spa-build-config.json" ]; then + if [ -f "/openmrs/spa-build-config.json.sha512sum" ]; then + cd "/openmrs" + shasum -a 512 -c /openmrs/spa-build-config.json.sha512sum > /dev/null || { + echo "Checksum mismatch. Rebuilding frontend..." + rebuild_frontend + } + else + echo "Checksum file not found. Rebuilding frontend..." + rebuild_frontend + fi + fi +fi + # if we are using the $IMPORTMAP_URL environment variable, we have to make this useful, # so we change "importmap.json" into "$IMPORTMAP_URL" allowing it to be changed by envsubst if [ -n "${IMPORTMAP_URL}" ]; then diff --git a/gateway/Dockerfile b/gateway/Dockerfile index 3cf1bc00e..7e137ad34 100644 --- a/gateway/Dockerfile +++ b/gateway/Dockerfile @@ -1,5 +1,5 @@ # syntax=docker/dockerfile:1.3 -FROM nginx:1.25-alpine +FROM --platform=$BUILDPLATFORM nginx:1.25-alpine ENV FRAME_ANCESTORS ""