From 6701e3bd7770a6e53fce1d512be9675996082cfd Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:47:17 -0700 Subject: [PATCH] Use HOMEPAGE_BUILDTIME to revalidate container restart/recreate --- Dockerfile | 2 +- docker-entrypoint.sh | 2 ++ src/pages/api/hash.js | 4 ++-- src/utils/config/config.js | 12 ++++++------ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1d9a49317..9fec35e70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,7 @@ COPY . . SHELL ["/bin/ash", "-xeo", "pipefail", "-c"] RUN npm run telemetry \ - && mkdir config && echo '---' > config/settings.yaml \ + && mkdir config \ && NEXT_PUBLIC_BUILDTIME=$BUILDTIME NEXT_PUBLIC_VERSION=$VERSION NEXT_PUBLIC_REVISION=$REVISION npm run build # Production image, copy all the files and run next diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index bf443461f..c28588081 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -10,6 +10,8 @@ export PGID=${PGID:-0} # while also supporting the lscr.io /config directory [ ! -d "/app/config" ] && ln -s /config /app/config +export HOMEPAGE_BUILDTIME=$(date +%s) + # Set privileges for /app but only if pid 1 user is root and we are dropping privileges. # If container is run as an unprivileged user, it means owner already handled ownership setup on their own. # Running chown in that case (as non-root) will cause error diff --git a/src/pages/api/hash.js b/src/pages/api/hash.js index fac3ad05e..963691349 100644 --- a/src/pages/api/hash.js +++ b/src/pages/api/hash.js @@ -19,8 +19,8 @@ export default async function handler(req, res) { return hash(readFileSync(configYaml, "utf8")); }); - // this ties hash to specific build which should force revaliation between versions - const buildTime = process.env.NEXT_PUBLIC_BUILDTIME?.length ? process.env.NEXT_PUBLIC_BUILDTIME : ''; + // set to date by docker entrypoint, will force revalidation between restarts/recreates + const buildTime = process.env.HOMEPAGE_BUILDTIME?.length ? process.env.HOMEPAGE_BUILDTIME : ''; const combinedHash = hash(hashes.join("") + buildTime); diff --git a/src/utils/config/config.js b/src/utils/config/config.js index 1644917ea..f312560d5 100644 --- a/src/utils/config/config.js +++ b/src/utils/config/config.js @@ -1,6 +1,6 @@ /* eslint-disable no-console */ import { join } from "path"; -import { existsSync, copyFile, readFileSync } from "fs"; +import { existsSync, readFileSync, copyFileSync } from "fs"; import cache from "memory-cache"; import yaml from "js-yaml"; @@ -13,13 +13,13 @@ export default function checkAndCopyConfig(config) { const configYaml = join(process.cwd(), "config", config); if (!existsSync(configYaml)) { const configSkeleton = join(process.cwd(), "src", "skeleton", config); - copyFile(configSkeleton, configYaml, (err) => { - if (err) { + try { + copyFileSync(configSkeleton, configYaml) + console.info("%s was copied to the config folder", config); + } catch (err) { console.error("error copying config", err); throw err; - } - console.info("%s was copied to the config folder", config); - }); + } return true; }