From 6f2d1eda247f5eefa6c1fcf1a2dba75d956dbd66 Mon Sep 17 00:00:00 2001 From: RedLeaderOne Date: Tue, 6 Aug 2024 06:34:08 -0400 Subject: [PATCH] fix: unable to find image failure on build server - Initialize Docker buildx builder conditionally based on architecture and environment - Refactor project root and Git-related logic for better clarity - Source project shell-scripts dependencies and set pre-conditions Issue NMO-571 --- .../run_bats_tests_in_docker.bash | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/tests/bats_testing_tools/run_bats_tests_in_docker.bash b/tests/bats_testing_tools/run_bats_tests_in_docker.bash index 019138f..ecee0f1 100644 --- a/tests/bats_testing_tools/run_bats_tests_in_docker.bash +++ b/tests/bats_testing_tools/run_bats_tests_in_docker.bash @@ -20,13 +20,7 @@ BATS_DOCKERFILE_DISTRO=${2:-'ubuntu'} ## Set Docker builder log output for debug. Options: plain, tty or auto (default) #export BUILDKIT_PROGRESS=plain -# ====Begin======================================================================================== - -# ....Project root logic........................................................................... -PROJECT_CLONE_GIT_ROOT=$(git rev-parse --show-toplevel) -PROJECT_CLONE_GIT_NAME=$(basename "$PROJECT_CLONE_GIT_ROOT" .git) -PROJECT_GIT_REMOTE_URL=$(git remote get-url origin) -PROJECT_GIT_NAME=$(basename "${PROJECT_GIT_REMOTE_URL}" .git) +# ....N2ST root logic.............................................................................. REPO_ROOT=$(pwd) N2ST_BATS_TESTING_TOOLS_ABS_PATH="$( cd "$( dirname "${0}" )" &> /dev/null && pwd )" @@ -34,13 +28,24 @@ N2ST_BATS_TESTING_TOOLS_ABS_PATH="$( cd "$( dirname "${0}" )" &> /dev/null && pw #N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH=".${N2ST_BATS_TESTING_TOOLS_ABS_PATH/$REPO_ROOT/}" N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH="tests/bats_testing_tools" -N2ST_PATH=$( git rev-parse --show-toplevel ) -#N2ST_PATH="${N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH}/../.." +#N2ST_PATH=$( git rev-parse --show-toplevel ) +N2ST_PATH="${N2ST_BATS_TESTING_TOOLS_ABS_PATH}/../.." test -d "${N2ST_PATH}" || exit 1 #tree -a -L 1 ${N2ST_PATH} N2ST_VERSION="$(cat "${N2ST_PATH}"/version.txt)" +# ....Source project shell-scripts dependencies.................................................... +pushd "$(pwd)" >/dev/null || exit 1 +source "${N2ST_PATH}"/import_norlab_shell_script_tools_lib.bash || exit 1 +popd >/dev/null || exit 1 + +# ....Project root logic........................................................................... +PROJECT_CLONE_GIT_ROOT=$(git rev-parse --show-toplevel) +PROJECT_CLONE_GIT_NAME=$(basename "$PROJECT_CLONE_GIT_ROOT" .git) +PROJECT_GIT_REMOTE_URL=$(git remote get-url origin) +PROJECT_GIT_NAME=$(basename "${PROJECT_GIT_REMOTE_URL}" .git) +# ....Pre-condition................................................................................ if [[ $(basename "$REPO_ROOT") != ${PROJECT_CLONE_GIT_NAME} ]]; then echo -e "\n[\033[1;31mERROR\033[0m] $0 must be executed from the project root!\nCurrent wordir: $(pwd)" 1>&2 echo '(press any key to exit)' @@ -51,16 +56,42 @@ fi test -d "${N2ST_BATS_TESTING_TOOLS_ABS_PATH}" || exit 1 test -f "${N2ST_BATS_TESTING_TOOLS_RELATIVE_PATH}/bats_helper_functions.bash" || exit 1 -# Do not load MSG_BASE nor MSG_BASE_TEAMCITY from there .env file so that tested logic does not leak in that file -_MSG_BASE="\033[1m[${PROJECT_GIT_NAME}]\033[0m" -_MSG_BASE_TEAMCITY="|[${PROJECT_GIT_NAME}|]" +# //// DEV //////////////////////////////////////////////////////////////////////////////////////// + + +n2st::norlab_splash "${PROJECT_PROMPT_NAME}" "${PROJECT_GIT_REMOTE_URL}" +n2st::print_formated_script_header "$(basename $0) ${MSG_END_FORMAT}on device ${MSG_DIMMED_FORMAT}$(hostname -s)" "${MSG_LINE_CHAR_BUILDER_LVL2}" + +n2st::set_is_teamcity_run_environment_variable +n2st::print_msg "IS_TEAMCITY_RUN=${IS_TEAMCITY_RUN} ${TC_VERSION}" +if [[ ${IS_TEAMCITY_RUN} != true ]] && [[ -z ${BUILDX_BUILDER} ]]; then + # Note: Default to default buildx builder (ie native host architecture) so that the build img + # be available in the local image store and that run executed via `up_and_attach.bash` doesn't + # require pulling built img from dockerhub. + n2st::set_which_architecture_and_os + n2st::print_msg "Current image architecture and os: $IMAGE_ARCH_AND_OS" + if [[ $IMAGE_ARCH_AND_OS == 'darwin/arm64' ]]; then + export BUILDX_BUILDER=desktop-linux + else + export BUILDX_BUILDER=default + fi + n2st::print_msg "Setting BUILDX_BUILDER=$BUILDX_BUILDER" + # Force builder initialisation + docker buildx inspect --bootstrap $BUILDX_BUILDER >/dev/null +fi +# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ DEV \\\\ +# ====Begin======================================================================================== # ....Execute docker steps......................................................................... # Note: # - CONTAINER_PROJECT_ROOT_NAME is for copying the source code including the repository root (i.e.: the project name) # - BUILDKIT_CONTEXT_KEEP_GIT_DIR is for setting buildkit to keep the .git directory in the container # Source https://docs.docker.com/build/building/context/#keep-git-directory +# Do not load MSG_BASE nor MSG_BASE_TEAMCITY from there .env file so that tested logic does not leak in that file +_MSG_BASE="\033[1m[${PROJECT_GIT_NAME}]\033[0m" +_MSG_BASE_TEAMCITY="|[${PROJECT_GIT_NAME}|]" + if [[ ${TEAMCITY_VERSION} ]]; then echo -e "##teamcity[blockOpened name='${_MSG_BASE_TEAMCITY} Build custom bats-core docker image']" else