puffin-app/env.sh
Matt 04bfef4391
All checks were successful
Build and Push Docker Images / docker (push) Successful in 46s
Fix frontend runtime environment configuration
CRITICAL FIX:
- Frontend now loads environment variables at runtime instead of build time
- This allows changing configuration without rebuilding the Docker image

CHANGES:
- Add env-config.js script loader to index.html
- Update env.sh to use correct path for serve (/app/dist)
- Update Dockerfile to run env.sh before starting serve
- Add VITE_STRIPE_PUBLISHABLE_KEY to docker-compose environment

HOW IT WORKS:
1. env.sh reads VITE_* variables from container environment
2. Generates /app/dist/env-config.js with window.env object
3. index.html loads env-config.js before React app
4. src/utils/config.ts reads from window.env at runtime

This fixes the "Missing required environment variable: WREN_API_TOKEN" error
because the frontend can now access environment variables passed to the container.

🔒 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-30 12:30:29 +01:00

49 lines
1.7 KiB
Bash

#!/bin/sh
# Script to dynamically replace environment variables in the static files
# This allows updating env variables without rebuilding the container
# The directory where the static files are located (serve uses /app/dist)
ROOT_DIR=/app/dist
# Create env-config.js with the environment variables
echo "window.env = {" > $ROOT_DIR/env-config.js
# First, check if .env file exists and read from it
if [ -f "$ROOT_DIR/.env" ]; then
echo "Loading environment variables from .env file..."
# Extract variables starting with VITE_ and add them to env-config.js
grep '^VITE_' $ROOT_DIR/.env | while read -r line; do
# Split the line into variable name and value
var_name=$(echo $line | cut -d '=' -f1)
var_value=$(echo $line | cut -d '=' -f2-)
# Remove VITE_ prefix for the frontend variable name
frontend_var_name=$(echo $var_name | sed 's/^VITE_//')
# Add the variable to env-config.js
echo " $frontend_var_name: \"$var_value\"," >> $ROOT_DIR/env-config.js
done
else
echo "No .env file found, checking container environment variables..."
# Fallback to container environment variables
# Loop through all environment variables starting with VITE_
env | grep '^VITE_' | while IFS='=' read -r var_name var_value; do
# Remove VITE_ prefix for the frontend variable name
frontend_var_name=$(echo $var_name | sed 's/^VITE_//')
# Add the variable to env-config.js
echo " $frontend_var_name: \"$var_value\"," >> $ROOT_DIR/env-config.js
done
fi
# Close the JavaScript object
echo "};" >> $ROOT_DIR/env-config.js
echo "Environment variables loaded successfully."
# Execute the command passed to the script (usually start nginx)
exec "$@"