-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre.sh
executable file
·313 lines (304 loc) · 11.6 KB
/
pre.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0 OR MIT
set -CeEuo pipefail
IFS=$'\n\t'
retry() {
for i in {1..10}; do
if "$@"; then
return 0
else
sleep "${i}"
fi
done
"$@"
}
bail() {
printf '::error::%s\n' "$*"
exit 1
}
warn() {
printf '::warning::%s\n' "$*"
}
info() {
printf >&2 'info: %s\n' "$*"
}
_sudo() {
if type -P sudo >/dev/null; then
sudo "$@"
else
"$@"
fi
}
download_and_checksum() {
local url="${1:?}"
local checksum="${2:?}"
retry curl --proto '=https' --tlsv1.2 -fsSL --retry 10 "${url}" -o tmp
if type -P sha256sum >/dev/null; then
sha256sum -c - >/dev/null <<<"${checksum} *tmp"
elif type -P shasum >/dev/null; then
# GitHub-hosted macOS runner does not install GNU Coreutils by default.
# https://github.com/actions/runner-images/issues/90
shasum -a 256 -c - >/dev/null <<<"${checksum} *tmp"
else
warn "checksum requires 'sha256sum' or 'shasum' command; consider installing one of them; skipped checksum for $(basename -- "${url}")"
fi
}
apt_update() {
retry _sudo apt-get -o Acquire::Retries=10 -qq update
apt_updated=1
}
apt_install() {
if [[ -z "${apt_updated:-}" ]]; then
apt_update
fi
retry _sudo apt-get -o Acquire::Retries=10 -o Dpkg::Use-Pty=0 install -y --no-install-recommends "$@"
}
dnf_install() {
retry _sudo "${dnf}" install -y "$@"
}
pacman_install() {
retry _sudo pacman -Sy --noconfirm "$@"
}
apk_install() {
if type -P sudo >/dev/null; then
retry sudo apk --no-cache add "$@"
elif type -P doas >/dev/null; then
retry doas apk --no-cache add "$@"
else
retry apk --no-cache add "$@"
fi
}
sys_install() {
case "${base_distro}" in
debian) apt_install "$@" ;;
fedora) dnf_install "$@" ;;
arch) pacman_install "$@" ;;
alpine) apk_install "$@" ;;
esac
}
# Inputs
tool="${INPUT_TOOL:?}"
locked="${INPUT_LOCKED:?}"
git="${INPUT_GIT:-}"
tag="${INPUT_TAG:-}"
rev="${INPUT_REV:-}"
# Refs: https://github.com/rust-lang/rustup/blob/HEAD/rustup-init.sh
base_distro=''
case "$(uname -s)" in
Linux)
ldd_version=$(ldd --version 2>&1 || true)
if grep -Fq musl <<<"${ldd_version}"; then
host_os="linux-musl"
else
host_glibc_version=$(grep -E "GLIBC|GNU libc" <<<"${ldd_version}" | sed -E "s/.* //g")
host_os="linux-gnu-${host_glibc_version}"
fi
if grep -Eq '^ID_LIKE=' /etc/os-release; then
base_distro=$(grep -E '^ID_LIKE=' /etc/os-release | cut -d= -f2)
case "${base_distro}" in
*debian*) base_distro=debian ;;
*fedora*) base_distro=fedora ;;
*arch*) base_distro=arch ;;
*alpine*) base_distro=alpine ;;
esac
else
base_distro=$(grep -E '^ID=' /etc/os-release | cut -d= -f2)
fi
base_distro="${base_distro//\"/}"
case "${base_distro}" in
fedora)
dnf=dnf
if ! type -P dnf >/dev/null; then
if type -P microdnf >/dev/null; then
# fedora-based distributions have "minimal" images that
# use microdnf instead of dnf.
dnf=microdnf
else
# If neither dnf nor microdnf is available, it is
# probably an RHEL7-based distribution that does not
# have dnf installed by default.
dnf=yum
fi
fi
;;
esac
;;
Darwin) host_os=macos ;;
MINGW* | MSYS* | CYGWIN* | Windows_NT) host_os=windows ;;
*) bail "unrecognized OS type '$(uname -s)'" ;;
esac
case "$(uname -m)" in
aarch64 | arm64) host_arch=aarch64 ;;
xscale | arm | armv*l)
# Ignore Arm for now, as we need to consider the version and whether hard-float is supported.
# https://github.com/rust-lang/rustup/pull/593
# https://github.com/cross-rs/cross/pull/1018
# Does it seem only armv7l+ is supported?
# https://github.com/actions/runner/blob/v2.319.0/src/Misc/externals.sh#L191
# https://github.com/actions/runner/issues/688
bail "32-bit Arm runner is not supported yet by this action; if you need support for this platform, please submit an issue at <https://github.com/taiki-e/cache-cargo-install-action>"
;;
# GitHub Actions Runner supports Linux (x86_64, AArch64, Arm), Windows (x86_64, AArch64),
# and macOS (x86_64, AArch64).
# https://github.com/actions/runner/blob/v2.319.0/.github/workflows/build.yml#L21
# https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners#supported-architectures-and-operating-systems-for-self-hosted-runners
# So we can assume x86_64 unless it is AArch64 or Arm.
*) host_arch=x86_64 ;;
esac
if [[ "${tool}" == *","* ]]; then
bail "cache-cargo-install-action does not support specifying multiple crates yet; consider calling this action per crate"
fi
fetch=''
if [[ "${tool}" == *"@"* ]]; then
if [[ -n "${git}" ]]; then
bail "<tool>@<version> syntax is not supported with 'git' input option"
fi
version="${tool#*@}"
tool="${tool%@*}"
if [[ ! "${version}" =~ ^([1-9][0-9]*\.[0-9]+\.[0-9]+|0\.[1-9][0-9]*\.[0-9]+|^0\.0\.[0-9]+)(-[0-9A-Za-z\.-]+)?(\+[0-9A-Za-z\.-]+)?$|^latest$ ]]; then
if [[ ! "${version}" =~ ^([1-9][0-9]*(\.[0-9]+(\.[0-9]+)?)?|0\.[1-9][0-9]*(\.[0-9]+)?|^0\.0\.[0-9]+)(-[0-9A-Za-z\.-]+)?(\+[0-9A-Za-z\.-]+)?$|^latest$ ]]; then
bail "cache-cargo-install-action does not support non-semver version: '${version}'"
fi
fetch='1'
fi
elif [[ -n "${git}" ]]; then
if [[ -n "${tag}" ]] && [[ -n "${rev}" ]]; then
bail "'tag' and 'rev' input options cannot be used together"
fi
if [[ -z "${tag}" ]] && [[ -z "${rev}" ]]; then
bail "'git' input option currently requires one of 'tag' or 'rev' input option"
fi
version=''
else
version=latest
fi
case "${locked}" in
true)
locked=--locked
locked_key=''
;;
false)
locked=''
locked_key=-locked-false
;;
*) bail "'locked' input option must be 'true' or 'false': '${locked}'" ;;
esac
if [[ "${version}" == "latest" ]] || [[ -n "${fetch}" ]]; then
install_action_dir="${HOME}/.cache-cargo-install-action"
case "${host_os}" in
linux*)
if ! type -P jq >/dev/null || ! type -P curl >/dev/null; then
case "${base_distro}" in
debian | fedora | arch | alpine)
printf '::group::Install packages required for installation (jq and/or curl)\n'
sys_packages=()
if ! type -P curl >/dev/null; then
sys_packages+=(ca-certificates curl)
fi
if [[ "${dnf:-}" == "yum" ]]; then
# On RHEL7-based distribution jq requires EPEL
if ! type -P jq >/dev/null; then
sys_packages+=(epel-release)
sys_install "${sys_packages[@]}"
sys_install jq --enablerepo=epel
else
sys_install "${sys_packages[@]}"
fi
else
if ! type -P jq >/dev/null; then
# https://github.com/taiki-e/install-action/issues/521
if [[ "${base_distro}" == "arch" ]]; then
sys_packages+=(glibc)
fi
sys_packages+=(jq)
fi
sys_install "${sys_packages[@]}"
fi
printf '::endgroup::\n'
;;
*) warn "cache-cargo-install-action requires jq and curl on non-Debian/Fedora/Arch/Alpine-based Linux" ;;
esac
fi
;;
macos)
if ! type -P jq >/dev/null || ! type -P curl >/dev/null; then
warn "cache-cargo-install-action requires jq and curl on macOS"
fi
;;
windows)
if ! type -P curl >/dev/null; then
warn "cache-cargo-install-action requires curl on Windows"
fi
if type -P jq >/dev/null; then
# https://github.com/jqlang/jq/issues/1854
_tmp=$(jq -r .a <<<'{}')
if [[ "${_tmp}" != "null" ]]; then
_tmp=$(jq -b -r .a 2>/dev/null <<<'{}' || true)
if [[ "${_tmp}" == "null" ]]; then
jq() { command jq -b "$@"; }
else
jq() { command jq "$@" | tr -d '\r'; }
fi
else
printf '::group::Install packages required for installation (jq)\n'
mkdir -p -- "${install_action_dir}/jq/bin"
url='https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-windows-amd64.exe'
checksum='7451fbbf37feffb9bf262bd97c54f0da558c63f0748e64152dd87b0a07b6d6ab'
(
cd -- "${install_action_dir}/jq/bin"
download_and_checksum "${url}" "${checksum}"
mv -- tmp jq.exe
)
printf '::endgroup::\n'
jq() { "${install_action_dir}/jq/bin/jq.exe" -b "$@"; }
fi
fi
;;
*) bail "unsupported host OS '${host_os}'" ;;
esac
crate_info=$(retry curl --proto '=https' --tlsv1.2 -fsSL --retry 10 "https://crates.io/api/v1/crates/${tool}")
case "${version}" in
latest)
version=$(jq -r '.crate.max_stable_version' <<<"${crate_info}")
if [[ "${version}" == "null" ]]; then
bail "no stable version found for ${tool}; if you want to install a pre-release version, please specify the full version"
fi
;;
*)
if [[ ! "${version}" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
bail "cache-cargo-install-action does not support non-semver version: '${version}'"
fi
# shellcheck disable=SC2207
versions=($(jq -r ".versions[] | select(.num | startswith(\"${version}.\")) | select(.yanked == false) | .num" <<<"${crate_info}"))
full_version=''
for v in ${versions[@]+"${versions[@]}"}; do
if [[ ! "${v}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\+[0-9A-Za-z\.-]+)?$ ]]; then
continue
fi
full_version="${v}"
break
done
if [[ -z "${full_version}" ]]; then
bail "no stable version found for ${tool} that match with '${version}.*'; if you want to install a pre-release version, please specify the full version"
fi
version="${full_version}"
;;
esac
fi
bin_dir="${RUNNER_TOOL_CACHE}/${tool}/bin"
printf '%s\n' "${bin_dir}" >>"${GITHUB_PATH}"
if [[ -n "${git}" ]]; then
key="${tool}-git-${tag:+"tag-${tag}"}${rev:+"rev-${rev}"}-${host_arch}-${host_os}${locked_key}"
else
key="${tool}-${version}-${host_arch}-${host_os}${locked_key}"
fi
cat >>"${GITHUB_OUTPUT}" <<EOF
tool=${tool}
version=${version}
key=${key}
path=${bin_dir}
locked=${locked}
git=${git}
tag=${tag}
rev=${rev}
EOF