Skip to content

Commit

Permalink
feat: merge .env and .env.custom file during installation (#3564)
Browse files Browse the repository at this point in the history
Closes #3558
  • Loading branch information
aldy505 authored Feb 4, 2025
1 parent cfb0491 commit ee3cbf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 25 additions & 0 deletions _unit-test/merge-env-file-test.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion install/_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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::"
Expand Down

0 comments on commit ee3cbf0

Please sign in to comment.