Skip to content
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

Replace all environment variables instead of only API_URL #5055

Merged
merged 13 commits into from
Aug 1, 2024
24 changes: 24 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,27 @@ If you want to change `API_URL` in runtime, you can use (assuming you have a run
```shell
docker exec -it -e API_URL=NEW_URL saleor-dashboard /docker-entrypoint.d/50-replace-api-url.sh
```

The replacement is not limited to `API_URL` only. You can also replace other environment variables in the same way.

```shell
docker exec -it \
-e API_URL=NEW_URL \
-e APP_MOUNT_URI=NEW_APP_MOUNT_URI \
-e APPS_MARKETPLACE_API_URL=NEW_APPS_MARKETPLACE_API_URL \
-e APPS_TUNNEL_URL_KEYWORDS=NEW_APPS_TUNNEL_URL_KEYWORDS \
-e STATIC_URL=NEW_STATIC_URL \
saleor-dashboard /docker-entrypoint.d/50-replace-api-url.sh
```

Of course you can also provide all the environment variables at the `docker run` command:

```shell
docker run --publish 8080:80 \
--env "API_URL=NEW_URL" \
--env "APP_MOUNT_URI=NEW_APP_MOUNT_URI" \
--env "APPS_MARKETPLACE_API_URL=NEW_APPS_MARKETPLACE_API_URL" \
--env "APPS_TUNNEL_URL_KEYWORDS=NEW_APPS_TUNNEL_URL_KEYWORDS" \
--env "STATIC_URL=NEW_STATIC_URL" \
saleor-dashboard
```
27 changes: 20 additions & 7 deletions nginx/replace-api-url.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job - works as it should - can you change the filename to replace-env-vars instead of replace-api-url.sh?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course

Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#!/usr/bin/env sh

# Replaces the API_URL from the bundle's index.html file with the $API_URL env var.
# Replaces environment variables in the bundle's index.html file with their respective values.
# This script is automatically picked up by the nginx entrypoint on startup.

set -e

INDEX_BUNDLE_PATH="/app/dashboard/index.html"

if [ -z "${API_URL}" ]; then
echo "No API_URL provided, using defaults."
else
echo "Setting API_URL to: $API_URL"
# Function to replace environment variables
replace_env_var() {
var_name=$1
var_value=$(eval echo \$$var_name)
if [ -n "$var_value" ]; then
echo "Setting $var_name to: $var_value"
sed -i "s#$var_name:.*#$var_name: \"$var_value\",#" "$INDEX_BUNDLE_PATH"
else
echo "No $var_name provided, using defaults."
fi
}

sed -i "s#API_URL:.*#API_URL: \"$API_URL\",#" "$INDEX_BUNDLE_PATH"
fi
# Replace each environment variable
replace_env_var "APP_MOUNT_URI"
replace_env_var "APPS_MARKETPLACE_API_URL"
replace_env_var "APPS_TUNNEL_URL_KEYWORDS"
replace_env_var "STATIC_URL"
replace_env_var "API_URL"

echo "Environment variable replacement complete."