11#! /usr/bin/env bash
2-
2+ LATEST_TAG= $( curl --fail --retry " $PARAM_GH_MAX_RETRIES " -Ls -o /dev/null -w ' %{url_effective} ' https://github.com/cli/cli/releases/latest | sed ' s:.*/:: ' )
33set_sudo () {
44 if [[ $EUID == 0 ]]; then
55 echo " "
@@ -32,22 +32,6 @@ download_gh_cli() {
3232 local platform=$1
3333 local file_extension=$2
3434 if [ " $PARAM_GH_CLI_VERSION " = " latest" ]; then
35- max_retries=$PARAM_GH_MAX_RETRIES
36- i=0
37- while (( i < max_retries )) ; do
38- (( i++ ))
39- LATEST_TAG=$( curl -s https://api.github.com/repos/cli/cli/releases/latest | jq -r ' .tag_name' )
40- if [ " $LATEST_TAG " != null ]; then
41- break
42- fi
43- echo " Couldn't get latest version, retrying..."
44- sleep 60
45- done
46- if (( i == max_retries )) ; then
47- echo " Error: Max retries exceeded."
48- exit 1
49- fi
50-
5135 PARAM_GH_CLI_VERSION=" ${LATEST_TAG# v} "
5236 fi
5337 local download_url=" https://github.com/cli/cli/releases/download/v${PARAM_GH_CLI_VERSION} /gh_${PARAM_GH_CLI_VERSION} _${platform} .${file_extension} "
@@ -78,6 +62,7 @@ install_gh_cli() {
7862}
7963
8064sudo=$( set_sudo)
65+ platform=$( detect_platform)
8166
8267# Check for required commands
8368for cmd in curl tar; do
9075# Verify if the CLI is already installed. Exit if it is.
9176if command -v gh > /dev/null 2>&1 ; then
9277 echo " GH CLI is already installed."
93- exit 0
78+ INSTALLED_VERSION=$( gh version | awk ' {print $3}' )
79+ if [ " $PARAM_GH_CLI_VERSION " = " latest" ]; then
80+ desired_v=" $LATEST_TAG "
81+ else
82+ desired_v=" $PARAM_GH_CLI_VERSION "
83+ fi
84+ major=$( printf " %s\n%s" " $desired_v " " $INSTALLED_VERSION " | sort -V | tail -1 )
85+ if [ " $major " = " $INSTALLED_VERSION " ]; then
86+ echo " GH CLI version installed is newer or equal to the desired. Not installing."
87+ exit 0
88+ else
89+ echo " GH CLI version installed is older than desired. Reinstalling."
90+ # Removing old version
91+ if [ " $platform " == " linux_amd64" ]; then
92+ set -x; $sudo apt remove --yes gh; set +x
93+ else
94+ set -x; $sudo rm /usr/local/bin/gh; set +x
95+ fi
96+ fi
9497fi
9598
9699# If the GH CLI version is less than or equal to 2.24.0 on macOS ARM then exit
@@ -100,7 +103,6 @@ if [[ "$platform" == "macos_arm" ]] && version_le "$PARAM_GH_CLI_VERSION" "2.24.
100103 exit 1
101104fi
102105
103- platform=$( detect_platform)
104106if [ " $platform " == " unsupported" ]; then
105107 echo " $( uname -a) -$( uname -m) is not supported. If you believe it should be, please consider opening an issue."
106108 exit 1
0 commit comments