From 3aadfc2bfc9b83f52f3deb9abc4caefe2c92b908 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Fri, 3 Jun 2022 14:49:04 +0200 Subject: [PATCH] ci-automation: Run functions in subshells The functions are sourcing other files that define global variables, so they will spill into the callers shell unnecessarily. We will also add some functionality that uses traps in follow-up commits, so it's good to limit the scope of traps too. --- ci-automation/garbage_collect.sh | 14 ++++++++++++-- ci-automation/image.sh | 13 +++++++++++-- ci-automation/packages.sh | 14 +++++++++++--- ci-automation/push_pkgs.sh | 13 +++++++++++-- ci-automation/sdk_bootstrap.sh | 13 +++++++++++-- ci-automation/sdk_container.sh | 13 +++++++++++-- ci-automation/test.sh | 13 +++++++++++-- ci-automation/vms.sh | 13 +++++++++++-- 8 files changed, 89 insertions(+), 17 deletions(-) diff --git a/ci-automation/garbage_collect.sh b/ci-automation/garbage_collect.sh index 40327df9e7..dfc5685204 100644 --- a/ci-automation/garbage_collect.sh +++ b/ci-automation/garbage_collect.sh @@ -24,9 +24,18 @@ # in the scripts repo. The newest 50 builds will be retained, # all older builds will be purged (50 is the default, see OPTIONAL INPUT above). -set -eu - function garbage_collect() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _garbage_collect_impl "${@}" + ) +} +# -- + +function _garbage_collect_impl() { local keep="${1:-50}" local dry_run="${DRY_RUN:-}" local purge_versions="${PURGE_VERSIONS:-}" @@ -133,3 +142,4 @@ function garbage_collect() { fi done } +# -- diff --git a/ci-automation/image.sh b/ci-automation/image.sh index 87ac8ca65e..5c0234a9a6 100644 --- a/ci-automation/image.sh +++ b/ci-automation/image.sh @@ -32,9 +32,18 @@ # 2. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. -set -eu - function image_build() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _image_build_impl "${@}" + ) +} +# -- + +function _image_build_impl() { local arch="$1" source sdk_lib/sdk_container_common.sh diff --git a/ci-automation/packages.sh b/ci-automation/packages.sh index 40d1d9d19e..a3ae052d21 100644 --- a/ci-automation/packages.sh +++ b/ci-automation/packages.sh @@ -56,10 +56,18 @@ # 3. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. - -set -eu - function packages_build() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _packages_build_impl "${@}" + ) +} +# -- + +function _packages_build_impl() { local version="$1" local arch="$2" local coreos_git="${3:-}" diff --git a/ci-automation/push_pkgs.sh b/ci-automation/push_pkgs.sh index 2f65c886bd..78e25af599 100644 --- a/ci-automation/push_pkgs.sh +++ b/ci-automation/push_pkgs.sh @@ -31,8 +31,6 @@ # 2. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. -set -eu - # This function is run _inside_ the SDK container function image_build__copy_to_bincache() { local arch="$1" @@ -46,6 +44,17 @@ function image_build__copy_to_bincache() { # -- function push_packages() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _push_packages_impl "${@}" + ) +} +# -- + +function _push_packages_impl() { local arch="$1" source ci-automation/ci_automation_common.sh diff --git a/ci-automation/sdk_bootstrap.sh b/ci-automation/sdk_bootstrap.sh index 135f71cf6c..13b0c88020 100644 --- a/ci-automation/sdk_bootstrap.sh +++ b/ci-automation/sdk_bootstrap.sh @@ -48,9 +48,18 @@ # 3. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. -set -eu - function sdk_bootstrap() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _sdk_bootstrap_impl "${@}" + ) +} +# -- + +function _sdk_bootstrap_impl() { local seed_version="$1" local version="$2" local coreos_git="${3-}" diff --git a/ci-automation/sdk_container.sh b/ci-automation/sdk_container.sh index 01cffe632a..6234eb783c 100644 --- a/ci-automation/sdk_container.sh +++ b/ci-automation/sdk_container.sh @@ -29,9 +29,18 @@ # 2. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. -set -eu - function sdk_container_build() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _sdk_container_build_impl "${@}" + ) +} +# -- + +function _sdk_container_build_impl() { : ${ARCH:="amd64"} source ci-automation/ci_automation_common.sh diff --git a/ci-automation/test.sh b/ci-automation/test.sh index f8b9e58c15..725efb8002 100644 --- a/ci-automation/test.sh +++ b/ci-automation/test.sh @@ -78,8 +78,6 @@ # script would need to make anyway. For more information, please refer # to the vendor_test.sh file. -set -euo pipefail - # Download torcx package and manifest, add build cache URL to manifest # so the docker.torcx-manifest-pkgs test can use it. function __prepare_torcx() { @@ -106,6 +104,17 @@ function __prepare_torcx() { # -- function test_run() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _test_run_impl "${@}" + ) +} +# -- + +function _test_run_impl() { local arch="$1" ; shift local image="$1"; shift diff --git a/ci-automation/vms.sh b/ci-automation/vms.sh index 7140fcf243..37da63af1a 100644 --- a/ci-automation/vms.sh +++ b/ci-automation/vms.sh @@ -31,9 +31,18 @@ # 2. "./ci-cleanup.sh" with commands to clean up temporary build resources, # to be run after this step finishes / when this step is aborted. -set -eu - function vm_build() { + # Run a subshell, so the traps, environment changes and global + # variables are not spilled into the caller. + ( + set -euo pipefail + + _vm_build_impl "${@}" + ) +} +# -- + +function _vm_build_impl() { local arch="$1" shift # $@ now contains image formats to build