-
Notifications
You must be signed in to change notification settings - Fork 1
Read VITE config from runtime env in Docker image #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables runtime configuration of environment variables in Docker deployments by generating a config.js file at container startup, allowing the same Docker image to be configured differently across environments without rebuilding.
Key changes:
- Introduced
src/lib/env.tsmodule that reads environment variables fromwindow.__ENV__(runtime) with fallback toimport.meta.env(build-time) - Modified environment variable access throughout the codebase to use the new
envmodule - Added Docker entrypoint script to generate
config.jsfrom runtime environment variables
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib/env.ts | New module for runtime/build-time environment variable resolution with fallback logic |
| src/lib/api-client.ts | Updated to use centralized env module instead of direct import.meta.env access |
| src/keycloak.tsx | Updated to use centralized env module for Keycloak configuration |
| src/constants/meta.ts | Refactored to consume values from env module |
| public/config.js | Placeholder file that initializes window.ENV object |
| index.html | Added script tag to load config.js before main application |
| docker/nginx/templates/default.conf.template | Added no-cache header for config.js endpoint |
| docker/entrypoint.d/00-env-config.sh | Shell script to generate config.js from environment variables at container startup |
| Dockerfile | Added entrypoint script installation to Docker build |
| docker-compose.yml | Added VITE_API_MOCKING_ENABLED to environment variables list |
| .env.example | Added VITE_API_MOCKING_ENABLED example value |
Comments suppressed due to low confidence (1)
docker/entrypoint.d/00-env-config.sh:12
- The shell script generates JavaScript with unquoted string values, which could lead to JavaScript syntax errors if any environment variable contains special characters (like quotes, newlines, or backslashes).
For example, if VITE_API_BASE_URL contains a quote character, the generated JavaScript would be invalid. Consider using JSON.stringify-like escaping or a safer approach:
cat <<'EOF' > /usr/share/nginx/html/config.js
window.__ENV__ = {
VITE_API_BASE_URL: JSON.stringify(process.env.VITE_API_BASE_URL || ""),
// ... other variables
}
EOFOr use a more robust approach with proper escaping:
# Using jq or similar tool to safely generate JSON
cat > /usr/share/nginx/html/config.js <<EOF
window.__ENV__ = $(jq -n \
--arg api_url "${VITE_API_BASE_URL}" \
--arg api_mocking "${VITE_API_MOCKING_ENABLED}" \
--arg keycloak_url "${VITE_KEYCLOAK_URL}" \
--arg keycloak_realm "${VITE_KEYCLOAK_REALM}" \
--arg keycloak_client "${VITE_KEYCLOAK_CLIENT_ID}" \
--arg crowdin "${VITE_BITCR_DEV_INCLUDE_CROWDIN_IN_CONTEXT_TOOLING}" \
'{VITE_API_BASE_URL: $api_url, VITE_API_MOCKING_ENABLED: $api_mocking, VITE_KEYCLOAK_URL: $keycloak_url, VITE_KEYCLOAK_REALM: $keycloak_realm, VITE_KEYCLOAK_CLIENT_ID: $keycloak_client, VITE_BITCR_DEV_INCLUDE_CROWDIN_IN_CONTEXT_TOOLING: $crowdin}')
EOFcat <<EOF > /usr/share/nginx/html/config.js
window.__ENV__ = {
VITE_API_BASE_URL: "${VITE_API_BASE_URL}",
VITE_API_MOCKING_ENABLED: "${VITE_API_MOCKING_ENABLED}",
VITE_KEYCLOAK_URL: "${VITE_KEYCLOAK_URL}",
VITE_KEYCLOAK_REALM: "${VITE_KEYCLOAK_REALM}",
VITE_KEYCLOAK_CLIENT_ID: "${VITE_KEYCLOAK_CLIENT_ID}",
VITE_BITCR_DEV_INCLUDE_CROWDIN_IN_CONTEXT_TOOLING: "${VITE_BITCR_DEV_INCLUDE_CROWDIN_IN_CONTEXT_TOOLING}"
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Deploying wildcat-dashboard with
|
| Latest commit: |
e918e3d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://e26f83d2.wildcat-dashboard.pages.dev |
| Branch Preview URL: | https://cleot-enable-docker-env.wildcat-dashboard.pages.dev |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| cat <<EOF > /usr/share/nginx/html/config.js | ||
| window.__ENV__ = ${JSON_ENV}; | ||
| EOF |
Copilot
AI
Dec 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated config.js file is placed in /usr/share/nginx/html/config.js, but there's no verification that the file was successfully created or that the jq command succeeded. Consider adding error handling to ensure the config.js file is generated correctly. For example:
if [ ! -f /usr/share/nginx/html/config.js ]; then
echo "Error: Failed to generate config.js"
exit 1
fi| EOF | |
| EOF | |
| if [ ! -f /usr/share/nginx/html/config.js ]; then | |
| echo "Error: Failed to generate config.js" | |
| exit 1 | |
| fi |
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
User description
Generates config.js at startup
Test image deployed to clowder-wildcat0:
PR Type
Enhancement
Description
Enable runtime environment configuration in Docker containers
Create centralized env module with fallback to build-time values
Generate config.js at startup from Docker environment variables
Add comprehensive tests for runtime environment resolution
Diagram Walkthrough
File Walkthrough
5 files
New centralized environment resolution moduleMigrate to centralized env moduleUse centralized env for API base URLUse centralized env for Keycloak configLoad config.js before main application1 files
Comprehensive tests for runtime env resolution5 files
Placeholder for runtime environment configGenerate config.js from Docker environmentAdd jq and entrypoint scripts for env configDisable caching for config.js endpointPass additional env vars to container1 files
Add VITE_API_MOCKING_ENABLED example1 files
Export App component for testing