@@ -109,18 +109,73 @@ find_version_from_git_tags() {
109109 echo " ${variable_name} =${! variable_name} "
110110}
111111
112+ # Use semver logic to decrement a version number then look for the closest match
113+ find_prev_version_from_git_tags () {
114+ local variable_name=$1
115+ local current_version=${! variable_name}
116+ local repository=$2
117+ # Normally a "v" is used before the version number, but support alternate cases
118+ local prefix=${3:- " tags/v" }
119+ # Some repositories use "_" instead of "." for version number part separation, support that
120+ local separator=${4:- " ." }
121+ # Some tools release versions that omit the last digit (e.g. go)
122+ local last_part_optional=${5:- " false" }
123+ # Some repositories may have tags that include a suffix (e.g. actions/node-versions)
124+ local version_suffix_regex=$6
125+ # Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
126+ set +e
127+ major=" $( echo " ${current_version} " | grep -oE ' ^[0-9]+' || echo ' ' ) "
128+ minor=" $( echo " ${current_version} " | grep -oP ' ^[0-9]+\.\K[0-9]+' || echo ' ' ) "
129+ breakfix=" $( echo " ${current_version} " | grep -oP ' ^[0-9]+\.[0-9]+\.\K[0-9]+' 2> /dev/null || echo ' ' ) "
130+
131+ if [ " ${minor} " = " 0" ] && [ " ${breakfix} " = " 0" ]; then
132+ (( major= major- 1 ))
133+ declare -g ${variable_name} =" ${major} "
134+ # Look for latest version from previous major release
135+ find_version_from_git_tags " ${variable_name} " " ${repository} " " ${prefix} " " ${separator} " " ${last_part_optional} "
136+ # Handle situations like Go's odd version pattern where "0" releases omit the last part
137+ elif [ " ${breakfix} " = " " ] || [ " ${breakfix} " = " 0" ]; then
138+ (( minor= minor- 1 ))
139+ declare -g ${variable_name} =" ${major} .${minor} "
140+ # Look for latest version from previous minor release
141+ find_version_from_git_tags " ${variable_name} " " ${repository} " " ${prefix} " " ${separator} " " ${last_part_optional} "
142+ else
143+ (( breakfix= breakfix- 1 ))
144+ if [ " ${breakfix} " = " 0" ] && [ " ${last_part_optional} " = " true" ]; then
145+ declare -g ${variable_name} =" ${major} .${minor} "
146+ else
147+ declare -g ${variable_name} =" ${major} .${minor} .${breakfix} "
148+ fi
149+ fi
150+ set -e
151+ }
152+
112153# Function to fetch the version released prior to the latest version
113154get_previous_version () {
114- repo_url=$1
115- curl -s " $repo_url " | jq -r ' del(.[].assets) | .[0].tag_name'
155+ local url=$1
156+ local repo_url=$2
157+ local variable_name=$3
158+ prev_version=${! variable_name}
159+
160+ output=$( curl -s " $repo_url " ) ;
161+ message=$( echo " $output " | jq -r ' .message' )
162+
163+ if [[ $message == " API rate limit exceeded" * ]]; then
164+ echo -e " \nAn attempt to find latest version using GitHub Api Failed... \nReason: ${message} "
165+ echo -e " \nAttempting to find latest version using GitHub tags."
166+ find_prev_version_from_git_tags prev_version " $url " " tags/v"
167+ declare -g ${variable_name} =" ${prev_version} "
168+ else
169+ echo -e " \nAttempting to find latest version using GitHub Api."
170+ version=$( echo " $output " | jq -r ' .tag_name' )
171+ declare -g ${variable_name} =" ${version# v} "
172+ fi
173+ echo " ${variable_name} =${! variable_name} "
116174}
117175
118- install_compose_switch_fallback () {
119- echo -e " \n(!) Failed to fetch the latest artifacts for compose-switch v${compose_switch_version} ..."
120- previous_version=$( get_previous_version " https://api.github.com/repos/docker/compose-switch/releases" )
121- echo -e " \nAttempting to install ${previous_version} "
122- compose_switch_version=${previous_version# v}
123- curl -fsSL " https://github.com/docker/compose-switch/releases/download/v${compose_switch_version} /docker-compose-linux-${architecture} " -o /usr/local/bin/compose-switch
176+ get_github_api_repo_url () {
177+ local url=$1
178+ echo " ${url/ https: \/\/ github.com/ https: \/\/ api.github.com\/ repos} /releases/latest"
124179}
125180
126181# ##########################################
@@ -265,6 +320,16 @@ echo "Finished installing docker / moby!"
265320docker_home=" /usr/libexec/docker"
266321cli_plugins_dir=" ${docker_home} /cli-plugins"
267322
323+ # fallback for docker-compose
324+ fallback_compose (){
325+ local url=$1
326+ local repo_url=$( get_github_api_repo_url " $url " )
327+ echo -e " \n(!) Failed to fetch the latest artifacts for docker-compose v${compose_version} ..."
328+ get_previous_version " ${url} " " ${repo_url} " compose_version
329+ echo -e " \nAttempting to install v${compose_version} "
330+ curl -fsSL " https://github.com/docker/compose/releases/download/v${compose_version} /docker-compose-linux-${target_compose_arch} " -o ${docker_compose_path}
331+ }
332+
268333# If 'docker-compose' command is to be included
269334if [ " ${DOCKER_DASH_COMPOSE_VERSION} " != " none" ]; then
270335 case " ${architecture} " in
@@ -301,17 +366,16 @@ if [ "${DOCKER_DASH_COMPOSE_VERSION}" != "none" ]; then
301366 fi
302367 else
303368 compose_version=${DOCKER_DASH_COMPOSE_VERSION# v}
304- find_version_from_git_tags compose_version " https://github.com/docker/compose" " tags/v"
369+ docker_compose_url=" https://github.com/docker/compose"
370+ find_version_from_git_tags compose_version " $docker_compose_url " " tags/v"
305371 echo " (*) Installing docker-compose ${compose_version} ..."
306- curl -L " https://github.com/docker/compose/releases/download/v${compose_version} /docker-compose-linux-${target_compose_arch} " -o ${docker_compose_path}
307-
308- if grep -q " Not Found" " ${docker_compose_path} " ; then
309- echo -e " \n(!) Failed to fetch the latest artifacts for docker-compose v${compose_version} ..."
310- previous_version=$( get_previous_version " https://api.github.com/repos/docker/compose/releases" )
311- echo -e " \nAttempting to install ${previous_version} "
312- compose_version=${previous_version# v}
313- curl -L " https://github.com/docker/compose/releases/download/v${compose_version} /docker-compose-linux-${target_compose_arch} " -o ${docker_compose_path}
314- fi
372+ curl -fsSL " https://github.com/docker/compose/releases/download/v${compose_version} /docker-compose-linux-${target_compose_arch} " -o ${docker_compose_path} || {
373+ if [[ $DOCKER_DASH_COMPOSE_VERSION == " latest" ]]; then
374+ fallback_compose " $docker_compose_url "
375+ else
376+ echo -e " Error: Failed to install docker-compose v${compose_version} "
377+ fi
378+ }
315379
316380 chmod +x ${docker_compose_path}
317381
@@ -325,15 +389,26 @@ if [ "${DOCKER_DASH_COMPOSE_VERSION}" != "none" ]; then
325389 fi
326390fi
327391
392+ # fallback method for compose-switch
393+ fallback_compose-switch () {
394+ local url=$1
395+ local repo_url=$( get_github_api_repo_url " $url " )
396+ echo -e " \n(!) Failed to fetch the latest artifacts for compose-switch v${compose_switch_version} ..."
397+ get_previous_version " $url " " $repo_url " compose_switch_version
398+ echo -e " \nAttempting to install v${compose_switch_version} "
399+ curl -fsSL " https://github.com/docker/compose-switch/releases/download/v${compose_switch_version} /docker-compose-linux-${architecture} " -o /usr/local/bin/compose-switch
400+ }
401+
328402# Install docker-compose switch if not already installed - https://github.com/docker/compose-switch#manual-installation
329403if [ " ${INSTALL_DOCKER_COMPOSE_SWITCH} " = " true" ] && ! type compose-switch > /dev/null 2>&1 ; then
330404 if type docker-compose > /dev/null 2>&1 ; then
331405 echo " (*) Installing compose-switch..."
332406 current_compose_path=" $( which docker-compose) "
333407 target_compose_path=" $( dirname " ${current_compose_path} " ) /docker-compose-v1"
334408 compose_switch_version=" latest"
335- find_version_from_git_tags compose_switch_version " https://github.com/docker/compose-switch"
336- curl -fsSL " https://github.com/docker/compose-switch/releases/download/v${compose_switch_version} /docker-compose-linux-${architecture} " -o /usr/local/bin/compose-switch || install_compose_switch_fallback
409+ compose_switch_url=" https://github.com/docker/compose-switch"
410+ find_version_from_git_tags compose_switch_version " $compose_switch_url "
411+ curl -fsSL " https://github.com/docker/compose-switch/releases/download/v${compose_switch_version} /docker-compose-linux-${architecture} " -o /usr/local/bin/compose-switch || fallback_compose-switch " $compose_switch_url "
337412 chmod +x /usr/local/bin/compose-switch
338413 # TODO: Verify checksum once available: https://github.com/docker/compose-switch/issues/11
339414 # Setup v1 CLI as alternative in addition to compose-switch (which maps to v2)
360435
361436usermod -aG docker ${USERNAME}
362437
363- install_previous_version_artifacts () {
364- wget_exit_code=$?
365- if [ $wget_exit_code -eq 8 ]; then # failure due to 404: Not Found.
366- echo -e " \n(!) Failed to fetch the latest artifacts for docker buildx v${buildx_version} ..."
367- repo_url=" https://api.github.com/repos/docker/buildx/releases" # GitHub repository URL
368- previous_version=$( get_previous_version " ${repo_url} " )
369- buildx_file_name=" buildx-${previous_version} .linux-${architecture} "
370- echo -e " \nAttempting to install ${previous_version} "
371- wget https://github.com/docker/buildx/releases/download/${previous_version} /${buildx_file_name}
372- else
373- echo " (!) Failed to download docker buildx with exit code: $wget_exit_code "
374- exit 1
375- fi
438+ # fallback for docker/buildx
439+ fallback_buildx () {
440+ local url=$1
441+ local repo_url=$( get_github_api_repo_url " $url " )
442+ echo -e " \n(!) Failed to fetch the latest artifacts for docker buildx v${buildx_version} ..."
443+ get_previous_version " $url " " $repo_url " buildx_version
444+ buildx_file_name=" buildx-v${buildx_version} .linux-${architecture} "
445+ echo -e " \nAttempting to install v${buildx_version} "
446+ wget https://github.com/docker/buildx/releases/download/v${buildx_version} /${buildx_file_name}
376447}
377448
378449if [ " ${INSTALL_DOCKER_BUILDX} " = " true" ]; then
379450 buildx_version=" latest"
380- find_version_from_git_tags buildx_version " https://github.com/docker/buildx" " refs/tags/v"
451+ docker_buildx_url=" https://github.com/docker/buildx"
452+ find_version_from_git_tags buildx_version " $docker_buildx_url " " refs/tags/v"
381453 echo " (*) Installing buildx ${buildx_version} ..."
382454 buildx_file_name=" buildx-v${buildx_version} .linux-${architecture} "
383455
384456 cd /tmp
385- wget https://github.com/docker/buildx/releases/download/v${buildx_version} /${buildx_file_name} || install_previous_version_artifacts
457+ wget https://github.com/docker/buildx/releases/download/v${buildx_version} /${buildx_file_name} || fallback_buildx " $docker_buildx_url "
386458
387459 docker_home=" /usr/libexec/docker"
388460 cli_plugins_dir=" ${docker_home} /cli-plugins"
0 commit comments