Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit f7a0a8b

Browse files

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

Diff for: script-library/github-debian.sh

+71-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#
1010
# Syntax: ./github-debian.sh [version]
1111

12-
CLI_VERSION=${1:-"latest"}
12+
CLI_VERSION=${VERSION:-"latest"}
13+
INSTALL_DIRECTLY_FROM_GITHUB_RELEASE=${INSTALLDIRECTLYFROMGITHUBRELEASE:-"true"}
1314

1415
GITHUB_CLI_ARCHIVE_GPG_KEY=23F3D4EA75716059
1516
GPG_KEY_SERVERS="keyserver hkp://keyserver.ubuntu.com:80
@@ -140,31 +141,77 @@ receive_gpg_keys() {
140141
fi
141142
}
142143

143-
# Function to run apt-get if needed
144-
apt_get_update_if_needed()
144+
apt_get_update()
145145
{
146-
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
147-
echo "Running apt-get update..."
148-
apt-get update
149-
else
150-
echo "Skipping apt-get update."
151-
fi
146+
echo "Running apt-get update..."
147+
apt-get update -y
152148
}
153149

154150
# Checks if packages are installed and installs them if not
155151
check_packages() {
156152
if ! dpkg -s "$@" > /dev/null 2>&1; then
157-
apt_get_update_if_needed
153+
apt_get_update
158154
apt-get -y install --no-install-recommends "$@"
159155
fi
160156
}
161157

158+
find_version_from_git_tags() {
159+
local variable_name=$1
160+
local requested_version=${!variable_name}
161+
if [ "${requested_version}" = "none" ]; then return; fi
162+
local repository=$2
163+
local prefix=${3:-"tags/v"}
164+
local separator=${4:-"."}
165+
local last_part_optional=${5:-"false"}
166+
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
167+
local escaped_separator=${separator//./\\.}
168+
local last_part
169+
if [ "${last_part_optional}" = "true" ]; then
170+
last_part="(${escaped_separator}[0-9]+)?"
171+
else
172+
last_part="${escaped_separator}[0-9]+"
173+
fi
174+
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
175+
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
176+
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
177+
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
178+
else
179+
set +e
180+
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
181+
set -e
182+
fi
183+
fi
184+
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
185+
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
186+
exit 1
187+
fi
188+
echo "${variable_name}=${!variable_name}"
189+
}
190+
191+
192+
# Fall back on direct download if no apt package exists
193+
# Fetches .deb file to be installed with dpkg
194+
install_deb_using_github() {
195+
check_packages wget
196+
arch=$(dpkg --print-architecture)
197+
198+
find_version_from_git_tags CLI_VERSION https://github.com/cli/cli
199+
cli_filename="gh_${CLI_VERSION}_linux_${arch}.deb"
200+
201+
mkdir -p /tmp/ghcli
202+
pushd /tmp/ghcli
203+
wget https://github.com/cli/cli/releases/download/v${CLI_VERSION}/${cli_filename}
204+
dpkg -i /tmp/ghcli/${cli_filename}
205+
popd
206+
rm -rf /tmp/ghcli
207+
}
208+
162209
export DEBIAN_FRONTEND=noninteractive
163210

164211
# Install curl, apt-transport-https, curl, gpg, or dirmngr, git if missing
165212
check_packages curl ca-certificates apt-transport-https dirmngr gnupg2
166213
if ! type git > /dev/null 2>&1; then
167-
apt_get_update_if_needed
214+
apt_get_update
168215
apt-get -y install --no-install-recommends git
169216
fi
170217

@@ -178,11 +225,16 @@ fi
178225

179226
# Install the GitHub CLI
180227
echo "Downloading github CLI..."
181-
# Import key safely (new method rather than deprecated apt-key approach) and install
182-
. /etc/os-release
183-
receive_gpg_keys GITHUB_CLI_ARCHIVE_GPG_KEY /usr/share/keyrings/githubcli-archive-keyring.gpg
184-
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list
185-
apt-get update
186-
apt-get -y install "gh${version_suffix}"
187-
rm -rf "/tmp/gh/gnupg"
188-
echo "Done!"
228+
229+
if [ "${INSTALL_DIRECTLY_FROM_GITHUB_RELEASE}" = "true" ]; then
230+
install_deb_using_github
231+
else
232+
# Import key safely (new method rather than deprecated apt-key approach) and install
233+
. /etc/os-release
234+
receive_gpg_keys GITHUB_CLI_ARCHIVE_GPG_KEY /usr/share/keyrings/githubcli-archive-keyring.gpg
235+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list
236+
apt-get update
237+
apt-get -y install "gh${version_suffix}"
238+
rm -rf "/tmp/gh/gnupg"
239+
echo "Done!"
240+
fi

0 commit comments

Comments
 (0)