feat: load env vars from file or archive at startup (#4053)

This commit is contained in:
Chip Wolf ‮
2026-05-13 00:56:30 +01:00
committed by GitHub
parent b35db38cd3
commit ffbd905ec4
15 changed files with 250 additions and 3 deletions
+25
View File
@@ -152,6 +152,31 @@ fi
cd /data || exit 1
##########################################
# Optionally load environment variables from a file or archive entry,
# allowing packs/artifacts to declare TYPE, VERSION and other settings
# inside-out. Loaded values override anything passed in through docker -e.
# Generic packs are processed first so that LOAD_ENV_FROM_FILE and
# LOAD_ENV_FROM_ARCHIVE can override any values they set.
if isTrue "${LOAD_ENV_FROM_GENERIC_PACK:-false}"; then
if ! loadEnvFromGenericPack; then
exit 1
fi
fi
if [[ ${LOAD_ENV_FROM_FILE:-} ]]; then
if ! loadEnvFromFile "${LOAD_ENV_FROM_FILE}"; then
exit 1
fi
fi
if [[ ${LOAD_ENV_FROM_ARCHIVE:-} ]]; then
if ! loadEnvFromArchive "${LOAD_ENV_FROM_ARCHIVE}" "${LOAD_ENV_FROM_ARCHIVE_ENTRY:-.env}"; then
exit 1
fi
fi
export DECLARED_TYPE=${TYPE^^}
export DECLARED_VERSION="$VERSION"
+116 -3
View File
@@ -26,6 +26,117 @@ function applyResultsFile() {
set +a
}
function loadEnvFromFile() {
local source=${1?Missing required source argument}
local downloaded=
if isURL "$source"; then
mkdir -p /data/.tmp
downloaded=$(mktemp -p /data/.tmp)
log "Downloading env file from $source"
if ! get -o "$downloaded" "$source"; then
logError "Failed to download env file from $source"
rm -f "$downloaded"
return 1
fi
log "Loading env vars from $source"
applyResultsFile "$downloaded"
rm -f "$downloaded"
elif [ -f "$source" ]; then
log "Loading env vars from $source"
applyResultsFile "$source"
else
logError "Env file not found: $source"
return 1
fi
}
function loadEnvFromArchive() {
local source=${1?Missing required source argument}
local entry=${2:-.env}
local archive=
local downloaded=
local tmpdir
local rc=0
mkdir -p /data/.tmp
if isURL "$source"; then
downloaded=$(mktemp -p /data/.tmp)
log "Downloading archive from $source"
if ! get -o "$downloaded" "$source"; then
logError "Failed to download archive from $source"
rm -f "$downloaded"
return 1
fi
archive=$downloaded
elif [ -f "$source" ]; then
archive=$source
else
logError "Archive not found: $source"
return 1
fi
tmpdir=$(mktemp -d -p /data/.tmp)
if extract "$archive" "$tmpdir" "$entry" && [ -f "$tmpdir/$entry" ]; then
log "Loading env vars from '$entry' in $source"
applyResultsFile "$tmpdir/$entry"
else
logError "Failed to load env entry '$entry' from $source"
rc=1
fi
rm -rf "$tmpdir"
[[ -n "$downloaded" ]] && rm -f "$downloaded"
return $rc
}
function loadEnvFromGenericPack() {
: "${GENERIC_PACKS:=${GENERIC_PACK:-}}"
: "${GENERIC_PACKS_PREFIX:=}"
: "${GENERIC_PACKS_SUFFIX:=}"
if [[ -z "${GENERIC_PACKS}" ]]; then
logWarning "LOAD_ENV_FROM_GENERIC_PACK is set but GENERIC_PACK(S) is empty"
return 0
fi
mkdir -p /data/.tmp
IFS=',' read -ra packs <<< "${GENERIC_PACKS}"
local loaded=0
local pack packEntry packFile tmpdir
for packEntry in "${packs[@]}"; do
pack="${GENERIC_PACKS_PREFIX}${packEntry}${GENERIC_PACKS_SUFFIX}"
if isURL "$pack"; then
mkdir -p /data/packs
if ! packFile=$(get -o /data/packs --output-filename --skip-up-to-date "$pack"); then
logError "Failed to download generic pack $pack"
return 1
fi
else
packFile=$pack
fi
if [[ ! -f "$packFile" ]]; then
logError "Generic pack not found: $packFile"
return 1
fi
tmpdir=$(mktemp -d -p /data/.tmp)
# Packs without a .env are valid — silently skip; the unpack step still applies them.
if extract "$packFile" "$tmpdir" .env 2>/dev/null && [ -f "$tmpdir/.env" ]; then
log "Loading env vars from .env in $pack"
applyResultsFile "$tmpdir/.env"
loaded=$((loaded + 1))
fi
rm -rf "$tmpdir"
done
if (( loaded == 0 )); then
logWarning "LOAD_ENV_FROM_GENERIC_PACK is set but no pack in GENERIC_PACK(S) contained a .env"
fi
}
function join_by() {
local d=$1
shift
@@ -445,17 +556,19 @@ function isType() {
function extract() {
src=${1?}
destDir=${2?}
shift 2
# remaining args are paths within the archive to extract; if none, extract everything
type=$(file -b --mime-type "${src}")
case "${type}" in
application/zip)
unzip -o -q -d "${destDir}" "${src}"
unzip -o -q -d "${destDir}" "${src}" "$@"
;;
application/x-tar | application/gzip | application/x-gzip | application/x-bzip2)
tar -C "${destDir}" -xf "${src}"
tar -C "${destDir}" -xf "${src}" "$@"
;;
application/zstd | application/x-zstd)
tar -C "${destDir}" --use-compress-program=unzstd -xf "${src}"
tar -C "${destDir}" --use-compress-program=unzstd -xf "${src}" "$@"
;;
*)
logError "Unsupported archive type: $type"