forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamcity-support.sh
340 lines (307 loc) · 11.2 KB
/
teamcity-support.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
# Common helpers for teamcity-*.sh scripts.
# root is the absolute path to the root directory of the repository.
root="$(dirname $(cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ))"
source "$root/build/teamcity-common-support.sh"
source "$root/build/teamcity/util.sh"
remove_files_on_exit() {
rm -f ~/.ssh/id_rsa{,.pub}
common_support_remove_files_on_exit
}
trap remove_files_on_exit EXIT
# maybe_ccache turns on ccache to speed up compilation, but only for PR builds.
# This speeds up the CI cycle for developers while preventing ccache from
# corrupting a release build.
maybe_ccache() {
if tc_release_branch; then
echo "On release branch ($TC_BUILD_BRANCH), so not enabling ccache."
else
echo "Building PR (#$TC_BUILD_BRANCH), so enabling ccache."
definitely_ccache
fi
}
definitely_ccache() {
run export COCKROACH_BUILDER_CCACHE=1
}
run() {
echo "$@"
"$@"
}
run_counter=-1
# Takes args that produce `go test -json` output. It filters stdout to contain
# only test output related to failing tests and run/pass/skip events for the
# other tests (no output). It writes artifacts/failures.txt containing text
# output for the failing tests.
# It's valid to call this multiple times; all output artifacts will be
# preserved.
function run_json_test() {
run_counter=$((run_counter+1))
tc_start_block "prep"
# TODO(tbg): better to go through builder for all of this.
go install github.com/cockroachdb/cockroach/pkg/cmd/github-post
mkdir -p artifacts
tmpfile="artifacts/raw.${run_counter}.json.txt"
tc_end_block "prep"
tc_start_block "run"
set +e
run "$@" 2>&1 \
| tee "${tmpfile}" \
| (cd "$root"/pkg/cmd/testfilter && go run main.go -mode=strip) \
| tee artifacts/stripped.txt
status=$?
set -e
tc_end_block "run"
# Post issues, if on a release branch. Note that we're feeding github-post all
# of the build output; it also does some slow test analysis.
if tc_release_branch; then
if [ -z "${GITHUB_API_TOKEN-}" ]; then
# GITHUB_API_TOKEN must be in the env or github-post will barf if it's
# ever asked to post, so enforce that on all runs.
# The way this env var is made available here is quite tricky. The build
# calling this method is usually a build that is invoked from PRs, so it
# can't have secrets available to it (for the PR could modify
# build/teamcity-* to leak the secret). Instead, we provide the secrets
# to a higher-level job (Publish Bleeding Edge) and use TeamCity magic to
# pass that env var through when it's there. This means we won't have the
# env var on PR builds, but we'll have it for builds that are triggered
# from the release branches.
echo "GITHUB_API_TOKEN must be set"
exit 1
else
tc_start_block "post issues"
github-post < "${tmpfile}"
tc_end_block "post issues"
fi
fi
tc_start_block "artifacts"
# Create (or append to) failures.txt artifact and delete stripped.txt.
(cd "$root"/pkg/cmd/testfilter && go run main.go -mode=omit) < artifacts/stripped.txt | \
(cd "$root"/pkg/cmd/testfilter && go run main.go -mode=convert) >> artifacts/failures.txt
if [ $status -ne 0 ]; then
# Keep the debug file around for failed builds. Compress it to avoid
# clogging the agents with stuff we'll hopefully rarely ever need to
# look at.
# If the process failed, also save the full human-readable output. This is
# helpful in cases in which tests timed out, where it's difficult to blame
# the failure on any particular test. It's also a good alternative to poking
# around in $tmpfile itself when anything else we don't handle well happens,
# whatever that may be.
fullfile=artifacts/full_output.txt
(cd "$root"/pkg/cmd/testfilter && go run main.go -mode=convert) < "${tmpfile}" >> "${fullfile}"
tar --strip-components 1 -czf "${tmpfile}.tgz" "${tmpfile}" "${fullfile}"
rm -f "${fullfile}"
fi
rm -f "${tmpfile}" artifacts/stripped.txt
# Some unit tests test automatic ballast creation. These ballasts can be
# larger than the maximum artifact size. Remove any artifacts with the
# EMERGENCY_BALLAST filename.
find artifacts -name "EMERGENCY_BALLAST" -delete
tc_end_block "artifacts"
# Make it easier to figure out whether we're exiting because of a test failure
# or because of some auxiliary failure.
tc_start_block "exit status"
echo "test run finished with exit status $status"
tc_end_block "exit status"
return $status
}
function would_stress() {
# Don't stressrace on the release branches; we only want that to happen on the
# PRs. There's no need in making master flakier than it needs to be; nightly
# stress will weed out the flaky tests.
if tc_release_branch; then
return 1
else
return 0
fi
}
function maybe_stress() {
# NB: This code doesn't know about posting Github issues as we don't stress on
# the release branches.
if ! would_stress; then
return 0
fi
target=$1
shift
block="Maybe ${target} pull request"
tc_start_block "${block}"
run build/builder.sh make protobuf
run build/builder.sh go install ./pkg/cmd/github-pull-request-make
run_json_test build/builder.sh env BUILD_VCS_NUMBER="$BUILD_VCS_NUMBER" TARGET="${target}" github-pull-request-make
tc_end_block "${block}"
}
# Returns the list of release branches from origin (origin/release-*), ordered
# by version (higher version numbers first).
get_release_branches() {
# We sort by the minor version first, followed by a stable sort on the major
# version.
git branch -r --format='%(refname)' \
| sed 's/^refs\/remotes\///' \
| grep '^origin\/release-*' \
| sort -t. -k2 -n -r \
| sort -t- -k2 -n -r -s
}
# Returns the number of commits in the curent branch that are not shared with
# the given branch.
get_branch_distance() {
git rev-list --count $1..HEAD
}
# Returns the branch among origin/master, origin/release-* which is the
# closest to the current HEAD.
#
# Suppose the origin looks like this:
#
# e (master)
# |
# d w (release-19.2)
# | |
# c u
# \ /
# \ /
# \ /
# b
# |
# a
#
# Example 1. PR on master on top of d:
#
# e (master) pr
# \ /
# \ /
# \ /
# d w (release-19.2)
# | |
# c u
# \ /
# \ /
# \ /
# b
# |
# a
#
# The pr commit has distance 1 from master and distance 3 from release-19.2
# (commits c, d, and pr); so we deduce that the upstream branch is master.
#
# Example 2. PR on release-19.2 on top of u:
#
# e (master)
# |
# d w (release-19.2)
# | \
# | \ pr
# | \ /
# c u
# \ /
# \ /
# \ /
# b
# |
# a
#
# The pr commit has distance 2 from master (commits u and w) and distance 1 from
# release-19.2; so we deduce that the upstream branch is release-19.2.
#
# If the PR is on top of the fork point (b in the example above), we return the
# release-19.2 branch.
#
# Example 3. PR on even older release:
#
# e (master)
# |
# d w (release-19.2)
# | |
# | |
# | | pr
# c u /
# \ / y (release-19.1)
# \ / /
# \ / /
# b x
# \ /
# \ /
# \ /
# a
#
# The pr commit has distance 3 from both master and release-19.2 (commits x, y,
# pr) and distance 1 from release-19.1. In general, the distance w.r.t. all
# newer releases than the correct one will be equal; specifically, it is the
# number of commits since the fork point of the correct release (the fork point
# in this example is commit a).
#
get_upstream_branch() {
local UPSTREAM DISTANCE D
UPSTREAM="origin/master"
DISTANCE=$(get_branch_distance origin/master)
# Check if we're closer to any release branches. The branches are ordered
# new-to-old, so stop as soon as the distance starts to increase.
for branch in $(get_release_branches); do
D=$(get_branch_distance $branch)
# It is important to continue the loop if the distance is the same; see
# example 3 above.
if [ $D -gt $DISTANCE ]; then
break
fi
UPSTREAM=$branch
DISTANCE=$D
done
echo "$UPSTREAM"
}
changed_go_pkgs() {
git fetch --quiet origin
upstream_branch=$(get_upstream_branch)
# Find changed packages, minus those that have been removed entirely. Note
# that the three-dot notation means we are diffing against the merge-base of
# the two branches, not against the tip of the upstream branch.
git diff --name-only "$upstream_branch..." -- "pkg/**/*.go" ":!*/testdata/*" \
| xargs -rn1 dirname \
| sort -u \
| { while read path; do if ls "$path"/*.go &>/dev/null; then echo -n "./$path "; fi; done; }
}
tc_release_branch() {
[[ "$TC_BUILD_BRANCH" == master || "$TC_BUILD_BRANCH" == release-* || "$TC_BUILD_BRANCH" == provisional_* ]]
}
if_tc() {
if [[ "${TC_BUILD_ID-}" ]]; then
"$@"
fi
}
tc_prepare() {
tc_start_block "Prepare environment"
run export BUILDER_HIDE_GOPATH_SRC=1
run mkdir -p artifacts
maybe_ccache
tc_end_block "Prepare environment"
}
generate_ssh_key() {
if [[ ! -f ~/.ssh/id_rsa.pub ]]; then
ssh-keygen -q -N "" -f ~/.ssh/id_rsa
fi
}
maybe_require_release_justification() {
# Set this to 1 to require a "release justification" note in the commit message
# or the PR description.
require_justification=1
if [ "$require_justification" = 1 ]; then
tc_start_block "Ensure commit message contains a release justification"
# Ensure master branch commits have a release justification.
if [[ $(git log -n1 | grep -ci "Release justification: \S\+") == 0 ]]; then
echo "Build Failed. No Release justification in the commit message or in the PR description." >&2
echo "Commits must have a Release justification of the form:" >&2
echo "Release justification: <some description of why this commit is safe to add to the release branch.>" >&2
exit 1
fi
tc_end_block "Ensure commit message contains a release justification"
fi
}
# Call this function with one argument, the error message to print if the
# workspace is dirty.
check_workspace_clean() {
# The workspace is clean iff `git status --porcelain` produces no output. Any
# output is either an error message or a listing of an untracked/dirty file.
if [[ "$(git status --porcelain 2>&1)" != "" ]]; then
git status >&2 || true
git diff -a >&2 || true
echo "====================================================" >&2
echo "Some automatically generated code is not up to date." >&2
echo $1 >&2
exit 1
fi
}