-
Notifications
You must be signed in to change notification settings - Fork 165
/
buildkite-pipeline.sh
executable file
·384 lines (338 loc) · 11 KB
/
buildkite-pipeline.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env bash
#
# Builds a buildkite pipeline based on the environment variables
#
set -e
cd "$(dirname "$0")"/..
output_file=${1:-/dev/stderr}
if [[ -n $CI_PULL_REQUEST ]]; then
# filter pr number from ci branch.
[[ $CI_BRANCH =~ pull/([0-9]+)/head ]]
pr_number=${BASH_REMATCH[1]}
echo "get affected files from PR: $pr_number"
if [[ $BUILDKITE_REPO =~ ^https:\/\/github\.com\/([^\/]+)\/([^\/\.]+) ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
elif [[ $BUILDKITE_REPO =~ ^git@github\.com:([^\/]+)\/([^\/\.]+) ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
else
echo "couldn't parse owner and repo. use defaults"
owner="anza-xyz"
repo="agave"
fi
# ref: https://github.com/cli/cli/issues/5368#issuecomment-1087515074
#
# Variable value contains dollar prefixed words that look like bash variable
# references. This is intentional.
# shellcheck disable=SC2016
query='
query($owner: String!, $repo: String!, $pr: Int!, $endCursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
files(first: 100, after: $endCursor) {
pageInfo{ hasNextPage, endCursor }
nodes {
path
}
}
}
}
}'
# get affected files
readarray -t affected_files < <(
gh api graphql \
-f query="$query" \
-F pr="$pr_number" \
-F owner="$owner" \
-F repo="$repo" \
--paginate \
--jq '.data.repository.pullRequest.files.nodes.[].path'
)
if [[ ${#affected_files[*]} -eq 0 ]]; then
echo "Unable to determine the files affected by this PR"
exit 1
fi
else
affected_files=()
fi
annotate() {
if [[ -n $BUILDKITE ]]; then
buildkite-agent annotate "$@"
fi
}
# Assume everything needs to be tested when this file or any Dockerfile changes
mandatory_affected_files=()
mandatory_affected_files+=(^ci/buildkite-pipeline.sh)
mandatory_affected_files+=(^ci/docker-rust/Dockerfile)
mandatory_affected_files+=(^ci/docker-rust-nightly/Dockerfile)
# Checks if a CI pull request affects one or more path patterns. Each
# pattern argument is checked in series. If one of them found to be affected,
# return immediately as such.
#
# Bash regular expressions are permitted in the pattern:
# affects .rs$ -- any file or directory ending in .rs
# affects .rs -- also matches foo.rs.bar
# affects ^snap/ -- anything under the snap/ subdirectory
# affects snap/ -- also matches foo/snap/
# Any pattern starting with the ! character will be negated:
# affects !^docs/ -- anything *not* under the docs/ subdirectory
#
affects() {
if [[ -z $CI_PULL_REQUEST ]]; then
# affected_files metadata is not currently available for non-PR builds so assume
# the worse (affected)
return 0
fi
for pattern in "${mandatory_affected_files[@]}" "$@"; do
if [[ ${pattern:0:1} = "!" ]]; then
for file in "${affected_files[@]}"; do
if [[ ! $file =~ ${pattern:1} ]]; then
return 0 # affected
fi
done
else
for file in "${affected_files[@]}"; do
if [[ $file =~ $pattern ]]; then
return 0 # affected
fi
done
fi
done
return 1 # not affected
}
# Checks if a CI pull request affects anything other than the provided path patterns
#
# Syntax is the same as `affects()` except that the negation prefix is not
# supported
#
affects_other_than() {
if [[ -z $CI_PULL_REQUEST ]]; then
# affected_files metadata is not currently available for non-PR builds so assume
# the worse (affected)
return 0
fi
for file in "${affected_files[@]}"; do
declare matched=false
for pattern in "$@"; do
if [[ $file =~ $pattern ]]; then
matched=true
fi
done
if ! $matched; then
return 0 # affected
fi
done
return 1 # not affected
}
start_pipeline() {
echo "# $*" > "$output_file"
echo "steps:" >> "$output_file"
}
command_step() {
cat >> "$output_file" <<EOF
- name: "$1"
command: "$2"
timeout_in_minutes: $3
artifact_paths: "log-*.txt"
agents:
queue: "${4:-solana}"
EOF
}
trigger_secondary_step() {
cat >> "$output_file" <<"EOF"
- name: "Trigger Build on agave-secondary"
trigger: "agave-secondary"
branches: "!pull/*"
async: true
soft_fail: true
build:
message: "${BUILDKITE_MESSAGE}"
commit: "${BUILDKITE_COMMIT}"
branch: "${BUILDKITE_BRANCH}"
env:
TRIGGERED_BUILDKITE_TAG: "${BUILDKITE_TAG}"
EOF
}
wait_step() {
echo " - wait" >> "$output_file"
}
all_test_steps() {
command_step checks1 "ci/docker-run-default-image.sh ci/test-checks.sh" 20 check
command_step checks2 "ci/docker-run-default-image.sh ci/test-dev-context-only-utils.sh check-bins" 15 check
command_step checks3 "ci/docker-run-default-image.sh ci/test-dev-context-only-utils.sh check-all-targets" 15 check
command_step miri "ci/docker-run-default-image.sh ci/test-miri.sh" 5 check
command_step frozen-abi "ci/docker-run-default-image.sh ./test-abi.sh" 15 check
wait_step
# Full test suite
.buildkite/scripts/build-stable.sh >> "$output_file"
# Docs tests
if affects \
.rs$ \
Cargo.lock$ \
Cargo.toml$ \
^ci/rust-version.sh \
^ci/test-docs.sh \
; then
command_step doctest "ci/docker-run-default-image.sh ci/test-docs.sh" 15
else
annotate --style info --context test-docs \
"Docs skipped as no .rs files were modified"
fi
wait_step
# SBF test suite
if affects \
.rs$ \
Cargo.lock$ \
Cargo.toml$ \
^ci/rust-version.sh \
^ci/test-stable-sbf.sh \
^ci/test-stable.sh \
^ci/test-local-cluster.sh \
^core/build.rs \
^fetch-perf-libs.sh \
^programs/ \
^sdk/ \
cargo-build-sbf$ \
cargo-test-sbf$ \
; then
cat >> "$output_file" <<"EOF"
- command: "ci/docker-run-default-image.sh ci/test-stable-sbf.sh"
name: "stable-sbf"
timeout_in_minutes: 35
artifact_paths: "sbf-dumps.tar.bz2"
agents:
queue: "solana"
EOF
else
annotate --style info \
"Stable-SBF skipped as no relevant files were modified"
fi
# Downstream backwards compatibility
if affects \
.rs$ \
Cargo.lock$ \
Cargo.toml$ \
^ci/rust-version.sh \
^ci/test-stable-perf.sh \
^ci/test-stable.sh \
^ci/test-local-cluster.sh \
^core/build.rs \
^fetch-perf-libs.sh \
^programs/ \
^sdk/ \
cargo-build-sbf$ \
cargo-test-sbf$ \
^ci/downstream-projects \
.buildkite/scripts/build-downstream-projects.sh \
; then
.buildkite/scripts/build-downstream-projects.sh >> "$output_file"
else
annotate --style info \
"downstream-projects skipped as no relevant files were modified"
fi
# Wasm support
if affects \
^ci/test-wasm.sh \
^ci/test-stable.sh \
^sdk/ \
; then
command_step wasm "ci/docker-run-default-image.sh ci/test-wasm.sh" 20
else
annotate --style info \
"wasm skipped as no relevant files were modified"
fi
# Benches...
if affects \
.rs$ \
Cargo.lock$ \
Cargo.toml$ \
^ci/rust-version.sh \
^ci/test-coverage.sh \
^ci/test-bench.sh \
^ci/bench \
.buildkite/scripts/build-bench.sh \
; then
.buildkite/scripts/build-bench.sh >> "$output_file"
else
annotate --style info --context test-bench \
"Bench skipped as no .rs files were modified"
fi
# Coverage...
if affects \
.rs$ \
Cargo.lock$ \
Cargo.toml$ \
^ci/rust-version.sh \
^ci/test-coverage.sh \
^scripts/coverage.sh \
; then
command_step coverage "ci/docker-run-default-image.sh ci/test-coverage.sh" 80
else
annotate --style info --context test-coverage \
"Coverage skipped as no .rs files were modified"
fi
}
pull_or_push_steps() {
command_step sanity "ci/test-sanity.sh" 5 check
wait_step
# Check for any .sh file changes
if affects \
.sh$ \
^.buildkite/hooks \
; then
command_step shellcheck "ci/shellcheck.sh" 5 check
wait_step
fi
# Version bump PRs are an edge case that can skip most of the CI steps
if affects .toml$ && affects .lock$ && ! affects_other_than .toml$ .lock$; then
optional_old_version_number=$(git diff origin/"$BUILDKITE_PULL_REQUEST_BASE_BRANCH"..HEAD validator/Cargo.toml | \
grep -e "^-version" | sed 's/-version = "\(.*\)"/\1/')
echo "optional_old_version_number: ->$optional_old_version_number<-"
new_version_number=$(grep -e "^version = " validator/Cargo.toml | sed 's/version = "\(.*\)"/\1/')
echo "new_version_number: ->$new_version_number<-"
# Every line in a version bump diff will match one of these patterns. Since we're using grep -v the output is the
# lines that don't match. Any diff that produces output here is not a version bump.
# | cat is a no-op. If this pull request is a version bump then grep will output no lines and have an exit code of 1.
# Piping the output to cat prevents that non-zero exit code from exiting this script
diff_other_than_version_bump=$(git diff origin/"$BUILDKITE_PULL_REQUEST_BASE_BRANCH"..HEAD | \
grep -vE "^ |^@@ |^--- |^\+\+\+ |^index |^diff |^-( \")?solana.*$optional_old_version_number|^\+( \")?solana.*$new_version_number|^-version|^\+version"|cat)
echo "diff_other_than_version_bump: ->$diff_other_than_version_bump<-"
if [ -z "$diff_other_than_version_bump" ]; then
echo "Diff only contains version bump."
command_step checks "ci/docker-run-default-image.sh ci/test-checks.sh" 20
exit 0
fi
fi
# Run the full test suite by default, skipping only if modifications are local
# to some particular areas of the tree
if affects_other_than ^.mergify .md$ ^docs/ ^.gitbook; then
all_test_steps
fi
# docs changes run on Github actions...
}
if [[ -n $BUILDKITE_TAG ]]; then
start_pipeline "Tag pipeline for $BUILDKITE_TAG"
annotate --style info --context release-tag \
"https://github.com/jito-foundation/jito-solana/releases/$BUILDKITE_TAG"
# Jump directly to the secondary build to publish release artifacts quickly
trigger_secondary_step
exit 0
fi
if [[ $BUILDKITE_BRANCH =~ ^pull ]]; then
echo "+++ Affected files in this PR"
for file in "${affected_files[@]}"; do
echo "- $file"
done
start_pipeline "Pull request pipeline for $BUILDKITE_BRANCH"
# Add helpful link back to the corresponding Github Pull Request
annotate --style info --context pr-backlink \
"Github Pull Request: https://github.com/jito-foundation/jito-solana/$BUILDKITE_BRANCH"
pull_or_push_steps
exit 0
fi
start_pipeline "Push pipeline for ${BUILDKITE_BRANCH:-?unknown branch?}"
pull_or_push_steps
wait_step
trigger_secondary_step
exit 0