Skip to content

Commit

Permalink
Replace all environment variables instead of only API_URL (#5055)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinuslu authored and Cloud11PL committed Aug 2, 2024
1 parent 3ac0f1f commit 0b9296f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-carrots-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

You can now replace all environment variables instead of only API_URL in Docker and nginx
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ FROM nginx:stable-alpine as runner
WORKDIR /app

COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx/replace-api-url.sh /docker-entrypoint.d/50-replace-api-url.sh
COPY ./nginx/replace-env-vars.sh /docker-entrypoint.d/50-replace-env-vars.sh
COPY --from=builder /app/build/ /app/

LABEL org.opencontainers.image.title="saleor/saleor-dashboard" \
org.opencontainers.image.description="A GraphQL-powered, single-page dashboard application for Saleor." \
org.opencontainers.image.url="https://saleor.io/" \
org.opencontainers.image.source="https://github.com/saleor/saleor-dashboard" \
org.opencontainers.image.revision="$COMMIT_ID" \
org.opencontainers.image.version="$PROJECT_VERSION" \
org.opencontainers.image.authors="Saleor Commerce (https://saleor.io)" \
org.opencontainers.image.licenses="BSD 3"
LABEL \
org.opencontainers.image.title="saleor/saleor-dashboard" \
org.opencontainers.image.description="A GraphQL-powered, single-page dashboard application for Saleor." \
org.opencontainers.image.url="https://saleor.io/" \
org.opencontainers.image.source="https://github.com/saleor/saleor-dashboard" \
org.opencontainers.image.revision="$COMMIT_ID" \
org.opencontainers.image.version="$PROJECT_VERSION" \
org.opencontainers.image.authors="Saleor Commerce (https://saleor.io)" \
org.opencontainers.image.licenses="BSD 3"
28 changes: 27 additions & 1 deletion docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,31 @@ Enter `http://localhost:8080/` to use the dashboard.
If you want to change `API_URL` in runtime, you can use (assuming you have a running container named `saleor-dashboard`):

```shell
docker exec -it -e API_URL=NEW_URL saleor-dashboard /docker-entrypoint.d/50-replace-api-url.sh
docker exec -it -e API_URL=NEW_URL saleor-dashboard /docker-entrypoint.d/50-replace-env-vars.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_API_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 "IS_CLOUD_INSTANCE=NEW_IS_CLOUD_INSTANCE" \
-e "LOCALE_CODE=NEW_LOCALE_CODE" \
saleor-dashboard /docker-entrypoint.d/50-replace-env-vars.sh
```

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

```shell
docker run --publish 8080:80 \
-e "API_URL=NEW_API_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 "IS_CLOUD_INSTANCE=NEW_IS_CLOUD_INSTANCE" \
-e "LOCALE_CODE=NEW_LOCALE_CODE" \
saleor-dashboard
```
16 changes: 0 additions & 16 deletions nginx/replace-api-url.sh

This file was deleted.

30 changes: 30 additions & 0 deletions nginx/replace-env-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

# 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"

# 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
}

# Replace each environment variable
replace_env_var "API_URL"
replace_env_var "APP_MOUNT_URI"
replace_env_var "APPS_MARKETPLACE_API_URL"
replace_env_var "APPS_TUNNEL_URL_KEYWORDS"
replace_env_var "IS_CLOUD_INSTANCE"
replace_env_var "LOCALE_CODE"

echo "Environment variable replacement complete."

0 comments on commit 0b9296f

Please sign in to comment.