kas files may contain environment variables and their default values.
The env: block instructs bitbake to take values of the specified
variables from the environment and expand them in recipes and/or
classes. When using kas-container, kas is effectively running in a
clean environment (as intended!): each variable must be explicitly
passed from the host environment to the container environment. This
is already done for a number of "well-known" variables. Extend this
to also forward environment variables listed in first-level kas files
(those included by the generated .config.yaml file or listed on the
kas-container command line plus any included file from the top-level
repository).
Signed-off-by: Cedric Hombourger <
cedric.h...@siemens.com>
---
docs/userguide/kas-container-description.inc | 5 ++++
kas-container | 29 +++++++++++++++++++-
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/docs/userguide/kas-container-description.inc b/docs/userguide/kas-container-description.inc
index 7f35013..7898bf6 100644
--- a/docs/userguide/kas-container-description.inc
+++ b/docs/userguide/kas-container-description.inc
@@ -4,6 +4,11 @@ decouples the build environment from the host system. For details, see
:ref:`env-vars-label`. The wrapper also takes care of mounting the necessary
directories and setting up the environment variables inside the container.
+If the ``yq`` command is available, it will make a best-effort to determinei
+environment variables listed under `env:` from top-level files to forward them
+into the container. Additional environment variables may be explicitly passed
+using ``--runtime-args "-e VAR=VALUE"`.
+
.. note::
The ``kas-container`` script has limited support for Git worktrees. Regular
Git operations on the checked-out repository are supported. However, executing
diff --git a/kas-container b/kas-container
index 4289749..8f1f696 100755
--- a/kas-container
+++ b/kas-container
@@ -244,6 +244,25 @@ process_file_arg()
KAS_REPO_DIRS="${KAS_REPO_DIRS} ${_KAS_REPO_DIR}"
}
+list_includes()
+{
+ yq -r '(.header.includes // []) | map(if . | type == "string" then . else .file end) | join("\n")' "${1}"
+}
+
+list_env_vars()
+{
+ for FILE in "${@}"; do
+ [ -f "${FILE}" ] || continue
+ yq -r '(.env // {}) | keys | join("\n")' "${FILE}"
+
+ # SC2046: Quote this to prevent word splitting.
+ # shellcheck disable=2046
+ # SC2086: Double quote to prevent globbing and word splitting.
+ # shellcheck disable=2086
+ list_env_vars $(list_includes "${FILE}")
+ done
+}
+
# Params: NAME CONTAINER_PATH MODE
# If the dir is not below KAS_WORK_DIR, the dir is mounted into the container.
forward_dir()
@@ -726,10 +745,18 @@ if command -v timedatectl >/dev/null; then
set -- "$@" -e "KAS_HOST_TZ=$(timedatectl show -p Timezone --value 2>/dev/null)"
fi
+# propagate environment variables listed in our first-level files if we have yq
+KAS_EXTRA_VARS=""
+if command -v yq >/dev/null; then
+ # SC2086: Double quote to prevent globbing and word splitting.
+ # shellcheck disable=2086
+ KAS_EXTRA_VARS=$(list_env_vars ${KAS_FIRST_FILES} | sort -u)
+fi
+
for var in TERM KAS_DISTRO KAS_MACHINE KAS_TARGET KAS_TASK KAS_CLONE_DEPTH \
KAS_PREMIRRORS DISTRO_APT_PREMIRRORS BB_NUMBER_THREADS PARALLEL_MAKE \
GIT_CREDENTIAL_USEHTTPPATH \
- TZ; do
+ TZ ${KAS_EXTRA_VARS}; do
if [ -n "$(eval echo \$${var})" ]; then
set -- "$@" -e "${var}=$(eval echo \"\$${var}\")"
fi
--
2.47.3