From 72005db13de50d8a66c22db2c130342848878c36 Mon Sep 17 00:00:00 2001 From: Connery Noble Date: Thu, 29 Sep 2022 12:31:58 -0700 Subject: [PATCH] Support both build-time and run-time values for WEBAPP_URL. Improve re-build time, if just the WEBAPP_URL is changed. --- Dockerfile | 13 ++++++++++--- README.md | 3 ++- scripts/replace-placeholder.sh | 16 ++++++++++++++++ scripts/replace-placeholders.sh | 5 ----- scripts/start.sh | 5 ++++- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100755 scripts/replace-placeholder.sh delete mode 100755 scripts/replace-placeholders.sh diff --git a/Dockerfile b/Dockerfile index 01e8a85..fa5b57e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,9 +8,7 @@ ARG NEXTAUTH_SECRET=secret ARG CALENDSO_ENCRYPTION_KEY=secret ARG MAX_OLD_SPACE_SIZE=4096 -# PLACEHOLDER value is compiled in during build, but will be replaced it at runtime -# see: scripts/replace-placeholders.sh -ENV NEXT_PUBLIC_WEBAPP_URL=https://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER \ +ENV NEXT_PUBLIC_WEBAPP_URL=http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER \ NEXT_PUBLIC_LICENSE_CONSENT=$NEXT_PUBLIC_LICENSE_CONSENT \ CALCOM_TELEMETRY_DISABLED=$CALCOM_TELEMETRY_DISABLED \ DATABASE_URL=$DATABASE_URL \ @@ -30,6 +28,8 @@ RUN yarn build FROM node:16 as runner WORKDIR /calcom +ARG NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 + ENV NODE_ENV production RUN apt-get update && \ @@ -44,5 +44,12 @@ COPY --from=builder /calcom/apps/web ./apps/web COPY --from=builder /calcom/packages/prisma/schema.prisma ./prisma/schema.prisma COPY scripts scripts +# Save value used during this build stage. If NEXT_PUBLIC_WEBAPP_URL and BUILT_NEXT_PUBLIC_WEBAPP_URL differ at +# run-time, then start.sh will find/replace static values again. +ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \ + BUILT_NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL + +RUN scripts/replace-placeholder.sh http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER ${NEXT_PUBLIC_WEBAPP_URL} + EXPOSE 3000 CMD ["/calcom/scripts/start.sh"] diff --git a/README.md b/README.md index bfdd58d..c921788 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Updating these variables is not required for evaluation, but is required for run | Variable | Description | Required | Default | | --- | --- | --- | --- | -| NEXT_PUBLIC_WEBAPP_URL | Base URL injected into static files | required | `http://localhost:3000` | +| NEXT_PUBLIC_WEBAPP_URL | Base URL injected into static files | optional | `http://localhost:3000` | | NEXT_PUBLIC_LICENSE_CONSENT | license consent - true/false | | | | CALCOM_TELEMETRY_DISABLED | Allow cal.com to collect anonymous usage data (set to `1` to disable) | | | | DATABASE_URL | database url with credentials | required | `postgresql://unicorn_user:magical_password@database:5432/calendso` | @@ -125,6 +125,7 @@ These variables must also be provided at runtime | Variable | Description | Required | Default | | --- | --- | --- | --- | +| NEXT_PUBLIC_WEBAPP_URL | Base URL of the site. NOTE: if this value differs from the value used at build-time, there will be a slight delay during container start (to update the statically built files). | optional | `http://localhost:3000` | | CALCOM_LICENSE_KEY | Enterprise License Key | | | | NEXTAUTH_SECRET | must match build variable | required | `secret` | | CALENDSO_ENCRYPTION_KEY | must match build variable | required | `secret` | diff --git a/scripts/replace-placeholder.sh b/scripts/replace-placeholder.sh new file mode 100755 index 0000000..57dbabb --- /dev/null +++ b/scripts/replace-placeholder.sh @@ -0,0 +1,16 @@ +FROM=$1 +TO=$2 + +if [ "${FROM}" = "${TO}" ]; then + echo "Nothing to replace, the value is already set to ${TO}." + + exit 0 +fi + +# Only peform action if $FROM and $TO are different. +echo "Replacing all statically built instances of $FROM with $TO." + +find apps/web/.next/ apps/web/public -type f | +while read file; do + sed -i "s|$FROM|$TO|g" "$file" +done diff --git a/scripts/replace-placeholders.sh b/scripts/replace-placeholders.sh deleted file mode 100755 index 272b4d0..0000000 --- a/scripts/replace-placeholders.sh +++ /dev/null @@ -1,5 +0,0 @@ -# Find and replace placeholder values with runtime-specific environment values -find apps/web/.next/ apps/web/public -type f | -while read file; do - sed -i "s|https://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER|$NEXT_PUBLIC_WEBAPP_URL|g" "$file" -done diff --git a/scripts/start.sh b/scripts/start.sh index 52e818c..d92bf2e 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,7 +1,10 @@ #!/bin/sh set -x -scripts/replace-placeholders.sh +# Replace the statically built BUILT_NEXT_PUBLIC_WEBAPP_URL with run-time NEXT_PUBLIC_WEBAPP_URL +# NOTE: if these values are the same, this will be skipped. +scripts/replace-placeholder.sh "$BUILT_NEXT_PUBLIC_WEBAPP_URL" "$NEXT_PUBLIC_WEBAPP_URL" + scripts/wait-for-it.sh ${DATABASE_HOST} -- echo "database is up" npx prisma migrate deploy --schema /calcom/packages/prisma/schema.prisma npx ts-node --transpile-only /calcom/packages/prisma/seed-app-store.ts