|
9 | 9 | #
|
10 | 10 | # Syntax: ./github-debian.sh [version]
|
11 | 11 |
|
12 |
| -CLI_VERSION=${1:-"latest"} |
| 12 | +CLI_VERSION=${VERSION:-"latest"} |
| 13 | +INSTALL_DIRECTLY_FROM_GITHUB_RELEASE=${INSTALLDIRECTLYFROMGITHUBRELEASE:-"true"} |
13 | 14 |
|
14 | 15 | GITHUB_CLI_ARCHIVE_GPG_KEY=23F3D4EA75716059
|
15 | 16 | GPG_KEY_SERVERS="keyserver hkp://keyserver.ubuntu.com:80
|
@@ -140,31 +141,77 @@ receive_gpg_keys() {
|
140 | 141 | fi
|
141 | 142 | }
|
142 | 143 |
|
143 |
| -# Function to run apt-get if needed |
144 |
| -apt_get_update_if_needed() |
| 144 | +apt_get_update() |
145 | 145 | {
|
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 |
152 | 148 | }
|
153 | 149 |
|
154 | 150 | # Checks if packages are installed and installs them if not
|
155 | 151 | check_packages() {
|
156 | 152 | if ! dpkg -s "$@" > /dev/null 2>&1; then
|
157 |
| - apt_get_update_if_needed |
| 153 | + apt_get_update |
158 | 154 | apt-get -y install --no-install-recommends "$@"
|
159 | 155 | fi
|
160 | 156 | }
|
161 | 157 |
|
| 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 | + |
162 | 209 | export DEBIAN_FRONTEND=noninteractive
|
163 | 210 |
|
164 | 211 | # Install curl, apt-transport-https, curl, gpg, or dirmngr, git if missing
|
165 | 212 | check_packages curl ca-certificates apt-transport-https dirmngr gnupg2
|
166 | 213 | if ! type git > /dev/null 2>&1; then
|
167 |
| - apt_get_update_if_needed |
| 214 | + apt_get_update |
168 | 215 | apt-get -y install --no-install-recommends git
|
169 | 216 | fi
|
170 | 217 |
|
|
178 | 225 |
|
179 | 226 | # Install the GitHub CLI
|
180 | 227 | 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