2025-05-13 18:50:30 +02:00
|
|
|
#!/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
|
|
|
|
|
ROOT_DIR=/usr/share/nginx/html
|
|
|
|
|
|
2025-10-29 14:52:39 +01:00
|
|
|
# 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
|
2025-05-13 18:50:30 +02:00
|
|
|
if [ -f "$ROOT_DIR/.env" ]; then
|
2025-10-29 14:52:39 +01:00
|
|
|
echo "Loading environment variables from .env file..."
|
|
|
|
|
|
2025-05-13 18:50:30 +02:00
|
|
|
# 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-)
|
2025-10-29 14:52:39 +01:00
|
|
|
|
2025-05-13 18:50:30 +02:00
|
|
|
# Remove VITE_ prefix for the frontend variable name
|
|
|
|
|
frontend_var_name=$(echo $var_name | sed 's/^VITE_//')
|
2025-10-29 14:52:39 +01:00
|
|
|
|
|
|
|
|
# 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_//')
|
|
|
|
|
|
2025-05-13 18:50:30 +02:00
|
|
|
# Add the variable to env-config.js
|
|
|
|
|
echo " $frontend_var_name: \"$var_value\"," >> $ROOT_DIR/env-config.js
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
2025-10-29 14:52:39 +01:00
|
|
|
# Close the JavaScript object
|
|
|
|
|
echo "};" >> $ROOT_DIR/env-config.js
|
|
|
|
|
|
|
|
|
|
echo "Environment variables loaded successfully."
|
|
|
|
|
|
2025-05-13 18:50:30 +02:00
|
|
|
# Execute the command passed to the script (usually start nginx)
|
|
|
|
|
exec "$@"
|