From 16f34ecd4ddcf2299253651c02fd80b094f7a3bc Mon Sep 17 00:00:00 2001 From: Yasin Uslu Date: Thu, 1 Aug 2024 14:47:10 +0300 Subject: [PATCH] Replace all environment variables instead of only API_URL (#5055) --- .changeset/brown-carrots-search.md | 5 +++++ Dockerfile | 19 ++++++++++--------- docs/docker.md | 28 +++++++++++++++++++++++++++- nginx/replace-api-url.sh | 16 ---------------- nginx/replace-env-vars.sh | 30 ++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 .changeset/brown-carrots-search.md delete mode 100755 nginx/replace-api-url.sh create mode 100755 nginx/replace-env-vars.sh diff --git a/.changeset/brown-carrots-search.md b/.changeset/brown-carrots-search.md new file mode 100644 index 00000000000..4d380c2f40c --- /dev/null +++ b/.changeset/brown-carrots-search.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +You can now replace all environment variables instead of only API_URL in Docker and nginx diff --git a/Dockerfile b/Dockerfile index 160b2c01268..f0c0959ba57 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" diff --git a/docs/docker.md b/docs/docker.md index acef71fb256..4aaaedb75f4 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -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 ``` diff --git a/nginx/replace-api-url.sh b/nginx/replace-api-url.sh deleted file mode 100755 index 07aa5cbc640..00000000000 --- a/nginx/replace-api-url.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env sh - -# Replaces the API_URL from the bundle's index.html file with the $API_URL env var. -# 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" - - sed -i "s#API_URL:.*#API_URL: \"$API_URL\",#" "$INDEX_BUNDLE_PATH" -fi diff --git a/nginx/replace-env-vars.sh b/nginx/replace-env-vars.sh new file mode 100755 index 00000000000..57cf63c6131 --- /dev/null +++ b/nginx/replace-env-vars.sh @@ -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."