-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.sh
executable file
·315 lines (250 loc) · 9.33 KB
/
build.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
#!/bin/bash
ACTION="$1"
export DWQ_DISQUE_URL="disque:7711"
CI_GIT_URL="ssh://git@git.riot-os.org:3333"
CI_GIT_URL_WORKER="https://git.riot-os.org"
MURDOCK_API_URL="http://localhost:8000"
CI_BUILD_REPO_WORKER="murdock/RIOT"
GITHUB_REPO_URL="https://github.com/RIOT-OS/RIOT"
BASEDIR="$(dirname $(realpath $0))"
[ -f "${BASEDIR}/local.sh" ] && . "${BASEDIR}/local.sh"
random() {
hexdump -n ${1:-4} -e '/2 "%u"' /dev/urandom
}
# true if "$2" starts with "$1", false otherwise
startswith() {
case "${2}" in
${1}*) true ;;
*) false ;;
esac
}
# this function returns 0 when this is build is building one of the two merge
# queue branches ("staging" or "trying", 1 otherwise.
is_merge_queue_build() {
startswith "gh-readonly-queue/" "${CI_BUILD_BRANCH}"
}
# this function gets CI_BASE_BRANCH and CI_BASE_COMMIT from the build branch name,
# when that looks like a Github merge_group build branch
handle_merge_queue_build() {
local repo_dir="$1"
if is_merge_queue_build; then
echo "--- this is a merge_group build, fetching info"
# Set CI_BASE_BRANCH in case of a merge queue build.
if test -z "${CI_BASE_BRANCH}"; then
base_branch="$(echo ${CI_BUILD_BRANCH} | sed 's_gh-readonly-queue/\(.*\)/.*_\1_')"
export CI_BASE_BRANCH="${base_branch}"
git -C "${repo_dir}" fetch github "${CI_BASE_BRANCH}" -f
fi
# Set CI_BASE_COMMIT in case of a merge queue build.
if test -z "${CI_BASE_COMMIT}"; then
previous_merge="$(git -C ${repo_dir} merge-base github/${CI_BASE_BRANCH} HEAD)"
export CI_BASE_COMMIT="${previous_merge}"
fi
echo "--- merge group base branch: ${CI_BASE_BRANCH}@${CI_BASE_COMMIT}"
fi
}
retry() {
local tries=$1
local delay=$2
shift 2
local n=0
while [ $n -lt $tries ]; do
$1 && return 0
$2
sleep $delay
n=$(expr $n + 1)
done
return 1
}
_gethead() {
local gitdir="$1"
local url="$2"
local branch="${3:-master}"
git -C "${gitdir}" ls-remote "${url}" "refs/heads/${branch}" | cut -f1
}
gethead() {
local url="$1"
local branch="${2:-master}"
local gitdir="$(git rev-parse --show-toplevel 2>/dev/null)"
[ -z "${gitdir}" ] && {
local tmpdir="$(mktemp -d)"
gitdir="${tmpdir}"
}
_gethead "${gitdir}" "${url}" "${branch}"
local res=$?
[ -n "${tmpdir}" ] && rm -rf "${tmpdir}"
return ${res}
}
post_build() {
echo "-- processing results ..."
for script in $(find ${BASEDIR}/post-build.d -type f -executable); do
echo "- running script \"${script}\""
python3 ${script} || true
done
echo "-- done processing results"
}
get_jobs() {
dwqc ${DWQ_ENV} './.murdock get_jobs'
}
checkout_commit() {
local repo_dir="$1"
local base_repo="$2"
local base_commit="$3"
echo "--- cloning base repo"
git-cache init
git-cache clone ${base_repo} ${base_commit} ${repo_dir}
echo "--- adding remotes"
git -C ${repo_dir} remote add cache_repo "${CI_GIT_URL}/${CI_BUILD_REPO_WORKER}.git"
git -C ${repo_dir} remote add github ${GITHUB_REPO_URL}
git -C ${repo_dir} fetch github ${base_commit}
git -C ${repo_dir} checkout ${base_commit}
}
create_merge_commit() {
local repo_dir="$1"
local base_repo="$2"
local base_head="$3"
local pr_head="$4"
local pr_num="$5"
checkout_commit ${repo_dir} ${base_repo} ${base_head}
echo "--- checking out merge branch"
local merge_branch=${CI_MURDOCK_PROJECT}/pull/${base_head}/${pr_head}
git -C ${repo_dir} checkout -B ${merge_branch}
echo "--- fetching PR HEAD: ${pr_head}"
git -C ${repo_dir} fetch github pull/${pr_num}/head -f
echo "--- merging ${pr_head} into ${base_head}"
git -C ${repo_dir} merge --no-rerere-autoupdate --no-edit --no-ff ${pr_head}
if [ $? -ne 0 ]; then
echo "--- creating merge commit failed, aborting!"
rm -rf ${repo_dir}
exit 1
else
echo "--- pushing result"
git -C ${repo_dir} push --force cache_repo
fi
export CI_MERGE_COMMIT="$(git -C ${repo_dir} rev-parse ${merge_branch})"
export CI_WORKER_BRANCH="${merge_branch}"
}
: ${NIGHTLY:=0}
: ${STATIC_TESTS:=0}
: ${APPS:=}
: ${BOARDS:=}
set_status() {
local status="{\"status\" : {\"status\": \"${1}\"}}"
/usr/bin/curl -s -d "${status}" -H "Content-Type: application/json" -H "Authorization: ${CI_JOB_TOKEN}" -X PUT ${MURDOCK_API_URL}/job/${CI_JOB_UID}/status > /dev/null
}
check_label() {
local label="${1}"
[ -z "${CI_PULL_LABELS}" ] && return 1
echo "${CI_PULL_LABELS}" | grep -q "${label}"
return $?
}
if [ -z "${CI_FAST_FAIL}" ]; then
if [ "${NIGHTLY}" = "1" ]; then
export CI_FAST_FAIL=0
elif check_label "CI: no fast fail"; then
export CI_FAST_FAIL=0
else
export CI_FAST_FAIL=1
fi
fi
if [ "${CI_FAST_FAIL}" = "1" ]; then
export DWQ_MAXFAIL=0
else
export DWQ_MAXFAIL=500
fi
main() {
set_status "fetching code"
export APPS BOARDS NIGHTLY STATIC_TESTS RUN_TESTS
export DWQ_ENV="-E APPS -E BOARDS -E NIGHTLY -E STATIC_TESTS -E RUN_TESTS \
-E CI_MURDOCK_PROJECT -E ENABLE_TEST_CACHE -E CI_FAST_FAIL \
-E FULL_BUILD -ECI_BUILD_BRANCH -ECI_BASE_BRANCH -ECI_BASE_COMMIT"
local repo_dir="RIOT"
if [ -n "${CI_BUILD_COMMIT}" ]; then
export DWQ_REPO="${CI_GIT_URL_WORKER}/${CI_BUILD_REPO_WORKER}"
export DWQ_COMMIT="${CI_BUILD_COMMIT}"
checkout_commit ${repo_dir} ${GITHUB_REPO_URL} ${CI_BUILD_COMMIT}
echo "--- checking out build branch"
local build_branch=${CI_MURDOCK_PROJECT}/build/${CI_BUILD_COMMIT}
git -C ${repo_dir} checkout -B ${build_branch}
git -C ${repo_dir} log -1 --oneline
echo "--- pushing build branch"
git -C ${repo_dir} push --force cache_repo ${build_branch}
echo "--- using build commit SHA1=${CI_BUILD_COMMIT}"
echo "-- done."
if [ -n "${CI_BUILD_BRANCH}" ]; then
echo "-- Building branch ${CI_BUILD_BRANCH} head: ${CI_BUILD_COMMIT}..."
handle_merge_queue_build "${repo_dir}"
elif [ -n "${CI_BUILD_TAG}" ]; then
echo "-- Building tag ${CI_BUILD_TAG} (${CI_BUILD_COMMIT})..."
else
echo "-- Building commit ${CI_BUILD_COMMIT}..."
fi
export CI_WORKER_BRANCH="${build_branch}"
elif [ -n "${CI_PULL_COMMIT}" ]; then
echo "-- PR base branch is ${CI_BASE_BRANCH} at ${CI_BASE_COMMIT}"
local actual_base_head="$(gethead ${CI_BASE_REPO} ${CI_BASE_BRANCH})"
if [ -n "${actual_base_head}" ]; then
if [ "${actual_base_head}" != "${CI_BASE_COMMIT}" ]; then
echo "-- HEAD of ${CI_BASE_BRANCH} is ${actual_base_head}"
export CI_BASE_COMMIT="${actual_base_head}"
fi
fi
echo "-- merging ${CI_PULL_COMMIT} into ${CI_BASE_COMMIT}"
create_merge_commit ${repo_dir} ${CI_BASE_REPO} ${CI_BASE_COMMIT} ${CI_PULL_COMMIT} ${CI_PULL_NR}
echo "--- using merge commit SHA1=${CI_MERGE_COMMIT}"
echo "-- done."
export DWQ_REPO="${CI_GIT_URL_WORKER}/${CI_BUILD_REPO_WORKER}"
export DWQ_COMMIT="${CI_MERGE_COMMIT}"
set_status "checking cluster"
dwqc "test -x .murdock" || {
echo "CI cluster sanity check failed!"
rm -f result.json
exit 2
}
echo "-- Building PR #${CI_PULL_NR} ${CI_PULL_URL} head: ${CI_PULL_COMMIT}..."
export DWQ_ENV="${DWQ_ENV} -E CI_BASE_REPO -E CI_PULL_REPO -E CI_PULL_COMMIT \
-E CI_PULL_NR -E CI_PULL_URL -E CI_PULL_LABELS -E CI_MERGE_COMMIT"
else # Invalid configuration, aborting
echo "Invalid job configuration, return with error"
exit 2
fi
echo "--- Will abort after more than ${DWQ_MAXFAIL} failed job(s)."
local report_queue="status::${CI_JOB_UID}:$(random)"
${BASEDIR}/reporter.py -- "${report_queue}" "${CI_JOB_UID}" "${CI_JOB_TOKEN}" &
local reporter_pid=$!
get_jobs | dwqc ${DWQ_ENV} \
--maxfail ${DWQ_MAXFAIL} \
--quiet --report ${report_queue} --outfile result.json
local build_test_res=$?
sleep 1
kill ${reporter_pid} >/dev/null 2>&1 && wait ${reporter_pid} 2>/dev/null
# Only build Doxygen documentation if the build job was successful
if [ ${build_test_res} -eq 0 ]; then
echo "-- Building Doxygen documentation"
chronic make -C ${repo_dir} doc --no-print-directory
cp -R ${repo_dir}/doc/doxygen/html ./doc-preview
fi
# export result to post-build scripts
if [ ${build_test_res} -eq 0 ]; then
export CI_BUILD_RESULT=success
else
export CI_BUILD_RESULT=failed
fi
# run post-build.d scripts
post_build
if [ -n "${CI_WORKER_BRANCH}" ]; then
echo "-- cleaning up worker branch"
git -C ${repo_dir} push --delete cache_repo ${CI_WORKER_BRANCH}
fi
# remove local copy of repository
rm -rf ${repo_dir}
# Process result.json to generate UI data
${BASEDIR}/process_result.py
echo "-- Compressing result.json"
echo "--- Disk usage before compression: $(du -sh result.json | awk '{print $1}')"
gzip result.json
echo "--- Disk usage after compression : $(du -sh result.json.gz | awk '{print $1}')"
echo "--- Total disk usage: $(du -sh . | awk '{print $1}')"
exit ${build_test_res}
}
main