Skip to content

Commit

Permalink
chore: simplify public env script (#157)
Browse files Browse the repository at this point in the history
* refactor: replace variable script

* fix: run script with bash to read array syntax
  • Loading branch information
rohan-chaturvedi authored Jan 22, 2024
1 parent fefe63b commit 84983a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 30 additions & 15 deletions frontend/scripts/replace-variable.sh
Original file line number Diff line number Diff line change
@@ -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
for VAR in "${ALL_VARS[@]}"; do
if [ ! -z "${!VAR}" ]; then
sed -i "s|BAKED_$VAR|${!VAR}|g" "$file"
fi
done
done
2 changes: 1 addition & 1 deletion frontend/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 84983a2

Please sign in to comment.