Skip to content

Commit ee3cbf0

Browse files
authored
feat: merge .env and .env.custom file during installation (#3564)
Closes #3558
1 parent cfb0491 commit ee3cbf0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

_unit-test/merge-env-file-test.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# This is a test file for a part of `_lib.sh`, where we read `.env.custom` file if there is one.
4+
# We only want to give very minimal value to the `.env.custom` file, and expect that it would
5+
# be merged with the original `.env` file, with the `.env.custom` file taking precedence.
6+
cat <<EOF >".env.custom"
7+
SENTRY_EVENT_RETENTION_DAYS=10
8+
EOF
9+
10+
# The `_test_setup.sh` script sources `install/_lib.sh`, so.. finger crossed this should works.
11+
source _unit-test/_test_setup.sh
12+
13+
rm -f .env.custom
14+
15+
echo "Expecting SENTRY_EVENT_RETENTION_DAYS to be 10, got ${SENTRY_EVENT_RETENTION_DAYS}"
16+
test "$SENTRY_EVENT_RETENTION_DAYS" == "10"
17+
echo "Pass"
18+
echo "Expecting SENTRY_BIND to be 9000, got ${SENTRY_BIND}"
19+
test "$SENTRY_BIND" == "9000"
20+
echo "Pass"
21+
echo "Expecting COMPOSE_PROJECT_NAME to be sentry-self-hosted, got ${COMPOSE_PROJECT_NAME}"
22+
test "$COMPOSE_PROJECT_NAME" == "sentry-self-hosted"
23+
echo "Pass"
24+
25+
report_success

install/_lib.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ else
1616
_ENV=.env
1717
fi
1818

19+
# Reading .env.custom has to come first. The value won't be overriden, instead
20+
# it would persist because of `export -p> >"$t"` later, which exports current
21+
# environment variables to a temporary file with a `declare -x KEY=value` format.
22+
# The new values on `.env` would be set only if they are not already set.
23+
if [[ "$_ENV" == ".env.custom" ]]; then
24+
q=$(mktemp) && export -p >"$q" && set -a && . ".env.custom" && set +a && . "$q" && rm "$q" && unset q
25+
fi
26+
1927
# Read .env for default values with a tip o' the hat to https://stackoverflow.com/a/59831605/90297
20-
t=$(mktemp) && export -p >"$t" && set -a && . $_ENV && set +a && . "$t" && rm "$t" && unset t
28+
t=$(mktemp) && export -p >"$t" && set -a && . ".env" && set +a && . "$t" && rm "$t" && unset t
2129

2230
if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
2331
_group="::group::"

0 commit comments

Comments
 (0)