-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.sh
executable file
·313 lines (291 loc) · 10.3 KB
/
main.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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
x() {
local cmd="$1"
shift
(
set -x
"${cmd}" "$@"
)
}
bail() {
echo "::error::$*"
exit 1
}
warn() {
echo "::warning::$*"
}
if [[ $# -gt 0 ]]; then
bail "invalid argument '$1'"
fi
token="${INPUT_TOKEN:-"${GITHUB_TOKEN:-}"}"
ref="${INPUT_REF:-"${GITHUB_REF:-}"}"
if [[ -z "${token}" ]]; then
bail "neither GITHUB_TOKEN environment variable nor 'token' input option is set"
fi
if [[ "${ref}" != "refs/tags/"* ]]; then
bail "tag ref should start with 'refs/tags/': '${ref}'"
fi
tag="${ref#refs/tags/}"
features="${INPUT_FEATURES:-}"
archive="${INPUT_ARCHIVE:?}"
if [[ ! "${INPUT_TAR}" =~ ^(all|unix|windows|none)$ ]]; then
bail "invalid input 'tar': ${INPUT_TAR}"
elif [[ ! "${INPUT_ZIP}" =~ ^(all|unix|windows|none)$ ]]; then
bail "invalid input 'zip': ${INPUT_ZIP}"
fi
leading_dir="${INPUT_LEADING_DIR:-}"
case "${leading_dir}" in
true) leading_dir="1" ;;
false) leading_dir="" ;;
*) bail "'leading_dir' input option must be 'true' or 'false': '${leading_dir}'" ;;
esac
no_default_features="${INPUT_NO_DEFAULT_FEATURES:-}"
case "${no_default_features}" in
true) no_default_features="1" ;;
false) no_default_features="" ;;
*) bail "'no_default_features' input option must be 'true' or 'false': '${no_default_features}'" ;;
esac
bin_name="${INPUT_BIN:?}"
bin_names=()
if [[ -n "${bin_name}" ]]; then
# We can expand a glob by expanding a variable without quote, but that way
# has a security issue of shell injection.
if [[ "${bin_name}" == *"?"* ]] || [[ "${bin_name}" == *"*"* ]] || [[ "${bin_name}" == *"["* ]]; then
# This check is not for security but for diagnostic purposes.
# We quote the filename, so without this uses get an error like
# "cp: cannot stat 'app-*': No such file or directory".
bail "glob pattern in 'bin' input option is not supported yet"
fi
while read -rd,; do bin_names+=("${REPLY}"); done <<<"${bin_name},"
fi
if [[ ${#bin_names[@]} -gt 1 ]] && [[ "${archive}" == *"\$bin"* ]]; then
bail "when multiple binary names are specified, default archive name or '\$bin' variable cannot be used in 'archive' option"
fi
include="${INPUT_INCLUDE:-}"
includes=()
if [[ -n "${include}" ]]; then
# We can expand a glob by expanding a variable without quote, but that way
# has a security issue of shell injection.
if [[ "${include}" == *"?"* ]] || [[ "${include}" == *"*"* ]] || [[ "${include}" == *"["* ]]; then
# This check is not for security but for diagnostic purposes.
# We quote the filename, so without this uses get an error like
# "cp: cannot stat 'LICENSE-*': No such file or directory".
bail "glob pattern in 'include' input option is not supported yet"
fi
while read -rd,; do includes+=("${REPLY}"); done <<<"${include},"
fi
asset="${INPUT_ASSET:-}"
assets=()
if [[ -n "${asset}" ]]; then
# We can expand a glob by expanding a variable without quote, but that way
# has a security issue of shell injection.
if [[ "${asset}" == *"?"* ]] || [[ "${asset}" == *"*"* ]] || [[ "${asset}" == *"["* ]]; then
# This check is not for security but for diagnostic purposes.
# We quote the filename, so without this uses get an error like
# "cp: cannot stat 'LICENSE-*': No such file or directory".
bail "glob pattern in 'asset' input option is not supported yet"
fi
while read -rd,; do assets+=("${REPLY}"); done <<<"${asset},"
fi
checksum="${INPUT_CHECKSUM:-}"
checksums=()
if [[ -n "${checksum}" ]]; then
while read -rd,; do
checksums+=("${REPLY}")
case "${REPLY}" in
sha256 | sha512 | sha1 | md5) ;;
*) bail "'checksum' input option must be 'sha256', 'sha512', 'sha1', or 'md5': '${REPLY}'" ;;
esac
done <<<"${checksum},"
fi
host=$(rustc -Vv | grep host | cut -c 7-)
target="${INPUT_TARGET:-"${host}"}"
target_lower="${target//-/_}"
target_lower="${target_lower//./_}"
target_upper="$(tr '[:lower:]' '[:upper:]' <<<"${target_lower}")"
build_tool="${INPUT_BUILD_TOOL:-}"
if [[ -z "${build_tool}" ]]; then
build_tool="cargo"
if [[ "${host}" != "${target}" ]]; then
rustup target add "${target}"
case "${target}" in
# https://github.com/cross-rs/cross#supported-targets
*windows-msvc | *windows-gnu | *darwin | *fuchsia | *redox) ;;
*)
# If any of these are set, it is obvious that the user has set up a cross-compilation environment on the host.
if [[ -z "$(eval "echo \${CARGO_TARGET_${target_upper}_LINKER:-}")" ]] && [[ -z "$(eval "echo \${CARGO_TARGET_${target_upper}_RUNNER:-}")" ]]; then
build_tool="cross"
fi
;;
esac
fi
fi
archive="${archive/\$bin/${bin_names[0]}}"
archive="${archive/\$target/${target}}"
archive="${archive/\$tag/${tag}}"
tar="tar"
case "${OSTYPE}" in
linux*)
platform="unix"
;;
darwin*)
platform="unix"
# Work around https://github.com/actions/cache/issues/403 by using GNU tar
# instead of BSD tar.
tar="gtar"
if ! type -P gtar &>/dev/null; then
brew install gnu-tar &>/dev/null
fi
;;
cygwin* | msys*)
platform="windows"
exe=".exe"
;;
*) bail "unrecognized OSTYPE '${OSTYPE}'" ;;
esac
# TODO: check profile.release.strip in root Cargo.toml and skip strip on our end if it is set.
strip=""
case "${target}" in
*-pc-windows-msvc) ;;
x86_64-* | i686-*)
strip="strip"
;;
arm*-linux-*eabi)
strip="arm-linux-gnueabi-strip"
;;
arm*-linux-*eabihf | thumb*-linux-*eabihf)
strip="arm-linux-gnueabihf-strip"
;;
arm*-none-eabi | thumb*-none-eabi)
strip="arm-none-eabi-strip"
;;
aarch64*-linux-*)
strip="aarch64-linux-gnu-strip"
;;
esac
if [[ -n "${strip:-}" ]]; then
if ! type -P "${strip}" &>/dev/null; then
warn "${strip} not found, skip stripping"
strip=""
fi
fi
build_options=("--release")
bins=()
for bin_name in "${bin_names[@]}"; do
bins+=("${bin_name}${exe:-}")
build_options+=("--bin" "${bin_name}")
done
if [[ -n "${features}" ]]; then
build_options+=("--features" "${features}")
fi
if [[ -n "${no_default_features}" ]]; then
build_options+=("--no-default-features")
fi
manifest_path="${INPUT_MANIFEST_PATH:-}"
if [[ -n "${manifest_path}" ]]; then
build_options+=("--manifest-path" "${manifest_path}")
target_dir=$(cargo metadata --format-version=1 --no-deps --manifest-path "${manifest_path}" | jq -r '."target_directory"')
else
target_dir=$(cargo metadata --format-version=1 --no-deps | jq -r '."target_directory"')
fi
if [[ -n "${INPUT_TARGET:-}" ]]; then
build_options+=("--target" "${target}")
target_dir="${target_dir}/${target}/release"
else
target_dir="${target_dir}/release"
fi
case "${build_tool}" in
cargo) x cargo build "${build_options[@]}" ;;
cross)
if ! type -P cross &>/dev/null; then
x cargo install cross
fi
x cross build "${build_options[@]}"
;;
*) bail "unrecognized build tool '${build_tool}'" ;;
esac
if [[ -n "${strip:-}" ]]; then
for bin_exe in "${bins[@]}"; do
x "${strip}" "${target_dir}/${bin_exe}"
done
fi
if [[ "${INPUT_TAR/all/${platform}}" == "${platform}" ]] || [[ "${INPUT_ZIP/all/${platform}}" == "${platform}" ]]; then
cwd=$(pwd)
tmpdir=$(mktemp -d)
mkdir "${tmpdir:?}/${archive}"
filenames=("${bins[@]}")
for bin_exe in "${bins[@]}"; do
cp "${target_dir}/${bin_exe}" "${tmpdir}/${archive}"/
done
for include in ${includes[@]+"${includes[@]}"}; do
cp -r "${include}" "${tmpdir}/${archive}"/
filenames+=("$(basename "${include}")")
done
pushd "${tmpdir}" >/dev/null
if [[ -n "${leading_dir}" ]]; then
# with leading directory
#
# /${archive}
# /${archive}/${bins}
# /${archive}/${includes}
if [[ "${INPUT_TAR/all/${platform}}" == "${platform}" ]]; then
assets+=("${archive}.tar.gz")
"${tar}" acf "${cwd}/${archive}.tar.gz" "${archive}"
fi
if [[ "${INPUT_ZIP/all/${platform}}" == "${platform}" ]]; then
assets+=("${archive}.zip")
if [[ "${platform}" == "unix" ]]; then
zip -r "${cwd}/${archive}.zip" "${archive}"
else
7z a "${cwd}/${archive}.zip" "${archive}"
fi
fi
else
# without leading directory
#
# /${bins}
# /${includes}
pushd "${archive}" >/dev/null
if [[ "${INPUT_TAR/all/${platform}}" == "${platform}" ]]; then
assets+=("${archive}.tar.gz")
"${tar}" acf "${cwd}/${archive}.tar.gz" "${filenames[@]}"
fi
if [[ "${INPUT_ZIP/all/${platform}}" == "${platform}" ]]; then
assets+=("${archive}.zip")
if [[ "${platform}" == "unix" ]]; then
zip -r "${cwd}/${archive}.zip" "${filenames[@]}"
else
7z a "${cwd}/${archive}.zip" "${filenames[@]}"
fi
fi
popd >/dev/null
fi
popd >/dev/null
rm -rf "${tmpdir:?}/${archive}"
fi
# Checksum of all assets except for .<checksum> files.
final_assets=("${assets[@]}")
for checksum in ${checksums[@]+"${checksums[@]}"}; do
# TODO: Should we allow customizing the name of checksum files?
if type -P "${checksum}sum" &>/dev/null; then
"${checksum}sum" "${assets[@]}" >"${archive}.${checksum}"
else
case "${OSTYPE}" in
darwin*)
# GitHub-hosted macOS runner does not install GNU Coreutils by default.
# https://github.com/actions/runner-images/issues/90
case "${checksum}" in
sha*) shasum -a "${checksum#sha}" "${assets[@]}" >"${archive}.${checksum}" ;;
md5) md5 "${assets[@]}" >"${archive}.${checksum}" ;;
*) bail "unrecognized 'checksum' input option '${checksum}'" ;;
esac
;;
*) bail "'${checksum}sum' command not found" ;;
esac
fi
final_assets+=("${archive}.${checksum}")
done
# https://cli.github.com/manual/gh_release_upload
GITHUB_TOKEN="${token}" gh release upload "${tag}" "${final_assets[@]}" --clobber