-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_deploy_all.sh
executable file
·205 lines (171 loc) · 6.11 KB
/
build_deploy_all.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
#!/bin/bash -e
# License: MIT
# David Graeff <david.graeff@web.de> - 2019
# Create void linux images via the build_one_arch script and move the compressed
# image files to ./releases. Parse the CHANGELOG file and create a new Github
# release according to the latest entry of that file, including new git tag and description.
#
# Upload all ./releases images and attach them to the latest Github release.
readonly RELEASE_API_URL="https://api.github.com/repos/openhab-nodes/ohx-os/releases"
readonly UPLOAD_API_URL="https://uploads.github.com/repos/openhab-nodes/ohx-os/releases"
if [ -f github_token.inc ]; then
source ./github_token.inc
fi
: "${GITHUB_TOKEN:=}"
build_one() {
MACHINE="$1"
ARCH="$2"
BASEIMG="releases/ohx-$MACHINE-$ARCH.img.xz"
mkdir -p releases
if [ ! -f "$BASEIMG" ]; then
say "Build $MACHINE - $ARCH"
sh build_one_arch.sh $MACHINE $ARCH || err "Failed to build $MACHINE on $ARCH"
mv "voidlinux/ohx-$MACHINE-$ARCH.img.xz" "$BASEIMG"
fi
}
# Attach binary to Github release. Remove existing one if necessary.
# The filename pattern is: ohx-machine-tagname-sha256short.img.xz, eg: "ohx-rpi3-v1.0.0-aceac12.img.xz"
deploy() {
local MACHINE="$1"
local ARCH="$2"
local LATEST_RELEASE="$3"
local _file="releases/ohx-$MACHINE-$ARCH.img.xz"
[ ! -f $_file ] && say "Skip non existing $_file" && return
local MACHINE_ESC=$(echo $MACHINE | sed -e 's/-/_/g')
local rel_id=$(echo $LATEST_RELEASE | jq -r ".id")
local assets=$(echo $LATEST_RELEASE | jq -r '.assets[] | with_entries(select(.key == "id" or .key == "name")) | flatten | .[]')
local sha=$(cat $_file| sha256sum -bz|cut -c -6)
local current_tag=$(tagname)
local is_done="0"
# Do we need to deploy?
IFS=' '
while read -r asset_id; do
read -r asset_name
local asset_machine=$(echo $asset_name|cut -d "-" -f2)
local asset_arch=$(echo $asset_name|cut -d "-" -f3)
local asset_tagname=$(echo $asset_name|cut -d "-" -f4)
local asset_sha=$(echo $asset_name|cut -d "-" -f5|cut -d "." -f1)
local delete_url="$RELEASE_API_URL/assets/$asset_id"
if [ "$asset_tagname" = "$current_tag" ] && [ "$asset_machine" = "$MACHINE_ESC" ] && [ "$asset_arch" = "$ARCH" ]; then
if [ "$asset_sha" != "$sha" ]; then
say "Checksums not equal. Reupload for $asset_machine ($asset_sha vs $sha). Old ID: $asset_id"
curl -sSL -X DELETE "$delete_url" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json"
else
say "Identical checksums. No need to redeploy $asset_machine for $ARCH"
is_done="1"
fi
break
fi
done <<< $assets
[ "$is_done" = "1" ] && return
local mimetype=$(file --mime-type -b "$_file")
local upload_url="$UPLOAD_API_URL/$rel_id/assets"
local basename="ohx-$MACHINE_ESC-$ARCH-$current_tag-$sha.img.xz"
local label="OHX $MACHINE Image ($basename)"
#label=$(echo $label | curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3-)
say "Uploading $basename..."
local _response=$(
curl -SL -X POST \
-H "Accept: application/vnd.github.manifold-preview" \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: $mimetype" \
--data-urlencode "label=$label" \
--data-binary "@$_file" "$upload_url?name=$basename"
)
local _state=$(jq -r '.state' <<< "$_response")
if [ "$_state" != "uploaded" ]; then
err "Artifact not uploaded: $basename: $_response"
else
say "Uploaded!"
fi
}
tagname() {
cat CHANGELOG.md |grep -Po "v([0-9]{1,}\.)+[0-9]{1,}" -m 1
}
build_num() {
git rev-parse HEAD
}
next_rel_title() {
cat CHANGELOG.md | grep -Pzo '##.*\n\n\K\X*?(?=\n##|$)' | tr '\0' '\n' | head -n1
}
next_rel_body() {
cat CHANGELOG.md | grep -Pzo '##.*\n\n\K\X*?(?=\n##|$)' | tr '\0' '\n' | sed '1d'
}
# Create Github release if not yet existing
make_release() {
latest_release=$(curl -sSL "${RELEASE_API_URL}/latest" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json")
if [ "$(echo $latest_release | jq -r '.message')" = "Not Found" ]; then
say "Latest release not found"
need_new="1"
else
latest_ver=$(echo $latest_release | jq -r '.name')
say "Latest release found: $latest_ver"
[ "$latest_ver" != "$(next_rel_title)" ] && need_new="1"
fi
if [ ! -z "$need_new" ]; then
say "Create new release: $(next_rel_title)"
local _payload=$(
jq --null-input \
--arg tag "$(tagname)" \
--arg name "$(next_rel_title)" \
--arg body "$(next_rel_body)" \
'{ tag_name: $tag, name: $name, body: $body, draft: false }'
)
curl -sSL -X POST "$RELEASE_API_URL" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "$_payload"
fi
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1; then
err "need '$1' (command not found) $2"
fi
}
say() {
local color=$( tput setaf 2 )
local normal=$( tput sgr0 )
echo "${color}$1${normal}"
}
err() {
local color=$( tput setaf 1 )
local normal=$( tput sgr0 )
echo "${color}$1${normal}" >&2
exit 1
}
need_cmd curl
need_cmd jq
need_cmd mkdir
need_cmd grep
need_cmd cat
need_cmd head
need_cmd sed
need_cmd basename
need_cmd file
if [ -z "$GITHUB_TOKEN" ]; then
say "No github token set! Go to your Github account -> Developer Settings -> Tokens and create a new token."
say "Store the new token in a file github_token.inc file as GITHUB_TOKEN=your_token"
exit 0
fi
build_one uefi x86_64 || err "Failed to build x86_64"
build_one uefi aarch64 || err "Failed to build aarch64"
build_one beaglebone armv7l || err "Failed to build beaglebone"
build_one cubieboard2 armv7l || err "Failed to build cubieboard2"
build_one rpi2 armv7l || err "Failed to build rpi2"
build_one rpi3 aarch64 || err "Failed to build rpi3"
build_one odroid-c2 aarch64 || err "Failed to build odroid-c2"
make_release || err "Failed to create a Github release"
deploy uefi x86_64 "$latest_release"
deploy uefi aarch64 "$latest_release"
deploy beaglebone armv7l "$latest_release"
deploy cubieboard2 armv7l "$latest_release"
deploy rpi2 armv7l "$latest_release"
deploy rpi3 aarch64 "$latest_release"
deploy odroid-c2 aarch64 "$latest_release"