Raw
1 # Library of functions shared by all CI scripts
2
3 if test true = "$GITHUB_ACTIONS"
4 then
5 begin_group () {
6 need_to_end_group=t
7 echo "::group::$1" >&2
8 set -x
9 }
10
11 end_group () {
12 test -n "$need_to_end_group" || return 0
13 set +x
14 need_to_end_group=
15 echo '::endgroup::' >&2
16 }
17 elif test true = "$GITLAB_CI"
18 then
19 begin_group () {
20 need_to_end_group=t
21 printf '\e[0Ksection_start:%s:%s[collapsed=true]\r\e[0K%s\n' \
22 "$(date +%s)" "$(echo "$1" | tr ' ' _)" "$1"
23 trap "end_group '$1'" EXIT
24 set -x
25 }
26
27 end_group () {
28 test -n "$need_to_end_group" || return 0
29 set +x
30 need_to_end_group=
31 printf '\e[0Ksection_end:%s:%s\r\e[0K\n' \
32 "$(date +%s)" "$(echo "$1" | tr ' ' _)"
33 trap - EXIT
34 }
35 else
36 begin_group () { :; }
37 end_group () { :; }
38
39 set -x
40 fi
41
42 group () {
43 group="$1"
44 shift
45 begin_group "$group"
46
47 # work around `dash` not supporting `set -o pipefail`
48 (
49 "$@" 2>&1
50 echo $? >exit.status
51 ) |
52 sed 's/^\(\([^ ]*\):\([0-9]*\):\([0-9]*:\) \)\(error\|warning\): /::\5 file=\2,line=\3::\1/'
53 res=$(cat exit.status)
54 rm exit.status
55
56 end_group "$group"
57 return $res
58 }
59
60 begin_group "CI setup via $(basename $0)"
61
62 # Set 'exit on error' for all CI scripts to let the caller know that
63 # something went wrong.
64 #
65 # We already enabled tracing executed commands earlier. This helps by showing
66 # how # environment variables are set and dependencies are installed.
67 set -e
68
69 skip_branch_tip_with_tag () {
70 # Sometimes, a branch is pushed at the same time the tag that points
71 # at the same commit as the tip of the branch is pushed, and building
72 # both at the same time is a waste.
73 #
74 # When the build is triggered by a push to a tag, $CI_BRANCH will
75 # have that tagname, e.g. v2.14.0. Let's see if $CI_BRANCH is
76 # exactly at a tag, and if so, if it is different from $CI_BRANCH.
77 # That way, we can tell if we are building the tip of a branch that
78 # is tagged and we can skip the build because we won't be skipping a
79 # build of a tag.
80
81 if TAG=$(git describe --exact-match "$CI_BRANCH" 2>/dev/null) &&
82 test "$TAG" != "$CI_BRANCH"
83 then
84 echo "$(tput setaf 2)Tip of $CI_BRANCH is exactly at $TAG$(tput sgr0)"
85 exit 0
86 fi
87 }
88
89 # Check whether we can use the path passed via the first argument as Git
90 # repository.
91 is_usable_git_repository () {
92 # We require Git in our PATH, otherwise we cannot access repositories
93 # at all.
94 if ! command -v git >/dev/null
95 then
96 return 1
97 fi
98
99 # And the target directory needs to be a proper Git repository.
100 if ! git -C "$1" rev-parse 2>/dev/null
101 then
102 return 1
103 fi
104 }
105
106 # Save some info about the current commit's tree, so we can skip the build
107 # job if we encounter the same tree again and can provide a useful info
108 # message.
109 save_good_tree () {
110 if ! is_usable_git_repository .
111 then
112 return
113 fi
114
115 echo "$(git rev-parse $CI_COMMIT^{tree}) $CI_COMMIT $CI_JOB_NUMBER $CI_JOB_ID" >>"$good_trees_file"
116 # limit the file size
117 tail -1000 "$good_trees_file" >"$good_trees_file".tmp
118 mv "$good_trees_file".tmp "$good_trees_file"
119 }
120
121 # Skip the build job if the same tree has already been built and tested
122 # successfully before (e.g. because the branch got rebased, changing only
123 # the commit messages).
124 skip_good_tree () {
125 if test true = "$GITHUB_ACTIONS"
126 then
127 return
128 fi
129
130 if ! is_usable_git_repository .
131 then
132 return
133 fi
134
135 if ! good_tree_info="$(grep "^$(git rev-parse $CI_COMMIT^{tree}) " "$good_trees_file")"
136 then
137 # Haven't seen this tree yet, or no cached good trees file yet.
138 # Continue the build job.
139 return
140 fi
141
142 echo "$good_tree_info" | {
143 read tree prev_good_commit prev_good_job_number prev_good_job_id
144
145 if test "$CI_JOB_ID" = "$prev_good_job_id"
146 then
147 cat <<-EOF
148 $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0)
149 This commit has already been built and tested successfully by this build job.
150 To force a re-build delete the branch's cache and then hit 'Restart job'.
151 EOF
152 else
153 cat <<-EOF
154 $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0)
155 This commit's tree has already been built and tested successfully in build job $prev_good_job_number for commit $prev_good_commit.
156 The log of that build job is available at $SYSTEM_TASKDEFINITIONSURI$SYSTEM_TEAMPROJECT/_build/results?buildId=$prev_good_job_id
157 To force a re-build delete the branch's cache and then hit 'Restart job'.
158 EOF
159 fi
160 }
161
162 exit 0
163 }
164
165 check_unignored_build_artifacts () {
166 if ! is_usable_git_repository .
167 then
168 return
169 fi
170
171 ! git ls-files --other --exclude-standard --error-unmatch \
172 -- ':/*' 2>/dev/null ||
173 {
174 echo "$(tput setaf 1)error: found unignored build artifacts$(tput sgr0)"
175 false
176 }
177 }
178
179 handle_failed_tests () {
180 return 1
181 }
182
183 create_failed_test_artifacts () {
184 mkdir -p "${TEST_OUTPUT_DIRECTORY:-t}"/failed-test-artifacts
185
186 for test_exit in "${TEST_OUTPUT_DIRECTORY:-t}"/test-results/*.exit
187 do
188 test 0 != "$(cat "$test_exit")" || continue
189
190 test_name="${test_exit%.exit}"
191 test_name="${test_name##*/}"
192 printf "\\e[33m\\e[1m=== Failed test: ${test_name} ===\\e[m\\n"
193 echo "The full logs are in the 'print test failures' step below."
194 echo "See also the 'failed-tests-*' artifacts attached to this run."
195 cat "${TEST_OUTPUT_DIRECTORY:-t}/test-results/$test_name.markup"
196
197 trash_dir="${TEST_OUTPUT_DIRECTORY:-t}/trash directory.$test_name"
198 cp "${TEST_OUTPUT_DIRECTORY:-t}/test-results/$test_name.out" "${TEST_OUTPUT_DIRECTORY:-t}"/failed-test-artifacts/
199 tar czf "${TEST_OUTPUT_DIRECTORY:-t}/failed-test-artifacts/$test_name.trash.tar.gz" "$trash_dir"
200 done
201 }
202
203 # GitHub Action doesn't set TERM, which is required by tput
204 export TERM=${TERM:-dumb}
205
206 # Clear MAKEFLAGS that may come from the outside world.
207 export MAKEFLAGS=
208
209 if test true = "$GITHUB_ACTIONS"
210 then
211 CI_TYPE=github-actions
212 CI_BRANCH="$GITHUB_REF"
213 CI_COMMIT="$GITHUB_SHA"
214 CI_OS_NAME="$(echo "$RUNNER_OS" | tr A-Z a-z)"
215 test macos != "$CI_OS_NAME" || CI_OS_NAME=osx
216 CI_REPO_SLUG="$GITHUB_REPOSITORY"
217 CI_JOB_ID="$GITHUB_RUN_ID"
218 CC="${CC_PACKAGE:-${CC:-gcc}}"
219 DONT_SKIP_TAGS=t
220 handle_failed_tests () {
221 echo "FAILED_TEST_ARTIFACTS=${TEST_OUTPUT_DIRECTORY:-t}/failed-test-artifacts" >>$GITHUB_ENV
222 create_failed_test_artifacts
223 return 1
224 }
225
226 cache_dir="$HOME/none"
227
228 GIT_TEST_OPTS="--github-workflow-markup"
229 JOBS=10
230
231 distro=$(echo "$CI_JOB_IMAGE" | tr : -)
232 elif test true = "$GITLAB_CI"
233 then
234 # This environment is multiple kB in size and may cause us to exceed
235 # xargs(1) limits on Windows.
236 unset GITLAB_FEATURES
237
238 CI_TYPE=gitlab-ci
239 CI_BRANCH="$CI_COMMIT_REF_NAME"
240 CI_COMMIT="$CI_COMMIT_SHA"
241
242 case "$OS,$CI_JOB_IMAGE" in
243 Windows_NT,*)
244 CI_OS_NAME=windows
245 JOBS=$NUMBER_OF_PROCESSORS
246 ;;
247 *,macos-*)
248 # GitLab CI has Python installed via multiple package managers,
249 # most notably via asdf and Homebrew. Ensure that our builds
250 # pick up the Homebrew one by prepending it to our PATH as the
251 # asdf one breaks tests.
252 export PATH="$(brew --prefix)/bin:$PATH"
253
254 CI_OS_NAME=osx
255 JOBS=$(nproc)
256 ;;
257 *,almalinux:*|*,alpine:*|*,debian:*|*,fedora:*|*,ubuntu:*|*,i386/ubuntu:*)
258 CI_OS_NAME=linux
259 JOBS=$(nproc)
260 ;;
261 *)
262 echo "Could not identify OS image" >&2
263 env >&2
264 exit 1
265 ;;
266 esac
267 CI_REPO_SLUG="$CI_PROJECT_PATH"
268 CI_JOB_ID="$CI_JOB_ID"
269 CC="${CC_PACKAGE:-${CC:-gcc}}"
270 DONT_SKIP_TAGS=t
271
272 handle_failed_tests () {
273 create_failed_test_artifacts
274 return 1
275 }
276
277 cache_dir="$HOME/none"
278
279 distro=$(echo "$CI_JOB_IMAGE" | tr : -)
280 else
281 echo "Could not identify CI type" >&2
282 env >&2
283 exit 1
284 fi
285
286 MAKEFLAGS="$MAKEFLAGS --jobs=$JOBS"
287 GIT_PROVE_OPTS="--timer --jobs $JOBS"
288
289 GIT_TEST_OPTS="$GIT_TEST_OPTS --verbose-log -x"
290 case "$CI_OS_NAME" in
291 windows|windows_nt)
292 GIT_TEST_OPTS="$GIT_TEST_OPTS --no-chain-lint --no-bin-wrappers"
293 ;;
294 esac
295
296 export GIT_TEST_OPTS
297 export GIT_PROVE_OPTS
298
299 good_trees_file="$cache_dir/good-trees"
300
301 mkdir -p "$cache_dir"
302
303 test -n "${DONT_SKIP_TAGS-}" ||
304 skip_branch_tip_with_tag
305 skip_good_tree
306
307 if test -z "$jobname"
308 then
309 jobname="$CI_OS_NAME-$CC"
310 fi
311
312 export DEVELOPER=1
313 export DEFAULT_TEST_TARGET=prove
314 export GIT_TEST_CLONE_2GB=true
315 export SKIP_DASHED_BUILT_INS=YesPlease
316
317 # In order to give maximum test coverage to contributor builds,
318 # preferrably even before the changes consume public review bandwidth,
319 # enable "expensive" tests for PR events.
320 # In order to catch bugs introduced at integration time by mismerges,
321 # enable the long tests for pushes to the integration branches as well.
322 case "$GITHUB_EVENT_NAME,$CI_BRANCH" in
323 pull_request,*|push,*next*|push,*master*|push,*main*|push,*maint*)
324 export GIT_TEST_LONG=YesPlease
325 ;;
326 esac
327
328 case "$distro" in
329 ubuntu-*)
330 # Python 2 is end of life, and Ubuntu 23.04 and newer don't actually
331 # have it anymore. We thus only test with Python 2 on older LTS
332 # releases.
333 if test "$distro" = "ubuntu-20.04"
334 then
335 PYTHON_PACKAGE=python2
336 else
337 PYTHON_PACKAGE=python3
338 fi
339 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/$PYTHON_PACKAGE"
340
341 export GIT_TEST_HTTPD=true
342
343 # The Linux build installs the defined dependency versions below.
344 # The OS X build installs much more recent versions, whichever
345 # were recorded in the Homebrew database upon creating the OS X
346 # image.
347 # Keep that in mind when you encounter a broken OS X build!
348 export LINUX_GIT_LFS_VERSION="1.5.2"
349 ;;
350 macos-*)
351 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=$(which python3)"
352 if [ "$jobname" != osx-gcc ]
353 then
354 MAKEFLAGS="$MAKEFLAGS APPLE_COMMON_CRYPTO_SHA1=Yes"
355 fi
356 ;;
357 esac
358
359 CUSTOM_PATH="${CUSTOM_PATH:-$HOME/path}"
360 export PATH="$CUSTOM_PATH:$PATH"
361
362 case "$jobname" in
363 linux32)
364 CC=gcc
365 ;;
366 linux-meson)
367 MESONFLAGS="$MESONFLAGS -Dcredential_helpers=libsecret,netrc"
368 ;;
369 linux-musl-meson)
370 MESONFLAGS="$MESONFLAGS -Dtest_utf8_locale=C.UTF-8"
371 ;;
372 linux-leaks|linux-reftable-leaks)
373 export SANITIZE=leak
374 export NO_CVS_TESTS=LetsSaveSomeTime
375 export NO_SVN_TESTS=LetsSaveSomeTime
376 export NO_P4_TESTS=LetsSaveSomeTime
377 ;;
378 linux-asan-ubsan)
379 export SANITIZE=address,undefined
380 export NO_SVN_TESTS=LetsSaveSomeTime
381 MAKEFLAGS="$MAKEFLAGS NO_PYTHON=YepBecauseP4FlakesTooOften"
382 ;;
383 osx-meson)
384 MESONFLAGS="$MESONFLAGS -Dcredential_helpers=osxkeychain"
385 ;;
386 windows-*)
387 export NO_RUST=UnfortunatelyYes
388 ;;
389 esac
390
391 MAKEFLAGS="$MAKEFLAGS CC=${CC:-cc}"
392
393 end_group "CI setup via $(basename $0)"
394 set -x