feat: merge .env and .env.custom file during installation (#3564)

Closes https://github.com/getsentry/self-hosted/issues/3558
This commit is contained in:
Reinaldy Rafli 2025-02-04 19:41:12 +07:00 committed by GitHub
parent cfb0491f63
commit ee3cbf0f91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
# This is a test file for a part of `_lib.sh`, where we read `.env.custom` file if there is one.
# We only want to give very minimal value to the `.env.custom` file, and expect that it would
# be merged with the original `.env` file, with the `.env.custom` file taking precedence.
cat <<EOF >".env.custom"
SENTRY_EVENT_RETENTION_DAYS=10
EOF
# The `_test_setup.sh` script sources `install/_lib.sh`, so.. finger crossed this should works.
source _unit-test/_test_setup.sh
rm -f .env.custom
echo "Expecting SENTRY_EVENT_RETENTION_DAYS to be 10, got ${SENTRY_EVENT_RETENTION_DAYS}"
test "$SENTRY_EVENT_RETENTION_DAYS" == "10"
echo "Pass"
echo "Expecting SENTRY_BIND to be 9000, got ${SENTRY_BIND}"
test "$SENTRY_BIND" == "9000"
echo "Pass"
echo "Expecting COMPOSE_PROJECT_NAME to be sentry-self-hosted, got ${COMPOSE_PROJECT_NAME}"
test "$COMPOSE_PROJECT_NAME" == "sentry-self-hosted"
echo "Pass"
report_success

View File

@ -16,8 +16,16 @@ else
_ENV=.env
fi
# Reading .env.custom has to come first. The value won't be overriden, instead
# it would persist because of `export -p> >"$t"` later, which exports current
# environment variables to a temporary file with a `declare -x KEY=value` format.
# The new values on `.env` would be set only if they are not already set.
if [[ "$_ENV" == ".env.custom" ]]; then
q=$(mktemp) && export -p >"$q" && set -a && . ".env.custom" && set +a && . "$q" && rm "$q" && unset q
fi
# Read .env for default values with a tip o' the hat to https://stackoverflow.com/a/59831605/90297
t=$(mktemp) && export -p >"$t" && set -a && . $_ENV && set +a && . "$t" && rm "$t" && unset t
t=$(mktemp) && export -p >"$t" && set -a && . ".env" && set +a && . "$t" && rm "$t" && unset t
if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
_group="::group::"