Sync with 'maint'
[git/debian.git] / ci / lib.sh
blob2f6d9d26e4033a927d9dcbaadbcc29c533cae5b4
1 # Library of functions shared by all CI scripts
3 if test true != "$GITHUB_ACTIONS"
4 then
5 begin_group () { :; }
6 end_group () { :; }
8 group () {
9 shift
10 "$@"
12 set -x
13 else
14 begin_group () {
15 need_to_end_group=t
16 echo "::group::$1" >&2
17 set -x
20 end_group () {
21 test -n "$need_to_end_group" || return 0
22 set +x
23 need_to_end_group=
24 echo '::endgroup::' >&2
26 trap end_group EXIT
28 group () {
29 set +x
30 begin_group "$1"
31 shift
32 "$@"
33 res=$?
34 end_group
35 return $res
38 begin_group "CI setup"
41 # Set 'exit on error' for all CI scripts to let the caller know that
42 # something went wrong.
44 # We already enabled tracing executed commands earlier. This helps by showing
45 # how # environment variables are set and and dependencies are installed.
46 set -e
48 skip_branch_tip_with_tag () {
49 # Sometimes, a branch is pushed at the same time the tag that points
50 # at the same commit as the tip of the branch is pushed, and building
51 # both at the same time is a waste.
53 # When the build is triggered by a push to a tag, $CI_BRANCH will
54 # have that tagname, e.g. v2.14.0. Let's see if $CI_BRANCH is
55 # exactly at a tag, and if so, if it is different from $CI_BRANCH.
56 # That way, we can tell if we are building the tip of a branch that
57 # is tagged and we can skip the build because we won't be skipping a
58 # build of a tag.
60 if TAG=$(git describe --exact-match "$CI_BRANCH" 2>/dev/null) &&
61 test "$TAG" != "$CI_BRANCH"
62 then
63 echo "$(tput setaf 2)Tip of $CI_BRANCH is exactly at $TAG$(tput sgr0)"
64 exit 0
68 # Save some info about the current commit's tree, so we can skip the build
69 # job if we encounter the same tree again and can provide a useful info
70 # message.
71 save_good_tree () {
72 echo "$(git rev-parse $CI_COMMIT^{tree}) $CI_COMMIT $CI_JOB_NUMBER $CI_JOB_ID" >>"$good_trees_file"
73 # limit the file size
74 tail -1000 "$good_trees_file" >"$good_trees_file".tmp
75 mv "$good_trees_file".tmp "$good_trees_file"
78 # Skip the build job if the same tree has already been built and tested
79 # successfully before (e.g. because the branch got rebased, changing only
80 # the commit messages).
81 skip_good_tree () {
82 if test true = "$GITHUB_ACTIONS"
83 then
84 return
87 if ! good_tree_info="$(grep "^$(git rev-parse $CI_COMMIT^{tree}) " "$good_trees_file")"
88 then
89 # Haven't seen this tree yet, or no cached good trees file yet.
90 # Continue the build job.
91 return
94 echo "$good_tree_info" | {
95 read tree prev_good_commit prev_good_job_number prev_good_job_id
97 if test "$CI_JOB_ID" = "$prev_good_job_id"
98 then
99 cat <<-EOF
100 $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0)
101 This commit has already been built and tested successfully by this build job.
102 To force a re-build delete the branch's cache and then hit 'Restart job'.
104 else
105 cat <<-EOF
106 $(tput setaf 2)Skipping build job for commit $CI_COMMIT.$(tput sgr0)
107 This commit's tree has already been built and tested successfully in build job $prev_good_job_number for commit $prev_good_commit.
108 The log of that build job is available at $SYSTEM_TASKDEFINITIONSURI$SYSTEM_TEAMPROJECT/_build/results?buildId=$prev_good_job_id
109 To force a re-build delete the branch's cache and then hit 'Restart job'.
114 exit 0
117 check_unignored_build_artifacts () {
118 ! git ls-files --other --exclude-standard --error-unmatch \
119 -- ':/*' 2>/dev/null ||
121 echo "$(tput setaf 1)error: found unignored build artifacts$(tput sgr0)"
122 false
126 handle_failed_tests () {
127 return 1
130 # GitHub Action doesn't set TERM, which is required by tput
131 export TERM=${TERM:-dumb}
133 # Clear MAKEFLAGS that may come from the outside world.
134 export MAKEFLAGS=
136 if test -n "$SYSTEM_COLLECTIONURI" || test -n "$SYSTEM_TASKDEFINITIONSURI"
137 then
138 CI_TYPE=azure-pipelines
139 # We are running in Azure Pipelines
140 CI_BRANCH="$BUILD_SOURCEBRANCH"
141 CI_COMMIT="$BUILD_SOURCEVERSION"
142 CI_JOB_ID="$BUILD_BUILDID"
143 CI_JOB_NUMBER="$BUILD_BUILDNUMBER"
144 CI_OS_NAME="$(echo "$AGENT_OS" | tr A-Z a-z)"
145 test darwin != "$CI_OS_NAME" || CI_OS_NAME=osx
146 CI_REPO_SLUG="$(expr "$BUILD_REPOSITORY_URI" : '.*/\([^/]*/[^/]*\)$')"
147 CC="${CC:-gcc}"
149 # use a subdirectory of the cache dir (because the file share is shared
150 # among *all* phases)
151 cache_dir="$HOME/test-cache/$SYSTEM_PHASENAME"
153 export GIT_PROVE_OPTS="--timer --jobs 10 --state=failed,slow,save"
154 export GIT_TEST_OPTS="--verbose-log -x --write-junit-xml"
155 MAKEFLAGS="$MAKEFLAGS --jobs=10"
156 test windows_nt != "$CI_OS_NAME" ||
157 GIT_TEST_OPTS="--no-chain-lint --no-bin-wrappers $GIT_TEST_OPTS"
158 elif test true = "$GITHUB_ACTIONS"
159 then
160 CI_TYPE=github-actions
161 CI_BRANCH="$GITHUB_REF"
162 CI_COMMIT="$GITHUB_SHA"
163 CI_OS_NAME="$(echo "$RUNNER_OS" | tr A-Z a-z)"
164 test macos != "$CI_OS_NAME" || CI_OS_NAME=osx
165 CI_REPO_SLUG="$GITHUB_REPOSITORY"
166 CI_JOB_ID="$GITHUB_RUN_ID"
167 CC="${CC_PACKAGE:-${CC:-gcc}}"
168 DONT_SKIP_TAGS=t
169 handle_failed_tests () {
170 mkdir -p t/failed-test-artifacts
171 echo "FAILED_TEST_ARTIFACTS=t/failed-test-artifacts" >>$GITHUB_ENV
173 for test_exit in t/test-results/*.exit
175 test 0 != "$(cat "$test_exit")" || continue
177 test_name="${test_exit%.exit}"
178 test_name="${test_name##*/}"
179 printf "\\e[33m\\e[1m=== Failed test: ${test_name} ===\\e[m\\n"
180 echo "The full logs are in the artifacts attached to this run."
181 cat "t/test-results/$test_name.markup"
183 trash_dir="t/trash directory.$test_name"
184 cp "t/test-results/$test_name.out" t/failed-test-artifacts/
185 tar czf t/failed-test-artifacts/"$test_name".trash.tar.gz "$trash_dir"
186 done
187 return 1
190 cache_dir="$HOME/none"
192 export GIT_PROVE_OPTS="--timer --jobs 10"
193 export GIT_TEST_OPTS="--verbose-log -x --github-workflow-markup"
194 MAKEFLAGS="$MAKEFLAGS --jobs=10"
195 test windows != "$CI_OS_NAME" ||
196 GIT_TEST_OPTS="--no-chain-lint --no-bin-wrappers $GIT_TEST_OPTS"
197 else
198 echo "Could not identify CI type" >&2
199 env >&2
200 exit 1
203 good_trees_file="$cache_dir/good-trees"
205 mkdir -p "$cache_dir"
207 test -n "${DONT_SKIP_TAGS-}" ||
208 skip_branch_tip_with_tag
209 skip_good_tree
211 if test -z "$jobname"
212 then
213 jobname="$CI_OS_NAME-$CC"
216 export DEVELOPER=1
217 export DEFAULT_TEST_TARGET=prove
218 export GIT_TEST_CLONE_2GB=true
219 export SKIP_DASHED_BUILT_INS=YesPlease
221 case "$runs_on_pool" in
222 ubuntu-latest)
223 if test "$jobname" = "linux-gcc-default"
224 then
225 break
228 if [ "$jobname" = linux-gcc ]
229 then
230 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/python3"
231 else
232 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/python2"
235 export GIT_TEST_HTTPD=true
237 # The Linux build installs the defined dependency versions below.
238 # The OS X build installs much more recent versions, whichever
239 # were recorded in the Homebrew database upon creating the OS X
240 # image.
241 # Keep that in mind when you encounter a broken OS X build!
242 export LINUX_P4_VERSION="16.2"
243 export LINUX_GIT_LFS_VERSION="1.5.2"
245 P4_PATH="$HOME/custom/p4"
246 GIT_LFS_PATH="$HOME/custom/git-lfs"
247 export PATH="$GIT_LFS_PATH:$P4_PATH:$PATH"
249 macos-latest)
250 if [ "$jobname" = osx-gcc ]
251 then
252 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=$(which python3)"
253 else
254 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=$(which python2)"
257 esac
259 case "$jobname" in
260 linux32)
261 CC=gcc
263 linux-musl)
264 CC=gcc
265 MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/python3 USE_LIBPCRE2=Yes"
266 MAKEFLAGS="$MAKEFLAGS NO_REGEX=Yes ICONV_OMITS_BOM=Yes"
267 MAKEFLAGS="$MAKEFLAGS GIT_TEST_UTF8_LOCALE=C.UTF-8"
269 linux-leaks)
270 export SANITIZE=leak
271 export GIT_TEST_PASSING_SANITIZE_LEAK=true
273 esac
275 MAKEFLAGS="$MAKEFLAGS CC=${CC:-cc}"
277 end_group
278 set -x