diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 4f80563c..3151011c 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -4,7 +4,7 @@ FROM node:alpine AS base WORKDIR /app # install system dependencies -RUN apk add curl +RUN apk add curl bash # Add a new user "app" and change ownership of the /app directory RUN addgroup app && adduser -S -G app app && chown -R app:app /app diff --git a/frontend/scripts/replace-variable.sh b/frontend/scripts/replace-variable.sh index 0d49b052..21a5fb68 100644 --- a/frontend/scripts/replace-variable.sh +++ b/frontend/scripts/replace-variable.sh @@ -1,22 +1,37 @@ #!/bin/bash +# replace-variables.sh -# Ensure NEXT_PUBLIC_BACKEND_API_BASE and NEXT_PUBLIC_NEXTAUTH_PROVIDERS are set -if [ -z "$NEXT_PUBLIC_BACKEND_API_BASE" ]; then - echo "NEXT_PUBLIC_BACKEND_API_BASE is not set. Please set it and rerun the script." - exit 1 -fi +# Define a list of mandatory environment variables to check +MANDATORY_VARS=("NEXT_PUBLIC_BACKEND_API_BASE" "NEXT_PUBLIC_NEXTAUTH_PROVIDERS") + +# Define a list of optional environment variables (no check needed) +OPTIONAL_VARS=("APP_HOST" "NEXT_PUBLIC_POSTHOG_KEY" "NEXT_PUBLIC_POSTHOG_HOST" "NEXT_PUBLIC_GITHUB_INTEGRATION_CLIENT_ID") -if [ -z "$NEXT_PUBLIC_NEXTAUTH_PROVIDERS" ]; then - echo "NEXT_PUBLIC_NEXTAUTH_PROVIDERS is not set. Please set it and rerun the script." - exit 1 +# Infer NEXT_PUBLIC_APP_HOST from APP_HOST if not already set +if [ -z "$NEXT_PUBLIC_APP_HOST" ] && [ ! -z "$APP_HOST" ]; then + export NEXT_PUBLIC_APP_HOST="$APP_HOST" fi +# Check if each mandatory variable is set +for VAR in "${MANDATORY_VARS[@]}"; do + if [ -z "${!VAR}" ]; then + echo "$VAR is not set. Please set it and rerun the script." + exit 1 + fi +done + +# Combine mandatory and optional variables for replacement +ALL_VARS=("${MANDATORY_VARS[@]}" "${OPTIONAL_VARS[@]}") + +# Add NEXT_PUBLIC_APP_HOST to the list for replacement +ALL_VARS+=("NEXT_PUBLIC_APP_HOST") + +# Find and replace BAKED values with real values find /app/public /app/.next -type f -name "*.js" | while read file; do - sed -i "s|BAKED_NEXT_PUBLIC_BACKEND_API_BASE|$NEXT_PUBLIC_BACKEND_API_BASE|g" "$file" - sed -i "s|BAKED_NEXT_PUBLIC_NEXTAUTH_PROVIDERS|$NEXT_PUBLIC_NEXTAUTH_PROVIDERS|g" "$file" - sed -i "s|BAKED_NEXT_PUBLIC_APP_HOST|$APP_HOST|g" "$file" - sed -i "s|BAKED_NEXT_PUBLIC_POSTHOG_KEY|$NEXT_PUBLIC_POSTHOG_KEY|g" "$file" - sed -i "s|BAKED_NEXT_PUBLIC_POSTHOG_HOST|$NEXT_PUBLIC_POSTHOG_HOST|g" "$file" - sed -i "s|BAKED_NEXT_PUBLIC_GITHUB_INTEGRATION_CLIENT_ID|$GITHUB_INTEGRATION_CLIENT_ID|g" "$file" -done \ No newline at end of file + for VAR in "${ALL_VARS[@]}"; do + if [ ! -z "${!VAR}" ]; then + sed -i "s|BAKED_$VAR|${!VAR}|g" "$file" + fi + done +done diff --git a/frontend/scripts/start.sh b/frontend/scripts/start.sh index 1bede6ce..11704311 100644 --- a/frontend/scripts/start.sh +++ b/frontend/scripts/start.sh @@ -1,5 +1,5 @@ #!/bin/sh # Set up runtime env vars and start next server -sh scripts/replace-variable.sh && +bash scripts/replace-variable.sh && yarn start