Support environment variables in env.sh for Portainer deployment
All checks were successful
Build and Push Docker Image / docker (push) Successful in 50s
All checks were successful
Build and Push Docker Image / docker (push) Successful in 50s
- Add fallback to read from container environment variables - Maintains backward compatibility with .env file approach - Allows setting VITE_* vars directly in Portainer UI This enables flexible deployment: - File-based: Mount .env file (docker-compose) - Env-based: Set env vars in Portainer container settings 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
077deb4194
commit
17c7a8f580
38
env.sh
38
env.sh
@ -6,31 +6,43 @@
|
|||||||
# The directory where the static files are located
|
# The directory where the static files are located
|
||||||
ROOT_DIR=/usr/share/nginx/html
|
ROOT_DIR=/usr/share/nginx/html
|
||||||
|
|
||||||
# Check if .env file exists
|
# 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
|
if [ -f "$ROOT_DIR/.env" ]; then
|
||||||
echo "Loading environment variables..."
|
echo "Loading environment variables from .env file..."
|
||||||
|
|
||||||
# Create env-config.js with the environment variables
|
|
||||||
echo "window.env = {" > $ROOT_DIR/env-config.js
|
|
||||||
|
|
||||||
# Extract variables starting with VITE_ and add them to env-config.js
|
# Extract variables starting with VITE_ and add them to env-config.js
|
||||||
grep '^VITE_' $ROOT_DIR/.env | while read -r line; do
|
grep '^VITE_' $ROOT_DIR/.env | while read -r line; do
|
||||||
# Split the line into variable name and value
|
# Split the line into variable name and value
|
||||||
var_name=$(echo $line | cut -d '=' -f1)
|
var_name=$(echo $line | cut -d '=' -f1)
|
||||||
var_value=$(echo $line | cut -d '=' -f2-)
|
var_value=$(echo $line | cut -d '=' -f2-)
|
||||||
|
|
||||||
# Remove VITE_ prefix for the frontend variable name
|
# Remove VITE_ prefix for the frontend variable name
|
||||||
frontend_var_name=$(echo $var_name | sed 's/^VITE_//')
|
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
|
# Add the variable to env-config.js
|
||||||
echo " $frontend_var_name: \"$var_value\"," >> $ROOT_DIR/env-config.js
|
echo " $frontend_var_name: \"$var_value\"," >> $ROOT_DIR/env-config.js
|
||||||
done
|
done
|
||||||
|
|
||||||
# Close the JavaScript object
|
|
||||||
echo "};" >> $ROOT_DIR/env-config.js
|
|
||||||
|
|
||||||
echo "Environment variables loaded successfully."
|
|
||||||
fi
|
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)
|
# Execute the command passed to the script (usually start nginx)
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user