Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ docker-in-docker ] - buildx can fallback to previous version if latest artifact not found #869

Prev Previous commit
changes as requested in comments by @samruddhikhandale
gauravsaini04 committed Feb 22, 2024

Verified

This commit was signed with the committer’s verified signature.
commit 3e5a33be6345b1a5e368ace1e274f74edc460e48
26 changes: 12 additions & 14 deletions src/docker-in-docker/install.sh
Original file line number Diff line number Diff line change
@@ -338,20 +338,18 @@ get_previous_version() {
curl -s "$repo_url" | jq -r 'del(.[].assets) | .[1].tag_name' # this would del the assets key and then get the second encountered tag_name's value from the filtered array of objects
}

fetch_previous_version() {
install_previous_version_artifacts() {
wget_exit_code=$?
if [ $wget_exit_code -ne 0 ]; then # means wget command to fetch latest version failed
if [ $wget_exit_code -eq 8 ]; then # failure due to 404: Not Found.
echo -e "\n(!) Failed to fetch the latest artifacts for docker buildx v${buildx_version}..."
repo_url="https://api.github.com/repos/docker/buildx/releases" # GitHub repository URL
previous_version=$(get_previous_version "${repo_url}")
buildx_file_name="buildx-${previous_version}.linux-${architecture}"
echo -e "\nAttempting to install ${previous_version}"
wget https://github.com/docker/buildx/releases/download/${previous_version}/${buildx_file_name}
else
echo "(!) Failed to download docker buildx with exit code: $wget_exit_code"
exit 1
fi
if [ $wget_exit_code -eq 8 ]; then # failure due to 404: Not Found.
echo -e "\n(!) Failed to fetch the latest artifacts for docker buildx v${buildx_version}..."
repo_url="https://api.github.com/repos/docker/buildx/releases" # GitHub repository URL
previous_version=$(get_previous_version "${repo_url}")
buildx_file_name="buildx-${previous_version}.linux-${architecture}"
echo -e "\nAttempting to install ${previous_version}"
wget https://github.com/docker/buildx/releases/download/${previous_version}/${buildx_file_name}
else
echo "(!) Failed to download docker buildx with exit code: $wget_exit_code"
exit 1
fi
}

@@ -362,7 +360,7 @@ if [ "${INSTALL_DOCKER_BUILDX}" = "true" ]; then
buildx_file_name="buildx-v${buildx_version}.linux-${architecture}"

cd /tmp
wget https://github.com/docker/buildx/releases/download/v${buildx_version}/${buildx_file_name} || fetch_previous_version
wget https://github.com/docker/buildx/releases/download/v${buildx_version}/${buildx_file_name} || install_previous_version_artifacts

docker_home="/usr/libexec/docker"
cli_plugins_dir="${docker_home}/cli-plugins"
16 changes: 13 additions & 3 deletions test/docker-in-docker/docker_build_fallback_buildx.sh
Original file line number Diff line number Diff line change
@@ -5,7 +5,16 @@ set -e
# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests before test for fallback
HL="\033[1;33m"
N="\033[0;37m"
echo -e "\n👉${HL} docker/buildx version as installed by docker-in-docker feature${N}"
check "docker-buildx" docker buildx version
check "docker-build" docker build ./
check "docker-buildx" bash -c "docker buildx version"
check "docker-buildx-path" bash -c "ls -la /usr/libexec/docker/cli-plugins/docker-buildx"

echo -e "\n👉${HL} Creating a scenario for fallback${N}\n"
# Code to test the made up scenario when latest version of docker/buildx fails on wget command for fetching the artifacts
repo_url="https://api.github.com/repos/docker/buildx/releases" # GitHub repository URL
architecture="$(dpkg --print-architecture)"
@@ -41,7 +50,7 @@ change_version_to_fail() {
echo "${buildx_version_fallback_test}"
}

fetch_previous_version() {
install_previous_version_artifacts() {
wget_exit_code=$?
if [ $wget_exit_code -ne 0 ]; then # means wget command to fetch latest version failed
if [ $wget_exit_code -eq 8 ]; then # failure due to 404: Not Found.
@@ -62,7 +71,7 @@ buildx_file_name="buildx-${test_version}.linux-${architecture}"
buildx_version=$test_version

# This wget command will fail as the wrong version won't fetch artifact
wget https://github.com/docker/buildx/releases/download/${buildx_version}/${buildx_file_name} || fetch_previous_version
wget https://github.com/docker/buildx/releases/download/${buildx_version}/${buildx_file_name} || install_previous_version_artifacts

docker_home="/usr/libexec/docker"
cli_plugins_dir="${docker_home}/cli-plugins"
@@ -75,7 +84,8 @@ chown -R "${USERNAME}:docker" "${docker_home}"
chmod -R g+r+w "${docker_home}"
find "${docker_home}" -type d -print0 | xargs -n 1 -0 chmod g+s

# Definition specific tests
# Definition specific tests after test for fallback
echo -e "\n👉${HL} docker/buildx version as installed by test for fallback${N}"
check "docker-buildx" docker buildx version
check "docker-build" docker build ./
check "docker-buildx" bash -c "docker buildx version"