Skip to content

Commit

Permalink
fix: unable to find image failure on build server
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
RedLeader962 committed Aug 6, 2024
1 parent bad76e4 commit 6f2d1ed
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions tests/bats_testing_tools/run_bats_tests_in_docker.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,32 @@ 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 )"

# ToDo: assessment › harccoding the relative path is more robust. Since the location wont change anymore, the version with string substitution is irelevant.
#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)'
Expand All @@ -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
Expand Down

0 comments on commit 6f2d1ed

Please sign in to comment.