-
Notifications
You must be signed in to change notification settings - Fork 1
/
release_egison-tutorial.sh
343 lines (312 loc) · 11.3 KB
/
release_egison-tutorial.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
# ===================================
# Automated build script for Egison
# Required Environment Variables:
# * TRAVIS_BUILD_DIR -- Given by TravisCI
# * ID_RSA -- Given by GitHub secrets.
# * API_AUTH -- Given by GitHub secrets.
# Auth token for GitHub Rest API.
# ===================================
set -xue
readonly THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DOCKERHUB_ACCOUNT="greymd"
LATEST_VERSION=
CURRENT_VERSION=
RELEASE_ARCHIVE=
readonly TARGET_BRANCH="master"
## User-Agent starts with Travis is required (https://github.com/travis-ci/travis-ci/issues/5649)
readonly COMMON_HEADER=("--retry" "5" "-H" "User-Agent: Travis/1.0" "-H" "Authorization: token $API_AUTH" "-H" "Accept: application/vnd.github.v3+json" "-L" "-f")
# Initialize SSH keys
init_ssh () {
mkdir -p "$HOME/.ssh/"
printf "Host github.com\\n\\tStrictHostKeyChecking no\\n" >> "$HOME/.ssh/config"
echo "${ID_RSA}" | base64 --decode | gzip -d > "$HOME/.ssh/id_rsa"
chmod 600 "$HOME/.ssh/id_rsa"
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
}
set_configures () {
local _package_builder_repo="$1" ;shift
local _build_target_repo="$1" ;shift
LATEST_VERSION=$(get_latest_release_cabal "${_build_target_repo}") # target repo is basically egison/egison-tutorial
CURRENT_VERSION=$(get_latest_release_file "${_package_builder_repo}" "VERSION_egison-tutorial" ) # Repo is egison/egison-package-builder
readonly FNAME="egison-tutorial-${LATEST_VERSION}.$(uname -m)"
RELEASE_ARCHIVE="${TRAVIS_BUILD_DIR:-$THIS_DIR}/${FNAME}"
readonly LATEST_VERSION CURRENT_VERSION RELEASE_ARCHIVE
}
get_release_list () {
local _repo="$1"
local _api_url="https://api.github.com/repos/${_repo}/releases"
curl "${COMMON_HEADER[@]}" "${_api_url}"
}
delete_release () {
local _package_builder_repo="$1" ;shift
local _id="$1" ;shift
local _api_url="https://api.github.com/repos/${_package_builder_repo}/releases"
curl "${COMMON_HEADER[@]}" \
-X DELETE "${_api_url}/${_id}"
}
upload_assets () {
local _repo="$1" ;shift
local _tag="$1"; shift
local _file="$1"; shift
local _url=
_url="$(get_upload_url "${_repo}" "${_tag}")"
curl "${COMMON_HEADER[@]}" \
-H "Content-Type: $(file -b --mime-type "${_file}")" \
--data-binary @"${_file}" \
"${_url}?name=$(basename "${_file}")"
}
get_latest_release () {
local _repo="$1"
curl "${COMMON_HEADER[@]}" \
-L "https://api.github.com/repos/${_repo}/releases/latest" > "./latest.json"
# shellcheck disable=SC2181
if [[ $? != 0 ]] || [[ ! -s "./latest.json" ]]; then
exit 1
fi
jq -r .tag_name < "./latest.json"| tr -d '\n'
rm "./latest.json"
}
get_latest_release_cabal () {
local _repo="$1"
echo "get_latest_release_cabal start $_repo" >&2
curl --retry 3 -f -v -H "User-Agent: Travis/1.0" \
-H "Authorization: token $API_AUTH" \
-L "https://raw.githubusercontent.com/${_repo}/master/${_repo/*\/}.cabal" \
| awk '/Version:/{print $NF}' > ./latest
_ret=$?
if [[ $_ret != 0 ]] || [[ ! -s "./latest" ]]; then
exit 1
fi
cat "./latest"
rm "./latest"
echo "get_latest_release end $_repo" >&2
}
get_latest_release_file () {
local _repo="$1" ;shift
local _file="$1"
echo "get_latest_release_file start $_repo" >&2
curl --retry 3 -f -v -H "User-Agent: Travis/1.0" \
-H "Authorization: token $API_AUTH" \
-L "https://raw.githubusercontent.com/${_repo}/master/${_file}" \
| awk -F- '{print $1}' > ./latest
_ret=$?
if [[ $_ret != 0 ]] || [[ ! -s "./latest" ]]; then
exit 1
fi
cat "./latest"
rm "./latest"
echo "get_latest_release end $_repo" >&2
}
build_tarball () {
local _file="$1" ;shift
local _ver="$1"
docker run ghcr.io/egison/egison-tutorial-tarball-builder bash /tmp/build.sh "${_ver}" > "${_file}"
file "${_file}"
{
file "${_file}" | grep 'gzip compressed'
} && echo "${_file} is successfully created." >&2
if [[ ! -s "${_file}" ]];then
echo "Failed to create '${_file}'"
exit 1
fi
}
build_rpm () {
local _tarfile="$1" ;shift
local _file="$1" ;shift
local _ver="$1"
if [[ ! -e "${_tarfile}" ]];then
build_tarball "${_tarfile}" "${_ver}"
fi
docker run -i "${DOCKERHUB_ACCOUNT}"/tar2rpm:1.0.1 < "${_tarfile}" > "${_file}"
file "${_file}"
## Result is like : "file.rpm: RPM v3.0 bin i386/x86_64 file-1.2.3"
file "${_file}" | grep 'RPM' || {
echo "Failed to create ${_file}" >&2
exit 1
}
echo "${_file} is successfully created." >&2
if [[ ! -s "${_file}" ]];then
echo "Failed to create '${_file}'"
exit 1
fi
}
build_deb () {
local _tarfile="$1" ;shift
local _file="$1" ;shift
local _ver="$1"
if [[ ! -e "${_tarfile}" ]];then
build_tarball "${_tarfile}" "${_ver}"
fi
docker run -i "${DOCKERHUB_ACCOUNT}"/tar2deb:1.0.1 < "${_tarfile}" > "${_file}"
file "${_file}"
## Result is like : "file.deb: Debian binary package (format 2.0)"
file "${_file}" | grep 'Debian' || {
echo "Failed to create ${_file}" >&2
exit 1
}
echo "${_file} is successfully created." >&2
if [[ ! -s "${_file}" ]];then
echo "Failed to create '${_file}'"
exit 1
fi
}
release_check () {
if [[ "${CURRENT_VERSION}" == "${LATEST_VERSION}" ]];then
echo "Skip git push. It is latest version." >&2
exit 0
fi
}
is_releasable () {
local _package_builder_repo="$1" ;shift
_release_id=$(get_release_list "${_package_builder_repo}" | jq '.[] | select(.tag_name == "'"${LATEST_VERSION}"'") | .id')
if [[ "${_release_id}" =~ ^[0-9][0-9]*$ ]]; then
return 0
else
echo "Egison should be released first. Skip build process." >&2
return 1
fi
}
bump_version () {
local _package_builder_repo="$1" ;shift
local _release_id
local _repo_name=${_package_builder_repo##*/}
rm -rf "${THIS_DIR:?}/${_repo_name}"
git clone -b "${TARGET_BRANCH}" \
"[email protected]:${_package_builder_repo}.git" \
"${THIS_DIR}/${_repo_name}"
cd "${THIS_DIR}/${_repo_name}"
echo "${LATEST_VERSION}-$(date +%s)" > "./VERSION_egison-tutorial"
# Crete versions and make changes to GitHub
git add "./VERSION_egison-tutorial"
git commit -m "[skip ci] Bump version to ${LATEST_VERSION} (egison-tutorial)"
## Push changes
git push origin "${TARGET_BRANCH}"
}
commit_package () {
local _package_builder_repo="$1" ;shift
local _ver="$1" ;shift
local _package="$1" ;shift
local _release_id
local _repo_name=${_package_builder_repo##*/}
rm -rf "${THIS_DIR:?}/${_repo_name}"
git clone -b "${TARGET_BRANCH}" \
"[email protected]:${_package_builder_repo}.git" \
"${THIS_DIR}/${_repo_name}"
mkdir "${THIS_DIR}/${_repo_name}/packages"
cp "$_package" "${THIS_DIR}/${_repo_name}/packages"
cd "${THIS_DIR}/${_repo_name}"
git add "./packages"
## No changes
[[ "$(git status --porcelain | grep -c .)" == "0" ]] && return 0
git commit -m "[skip ci] Update package ${_package} to ${_ver}"
git push origin "${TARGET_BRANCH}"
}
is_uploaded() {
local _repo="$1" ;shift
local _ver="$1" ;shift
local _fname="$1" ;shift
local _result=
_result="$(get_release_list "${_repo}" \
| jq ".[] | select (.tag_name==\"${_ver}\")" \
| jq -r '.assets[] | .name' )"
set +e
if grep "${_fname}" <<<"$_result" ;then
echo "$_fname is ALREADY uploaded" >&2
return 0
else
echo "$_fname is NOT uploaded yet" >&2
return 1
fi
set -e
}
download_asset() {
local _repo="$1" ;shift
local _ver="$1" ;shift
local _download_file="$1" ;shift
local _saved_file="$1" ;shift
local _download_url
_download_url="$(get_release_list "${_repo}" \
| jq ".[] | select(.tag_name == \"${_ver}\")" \
| jq -r ".assets[] | select(.name == \"${_download_file}\") | .browser_download_url")"
[[ "$_download_url" =~ ^https.*${_download_file}$ ]] || return 1
curl -o "$_saved_file" -L -f --retry 5 "$_download_url"
}
get_upload_url () {
local _repo="$1" ;shift
local _tag="$1" ;shift
local _release_info
_release_info="$(get_release_list "${_repo}")"
echo "${_release_info}" | jq ".[] | select (.tag_name==\"${_tag}\")" | jq -r .upload_url | perl -pe 's/{.*}//'
}
main () {
local _cmd="$1"
local _package_builder_repo _build_target_repo _upload_target_repo
case "$_cmd" in
init)
init_ssh
;;
bump)
_package_builder_repo="$2"
_build_target_repo="$3"
# set variables LATEST_VERSION CURRENT_VERSION RELEASE_ARCHIVE
set_configures "$_package_builder_repo" "$_build_target_repo"
release_check
bump_version "$_package_builder_repo" ## IT MIGHT BE DESTRUCTIVE!! TAKE CARE THE REPOSITORY NAME!!
;;
upload-tarball)
_package_builder_repo="$2"
_build_target_repo="$3"
_upload_target_repo="$4"
set_configures "$_package_builder_repo" "$_build_target_repo"
is_releasable "${_package_builder_repo}" || exit 0
is_uploaded "${_package_builder_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.tar.gz")" && exit 0
build_tarball "${RELEASE_ARCHIVE}.tar.gz" "${LATEST_VERSION}"
upload_assets "${_upload_target_repo}" "${LATEST_VERSION}" "${RELEASE_ARCHIVE}.tar.gz"
;;
upload-rpm)
_package_builder_repo="$2"
_build_target_repo="$3"
_upload_target_repo="$4"
set_configures "$_package_builder_repo" "$_build_target_repo"
is_releasable "${_package_builder_repo}" || exit 0
is_uploaded "${_upload_target_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.rpm")" && exit 0
build_rpm "${RELEASE_ARCHIVE}.tar.gz" "${RELEASE_ARCHIVE}.rpm" "${LATEST_VERSION}"
upload_assets "${_upload_target_repo}" "${LATEST_VERSION}" "${RELEASE_ARCHIVE}.rpm"
;;
upload-deb)
_package_builder_repo="$2"
_build_target_repo="$3"
_upload_target_repo="$4"
set_configures "$_package_builder_repo" "$_build_target_repo"
is_releasable "${_package_builder_repo}" || exit 0
is_uploaded "${_upload_target_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.deb")" && exit 0
build_deb "${RELEASE_ARCHIVE}.tar.gz" "${RELEASE_ARCHIVE}.deb" "${LATEST_VERSION}"
upload_assets "${_upload_target_repo}" "${LATEST_VERSION}" "${RELEASE_ARCHIVE}.deb"
;;
commit-deb)
_package_builder_repo="$2"
_build_target_repo="$3"
_upload_target_repo="$4"
set_configures "$_package_builder_repo" "$_build_target_repo"
is_releasable "${_package_builder_repo}" || exit 0
is_uploaded "${_upload_target_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.deb")" || exit 1
download_asset "${_package_builder_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.deb")" "egison-tutorial.$(uname -m).deb"
commit_package "${_upload_target_repo}" "${LATEST_VERSION}" "egison-tutorial.$(uname -m).deb"
;;
commit-rpm)
_package_builder_repo="$2"
_build_target_repo="$3"
_upload_target_repo="$4"
set_configures "$_package_builder_repo" "$_build_target_repo"
is_releasable "${_package_builder_repo}" || exit 0
is_uploaded "${_upload_target_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.rpm")" || exit 1
download_asset "${_package_builder_repo}" "${LATEST_VERSION}" "$(basename "${RELEASE_ARCHIVE}.rpm")" "egison-tutorial.$(uname -m).rpm"
commit_package "${_upload_target_repo}" "${LATEST_VERSION}" "egison-tutorial.$(uname -m).rpm"
;;
*)
exit 1
esac
}
main "$@"