Ugh. envsubst doesn't do exact string match. Throw in extra slash, then remove it afterward. UGH.

This commit is contained in:
Aaron Louie 2020-05-23 23:55:43 -04:00
parent 972be2e1e1
commit d954f2620d
3 changed files with 19 additions and 22 deletions

View File

@ -27,8 +27,7 @@ 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", \
"/usr/share/nginx/html/index.html"]
"PRODUCTION,API_URL,IRB_URL,HOME_ROUTE,BASE_HREF,PORT0"]
### STAGE 3: Profit! ###
CMD ["nginx", "-g", "daemon off;"]

View File

@ -5,7 +5,6 @@
# Parameters:
# $1: Comma-delimited list of file paths
# $2: Comma-delimited list of environment variables
# $3: File path to index.html (optional)
#####################################################################
echo 'Substituting environment variables...'
@ -26,7 +25,7 @@ fi
if [[ "$2" == *"BASE_HREF"* ]]; then
length=${#BASE_HREF}
last_char=${BASE_HREF:length-1:1}
[[ $last_char != "/" ]] && BASE_HREF="$BASE_HREF/"; :
[[ $last_char == "/" ]] && BASE_HREF="$BASE_HREF"; :
fi
# Convert "VAR1,VAR2,VAR3,..." to "\$VAR1 \$VAR2 \$VAR3 ..."
@ -39,6 +38,19 @@ do
envsubst "$env_list" < "$file_path" > "$file_path".tmp && mv "$file_path".tmp "$file_path"
echo '...'
# 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"
fi
done
echo 'Finished substituting environment variables.'
@ -47,19 +59,5 @@ do
echo "$env_var = ${!env_var}"
done
# The third parameter is the path to the index.html file
# Rewrite base href in index.html.
# Use @ as a sed delimiter because $BASE_HREF will contain a / character
if [[ -z $3 ]]; then
# Execute all other commands with parameters
exit 0
else
# Wait a second in case envsubst needs more time
sleep 1
sed -i -e 's@<base href\=\"\/\">@<base href\=\"'"$BASE_HREF"'\">@' "$3"
# Execute all other commands with parameters
exec "${@:4}"
fi
# Execute all other commands with parameters
exec "${@:3}"

View File

@ -3,9 +3,9 @@ server {
root /usr/share/nginx/html;
index index.html index.htm;
location $BASE_HREF {
location $BASE_HREF/ {
alias /usr/share/nginx/html;
try_files $uri$args $uri$args/ $BASE_HREFindex.html =404;
try_files $uri$args $uri$args/ $BASE_HREF/index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;