From 746cda468918b6efcd4afbf82227eb08c557b972 Mon Sep 17 00:00:00 2001 From: Aaron Louie Date: Sun, 24 May 2020 11:26:20 -0400 Subject: [PATCH] Finds and replaces base href template string with environment variable --- Dockerfile | 5 +++-- docker/substitute-env-variables.sh | 24 ++++++++++++++++++------ package.json | 4 ++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index e9e91fa..1fb1323 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ ADD package-lock.json /crc-bpmn/ COPY . /crc-bpmn/ -ARG build_config=staging +ARG build_config=production RUN npm install && \ npm run build:$build_config @@ -27,7 +27,8 @@ RUN chmod +x ./entrypoint.sh # Substitute environment variables in nginx configuration and index.html ENTRYPOINT ["./entrypoint.sh", \ "/usr/share/nginx/html/index.html,/etc/nginx/conf.d/default.conf", \ - "PRODUCTION,API_URL,IRB_URL,HOME_ROUTE,BASE_HREF,PORT0"] + "PRODUCTION,API_URL,IRB_URL,HOME_ROUTE,BASE_HREF,PORT0", \ + "/usr/share/nginx/html"] ### STAGE 3: Profit! ### CMD ["nginx", "-g", "daemon off;"] diff --git a/docker/substitute-env-variables.sh b/docker/substitute-env-variables.sh index 2fa36c1..57ef0a0 100755 --- a/docker/substitute-env-variables.sh +++ b/docker/substitute-env-variables.sh @@ -5,6 +5,7 @@ # Parameters: # $1: Comma-delimited list of file paths # $2: Comma-delimited list of environment variables +# $3: Absolute path to nginx html directory (optional) ##################################################################### echo 'Substituting environment variables...' @@ -21,11 +22,28 @@ if [[ -z $2 ]]; then exit 1 fi +# The third parameter is the absolute path to the nginx html directory +if [[ -z $2 ]]; then + echo 'ERROR: No path to nginx html directory given.' + exit 1 +fi + # Add trailing slash to $BASE_HREF if needed if [[ "$2" == *"BASE_HREF"* ]]; then length=${#BASE_HREF} last_char=${BASE_HREF:length-1:1} [[ $last_char == "/" ]] && BASE_HREF="$BASE_HREF"; : + + # The third parameter is the absolute path to the nginx html directory + if [[ $# -eq 3 ]]; then + # Replace all instances of __REPLACE_ME_WITH_BASE_HREF__ with $BASE_HREF + find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \ + xargs -0 sed -i 's@__REPLACE_ME_WITH_BASE_HREF__@'"$BASE_HREF"'@g' + + echo 'Replacing base href...' + # Wait a few seconds in case find | sed needs more time + sleep 3 + fi fi # Convert "VAR1,VAR2,VAR3,..." to "\$VAR1 \$VAR2 \$VAR3 ..." @@ -41,12 +59,6 @@ do # Wait a second in case envsubst needs more time sleep 1 - # If this is the index.html file, rewrite base href. - # Use @ as a sed delimiter because $BASE_HREF will contain a / character - if [[ $file_path == *"/index.html"* ]]; then - sed -i -e 's@@@' "$3" - fi - # If this is the nginx default.conf file, replace double slashes with single slashes if [[ $file_path == *"/default.conf"* ]]; then sed -i -e 's@//@/@g' "$file_path" diff --git a/package.json b/package.json index 62bdaab..4919da2 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "ng": "ng", "start": "ng serve", "build": "ng build", - "build:prod": "ng build --configuration=production", - "build:staging": "ng build --configuration=staging", + "build:prod": "ng build --configuration=production --prod --base-href=__REPLACE_ME_WITH_BASE_HREF__ --deploy-url=__REPLACE_ME_WITH_BASE_HREF__", + "build:staging": "ng build --configuration=staging --prod --base-href=__REPLACE_ME_WITH_BASE_HREF__ --deploy-url=__REPLACE_ME_WITH_BASE_HREF__", "build:test": "ng build --configuration=test", "test": "ng test", "test:coverage": "ng test --codeCoverage=true --watch=false --browsers=ChromeHeadless",