Reloads nginx after substituting environment variables

This commit is contained in:
Aaron Louie 2020-05-26 08:28:42 -04:00
parent 9918c872e0
commit 0728801a13
3 changed files with 56 additions and 32 deletions

View File

@ -14,7 +14,7 @@ RUN npm install && \
npm run build:$build_config npm run build:$build_config
### STAGE 2: Run ### ### STAGE 2: Run ###
FROM nginx:alpine as runtime FROM nginx:alpine
RUN set -x && apk add --update --no-cache bash libintl gettext curl RUN set -x && apk add --update --no-cache bash libintl gettext curl
COPY --from=builder /crc-bpmn/dist/* /etc/nginx/html/ COPY --from=builder /crc-bpmn/dist/* /etc/nginx/html/
@ -24,11 +24,14 @@ COPY --from=builder /crc-bpmn/nginx.conf /etc/nginx/conf.d/default.conf
COPY ./docker/substitute-env-variables.sh ./entrypoint.sh COPY ./docker/substitute-env-variables.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh RUN chmod +x ./entrypoint.sh
# Substitute environment variables in nginx configuration and index.html # The entrypoint.sh script will run after the container finishes starting.
# Substitutes environment variables in nginx configuration and index.html,
# then reloads nginx.
ENTRYPOINT ["./entrypoint.sh", \ ENTRYPOINT ["./entrypoint.sh", \
"/etc/nginx/html/index.html,/etc/nginx/conf.d/default.conf", \ "/etc/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", \
"/etc/nginx/html"] "/etc/nginx/html", \
"true"]
### STAGE 3: Profit! ### ### STAGE 3: Profit! ###
CMD ["nginx", "-s", "reload"] CMD ["nginx", "-g", "daemon off;"]

View File

@ -62,21 +62,21 @@ services:
- "5000:5000" - "5000:5000"
command: ./wait-for-it.sh pb:5001 -t 0 -- ./docker_run.sh command: ./wait-for-it.sh pb:5001 -t 0 -- ./docker_run.sh
# # Uncomment for debugging # Uncomment for debugging
# bpmn: bpmn:
# container_name: bpmn container_name: bpmn
# depends_on: depends_on:
# - db - db
# - pb - pb
# - ldap - ldap
# - backend - backend
# image: sartography/cr-connect-bpmn:$E2E_TAG image: sartography/cr-connect-bpmn:$E2E_TAG
# environment: environment:
# - API_URL=http://localhost:5000/v1.0 - API_URL=http://localhost:5000/v1.0
# - BASE_HREF=/bpmn - BASE_HREF=/bpmn
# - HOME_ROUTE=home - HOME_ROUTE=home
# - IRB_URL=http://localhost:5001/ - IRB_URL=http://localhost:5001/
# - PORT0=5002 - PORT0=5002
# - PRODUCTION=false - PRODUCTION=false
# ports: ports:
# - "5002:5002" - "5002:5002"

View File

@ -6,26 +6,40 @@
# $1: Comma-delimited list of file paths # $1: Comma-delimited list of file paths
# $2: Comma-delimited list of environment variables # $2: Comma-delimited list of environment variables
# $3: Absolute path to nginx html directory (optional) # $3: Absolute path to nginx html directory (optional)
# $4: Should restart nginx (optional)
##################################################################### #####################################################################
echo 'Substituting environment variables...' echo 'Substituting environment variables...'
num_args=0
# The first parameter is a comma-delimited list of paths to files which should be substituted # The first parameter is a comma-delimited list of paths to files which should be substituted
if [[ -z $1 ]]; then if [[ -z $1 ]]; then
echo 'ERROR: No target files given.' echo 'ERROR: No target files given.'
exit 1 exit 1
else
num_args=1
fi fi
# The second parameter is a comma-delimited list of environment variable names # The second parameter is a comma-delimited list of environment variable names
if [[ -z $2 ]]; then if [[ -z $2 ]]; then
echo 'ERROR: No environment variables given.' echo 'ERROR: No environment variables given.'
exit 1 exit 1
else
num_args=2
fi fi
# The third parameter is the absolute path to the nginx html directory # The third parameter is the absolute path to the nginx html directory
if [[ -z $2 ]]; then if [[ -z $3 ]]; then
echo 'ERROR: No path to nginx html directory given.' echo '' # It's optional. Don't print anything.
exit 1 else
num_args=3
fi
# The fourth parameter, if 'true', is whether we should reload nginx
if [[ -z $4 ]]; then
echo '' # It's optional. Don't print anything.
else
num_args=4
fi fi
# Add trailing slash to $BASE_HREF if needed # Add trailing slash to $BASE_HREF if needed
@ -35,7 +49,7 @@ if [[ "$2" == *"BASE_HREF"* ]]; then
[[ $last_char != "/" ]] && BASE_HREF="$BASE_HREF/"; : [[ $last_char != "/" ]] && BASE_HREF="$BASE_HREF/"; :
# The third parameter is the absolute path to the nginx html directory # The third parameter is the absolute path to the nginx html directory
if [[ $# -ge 3 ]]; then if [[ $num_args -ge 3 ]]; then
# Replace all instances of __REPLACE_ME_WITH_BASE_HREF__ with $BASE_HREF # Replace all instances of __REPLACE_ME_WITH_BASE_HREF__ with $BASE_HREF
find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \ find "$3" \( -type d -name .git -prune \) -o -type f -print0 | \
xargs -0 sed -i 's@__REPLACE_ME_WITH_BASE_HREF__@'"$BASE_HREF"'@g' xargs -0 sed -i 's@__REPLACE_ME_WITH_BASE_HREF__@'"$BASE_HREF"'@g'
@ -71,11 +85,18 @@ do
echo "$env_var = ${!env_var}" echo "$env_var = ${!env_var}"
done done
# Execute all other commands with parameters # Reload nginx
if [[ $# -gt 3 ]]; then if [ $num_args -ge 4 ] && [ "$4" == "true" ]; then
exec "${@:4}" # Check to see if nginx command is available
else if hash nginx 2> /dev/null; then
if [[ $# -gt 2 ]]; then echo "Reloading nginx..."
exec "${@:3}" exec nginx -s reload
echo "nginx reloaded."
else
echo "nginx command not found on this system."
fi fi
fi fi
# Execute all other commands with parameters
num_args=$((num_args + 1))
exec "${@:num_args}"