Files
romm/docker/init_scripts/docker-entrypoint.sh
Michael Manganiello 98254d50b8 misc: Use nginx templates to allow for environment variable usage
Using the `envsubst` command, we can replace environment variables in
the nginx template files. This allows for more flexibility when
configuring the nginx server.

The Docker image we use as base for Nginx does provide the
`20-envsubst-on-templates.sh` script that will replace environment
variables in the template files.

This change does not include any behavior change, but unblocks future
changes that require environment variables in the nginx configuration.
2025-02-18 22:36:51 -03:00

40 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Load environment variables from variants with a _FILE suffix.
# The following logic reads variables with a _FILE suffix and
# loads the contents of the file specified in the variable
# into the variable without the suffix.
for var_name in $(printenv | cut -d= -f1 | grep "_FILE$" || true); do
# If variable is empty, skip.
if [[ -z ${!var_name} ]]; then
continue
fi
var_name_no_suffix=${var_name%"_FILE"}
# If the variable without the suffix is already set, raise an error.
if [[ -n ${!var_name_no_suffix} ]]; then
echo "ERROR: Both ${var_name_no_suffix} and ${var_name} are set (but are exclusive)" >&2
exit 1
fi
file_path="${!var_name}"
# If file does not exist, raise an error.
if [[ ! -f ${file_path} ]]; then
echo "ERROR: File ${file_path} from ${var_name} does not exist" >&2
exit 1
fi
echo "Setting ${var_name_no_suffix} from ${var_name} at ${file_path}"
export "${var_name_no_suffix}"="$(cat "${file_path}")"
# Unset the _FILE variable.
unset "${var_name}"
done
# Replace environment variables used in nginx configuration templates.
/docker-entrypoint.d/20-envsubst-on-templates.sh >/dev/null
exec "$@"