Finds and replaces base href template string with environment variable

This commit is contained in:
Aaron Louie 2020-05-24 11:26:20 -04:00
parent f206aae9ad
commit 746cda4689
3 changed files with 23 additions and 10 deletions

View File

@ -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;"]

View File

@ -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@<base href\=\"\/\">@<base href\=\"'"$BASE_HREF"'\">@' "$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"

View File

@ -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",