| ︙ | | |
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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
|
-
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
|
fi
# 0. Resolve paths
repository="$(cd "$(dirname "${repository}")" && echo "$(pwd)/$(basename "${repository}")")" || exit 1
workdir="$(mkdir -p "${workdir}" && cd "${workdir}" && pwd)" || exit 1
# 1. Register cleanup
tmpdir=''
tmpdirs=()
function cleanup() {
local tmpdir
for tmpdir in "${tmpdirs[@]}"; do
case "${tmpdir}" in
/*)
if [ -n "${tmpdir}" ]; then
rm -rf "${tmpdir}"
fi
if [ -n "${tmpdir}" -a -d "${tmpdir}" ]; then
rm -rf "${tmpdir}"
fi
;;
esac
done
}
trap cleanup EXIT
# 2. Setup a sane environment
## 2.a. Abort on errors
set -e
|
| ︙ | | |
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
|
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
|
-
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
# 4. Get a list of branches
branches=( $(fossil branch -R "${repository}" list) )
# 5. Get Fossil CI configuration from the trunk branch
## 5.a. Set default config
### 5.a.i. Set default variables
excludedBranches=()
includedBranches=('')
includedBranches=()
excludedBuilders=()
includedBuilders=()
buildCommands=('./autogen.sh || :' ./configure make)
initCommands=()
postCommands=()
branchPostCommands=()
testCommands=()
buildArtifacts=()
builderID=''
projectName="$(fossil info -R "${repository}" | awk '/^project-name:/{ sub(/^project-name: */, ""); gsub(/ /, ""); print; }')"
tagSuffix=''
tagPrefix=''
fossilUser='fossil-ci'
env=()
# tagOmit='all|pass|redundant|none'
tagOmit='none'
# buildResultsUpload='fail|all|none'
buildResultsUpload='fail'
# testResultsUpload='fail|all|none'
testResultsUpload='fail'
# postResultsUpload='fail|all|none'
postResultsUpload='all'
### 5.a.ii. Default functions
function uploadBuildResults() {
### XXX:TODO
:
local branch="$1"
local checkout="$2"
local buildlog="$3"
uploadResultsLog "${branch}" "${checkout}" "${buildlog}" "build.txt"
}
function uploadTestResults() {
local branch="$1"
local checkout="$2"
local testlog="$3"
### XXX:TODO
:
}
uploadResultsLog "${branch}" "${checkout}" "${testlog}" "test.txt"
}
function uploadPostResults() {
local branch="$1"
local checkout="$2"
local postlog="$3"
uploadResultsLog "${branch}" "${checkout}" "${postlog}" "post.txt"
}
function uploadResultsLog() {
local branch="$1"
local checkout="$2"
local log="$3"
local name="$4"
local uvName="build/output/${branch}/${checkout}/logs/${name}"
fossilUploadUV "${log}" "${uvName}" || return 1
echo "${uvName}"
}
function uploadBuildArtifacts() {
local branch="$1"
local checkout="$2"
local branchdir="$3"
shift; shift; shift
local artifacts=("$@")
### XXX:TODO
:
local name path
for artifact in "${artifacts[@]}"; do
case "${artifact}" in
*=*)
path="$(echo "${artifact}" | cut -d '=' -f 1)"
name="$(echo "${artifact}" | cut -d '=' -f 2-)"
;;
*)
path="${artifact}"
name="${artifact}"
;;
esac
if [ -f "${branchdir}/${path}" ]; then
fossilUploadUV "${branchdir}/${path}" "build/output/${branch}/${checkout}/artifacts/${name}"
fi
done
}
function fossilSetWikiPage() {
local page="$1"
local contents="$2"
### XXX:TODO
:
}
( echo "${contents}" | fossil wiki create -R "${repository}" "${page}" --mimetype text/x-markdown 2>/dev/null >/dev/null ) || \
( echo "${contents}" | fossil wiki commit -R "${repository}" "${page}" --mimetype text/x-markdown 2>/dev/null >/dev/null ) || return 1
}
function fossilSetCheckinWikiPage() {
local checkin="$1"
local contents="$2"
fossilSetWikiPage "checkin/${checkin}" "${contents}"
}
function fossilUploadUV() {
local file="$1"
local name="$2"
### XXX:TODO
:
fossil uv add "${file}" --as "${name}" -R "${repository}"
}
function setupEnv() {
local envChange
local oldEnv
local envOverrideFile
# Clear all environment variables except for a few safe ones
oldEnv="$(export -p | sed 's@^declare -x @@')"
unset $(echo "${oldEnv}" | cut -f 1 -d = | grep -Ev '^(TZ|SHELL|TMPDIR|HOME|PATH|USER|FOSSIL_CI_RUN_ID|FOSSIL_CI_CURRENT_BRANCH|FOSSIL_CI_CURRENT_PROJECT|FOSSIL_CI_CURRENT_REPOSITORY|FOSSIL_CI_CURRENT_BUILDER)$')
envOverrideFile="${TMPDIR}/.fossil-ci-env-add-${FOSSIL_CI_RUN_ID}"
if [ -f "${envOverrideFile}" ]; then
local envOverride
mapfile envOverride < "${envOverrideFile}"
env+=("${envOverride[@]}")
fi
for envChange in "${env[@]}"; do
case "${envChange}" in
*=*)
export "${envChange}"
;;
-*)
unset "${envChange:1}"
;;
!HOME|!PATH|!USER|!TMPDIR|!TZ|!SHELL)
# Don't allow unsetting or restoring of critical environment variables
echo "Ignoring attempt to unset or restore critical environment variable: ${envChange}" >&2
;;
!*)
# Restore the old value of the environment variable, if it was set
eval "$(echo "${oldEnv}" | while IFS= read -r line; do
var="$(echo "${line}" | cut -f 1 -d =)"
if [ "${var}" = "${envChange:1}" ]; then
echo "export ${line}"
fi
done)"
;;
*)
echo "Invalid environment variable: ${envChange}" >&2
;;
esac
done
# XXX:TODO: Support secrets
}
## 5.b. Read config
config="$(fossil cat -R "${repository}" -r trunk "${fossilCIDir}/config" 2>/dev/null)" || :
## 5.c Load config
if [ -f ~/.fossil-ci/config ]; then
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
-
+
+
+
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
+
+
+
+
+
+
-
+
-
+
+
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
+
+
+
+
+
-
+
+
|
### 5.d.i. Add builderID as a tag suffix if none was given
if [ -z "${tagSuffix}" ]; then
if [ -n "${builderID}" ]; then
tagSuffix="-${builderID}"
fi
fi
# 6. Perform any configured initialization
for cmd in "${initCommands[@]}"; do
eval "${cmd}" >/dev/null 2>/dev/null || :
done
# 7. For each branch, attempt to build
# 6. Create non-overridable commands
function addEnv() (
set +x
local var="$1"
local value="$2"
echo "${var}=${value@Q}" >> "${TMPDIR}/.fossil-ci-env-add-${FOSSIL_CI_RUN_ID}"
)
for branch in "${branches[@]}"; do
## 7.a. Only process specific branches
ignoreBranch='1'
for testBranch in "${includedBranches[@]}"; do
function setupHome() {
local runTmpDir
if echo "${branch}" | grep "${testBranch}" >/dev/null; then
ignoreBranch='0'
export TZ='UTC'
export SHELL='/bin/bash'
export TMPDIR="${workdir}/tmp"
runTmpDir="$(mktemp -d)"
tmpdirs+=("${runTmpDir}")
break
fi
done
for testBranch in "${excludedBranches[@]}"; do
if echo "${branch}" | grep "${testBranch}" >/dev/null; then
ignoreBranch='1'
# Setup Home and TMPDIR to be within the workdir, so that any files created there will be automatically cleaned up when the workdir is cleaned up
export HOME="${runTmpDir}/home"
export TMPDIR="${runTmpDir}/tmp"
mkdir -p "${TMPDIR}" "${HOME}"
}
function runBranch() (
set -e -o pipefail
break
fi
done
trap cleanup EXIT
branch="$1"
if [ "${ignoreBranch}" = '1' ]; then
continue
export FOSSIL_CI_CURRENT_BRANCH="${branch}"
export FOSSIL_CI_CURRENT_PROJECT="${projectName}"
export FOSSIL_CI_CURRENT_REPOSITORY="${repository}"
export FOSSIL_CI_CURRENT_BUILDER="${builderID}"
export FOSSIL_CI_RUN_ID="$(uuidgen -r)"
fi
## 7.b. Find the branch working directory
branchdir="$(echo "${branch}" | sed 's@[^A-Za-z0-9-]@_@g')"
branchdir="${branchdir}-$(echo "${branch}" | openssl dgst -sha256 | sed 's@^.*= *@@' | cut -c 1-16)"
builddir="${workdir}/build-output/branch-${branchdir}"
branchdir="${workdir}/working-copies/branch-${branchdir}"
mkdir -p "${branchdir}"
## 7.c. Determine current checkout ID
checkout="$(fossil info -R "${repository}" "${branch}" | awk '/^uuid:/{ print $3 "T" $4 "-" $2 }')"
checkout="$(fossil info -R "${repository}" "${branch}" | awk '/^hash:/{ print $3 "T" $4 "-" $2 }')"
fullCheckout="$(fossil timeline -R "${repository}" --branch "${branch}" --format %H --type ci --limit 1 | head -n 1)"
builddir="${builddir}/checkout-${checkout}"
## 7.d. Determine if build even needs to occur
if [ -d "${builddir}" ]; then
continue
exit 0
fi
mkdir -p "${builddir}"
## 7.d. Create other directories as needed
mkdir -p "${workdir}/tmp"
## 7.d Setup the environment for the branch
setupHome
## 7.e. Check-out or update the branch
(
cd "${branchdir}" || exit 1
if [ ! -f '.fslckout' ]; then
fossil open "${repository}" "${branch}" --nested
else
fossil update "${branch}"
fi
) > "${builddir}/update.log" 2>&1 || continue
) > "${builddir}/update.log" 2>&1 || exit 0
## 7.f. Build and log the results, if there are any
build_pass='1'
(
cd "${branchdir}" || exit 1
for cmd in "${buildCommands[@]}"; do
(
# Prepare the environment for each step
setupEnv || exit 1
( eval "set -x; ${cmd}" ) || exit 1
eval "set -x; ${cmd}"
) || exit 1
done
) > "${builddir}/build.log" 2>&1 || build_pass='0'
### 7.g. Test the branch
tests_pass='-1'
if [ "${build_pass}" = '1' ]; then
tests_pass='1'
(
cd "${branchdir}" || exit 1
for cmd in "${testCommands[@]}"; do
(
# Prepare the environment for each step
setupEnv || exit 1
( eval "set -x; ${cmd}" ) || exit 1
eval "set -x; ${cmd}"
) || exit 1
done
) > "${builddir}/test.log" 2>&1 || tests_pass='0'
fi
### 7.h. Tag the branch with
tagsToAdd=()
if [ "${build_pass}" = '1' ]; then
|
| ︙ | | |
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
|
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
fi
tagsToAddOpts=()
for tag in "${tagsToAdd[@]}"; do
tagsToAddOpts=("${tagsToAddOpts[@]}" --tag "${tagPrefix}${tag}${tagSuffix}")
done
if [ "${#tagsToAddOpts[@]}" -gt 0 ]; then
fossil amend -R "${repository}" "${branch}" "${tagsToAddOpts[@]}" > "${builddir}/update.log" 2>&1
fossil amend -R "${repository}" "${branch}" --user-override "${fossilUser}" "${tagsToAddOpts[@]}" > "${builddir}/update.log" 2>&1
fi
### 7.i. Upload the logs somewhere if requested
uploadedLogByPhase_build=''
uploadedLogByPhase_test=''
uploadedLogByPhase_post=''
uploadBuildResults='0'
case "${buildResultsUpload}-${build_pass}" in
all-0|all-1|fail-0|pass-1)
uploadBuildResults='1'
;;
esac
uploadTestResults='0'
case "${buildResultsUpload}-${tests_pass}" in
all-0|all-1|fail-0|pass-1)
uploadTestResults='1'
;;
esac
uploadPostResults='0'
case "${postResultsUpload}-${build_pass}" in
all-0|all-1|fail-0|pass-1)
uploadPostResults='1'
;;
esac
if [ "${uploadBuildResults}" ]; then
uploadBuildResults "${branch}" "${checkout}" "${builddir}/build.log" || :
uploadedLogByPhase_build="$(uploadBuildResults "${branch}" "${checkout}" "${builddir}/build.log" || :)"
fi
if [ "${uploadBuildResults}" ]; then
uploadTestResults "${branch}" "${checkout}" "${builddir}/test.log" || :
if [ "${uploadTestResults}" ]; then
uploadedLogByPhase_test="$(uploadTestResults "${branch}" "${checkout}" "${builddir}/test.log" || :)"
fi
### 7.j. Upload build artifacts somewhere if requested
uploadBuildArtifacts "${branch}" "${checkout}" "${branchdir}" || :
uploadBuildArtifacts "${branch}" "${checkout}" "${branchdir}" "${buildArtifacts[@]}" || :
### 7.k. Get a list of tags
tags=( $(fossil tag -R "${repository}" list "${branch}") )
# 8. Perform post-build work (e.g., for release engineering)
(
cd "${branchdir}" || exit 1
for cmd in "${branchPostCommands[@]}"; do
( eval "set -x; ${cmd}" ) || exit 1
done
) > "${builddir}/post.log" 2>&1 || :
if [ "${uploadPostResults}" ]; then
if [ -s "${builddir}/post.log" ]; then
uploadedLogByPhase_post="$(uploadPostResults "${branch}" "${checkout}" "${builddir}/post.log" || :)"
fi
fi
if true; then
uploadedLogs=()
if [ -n "${uploadedLogByPhase_build}" ]; then
uploadedLogs+=("${uploadedLogByPhase_build}")
fi
if [ -n "${uploadedLogByPhase_test}" ]; then
uploadedLogs+=("${uploadedLogByPhase_test}")
fi
if [ -n "${uploadedLogByPhase_post}" ]; then
uploadedLogs+=("${uploadedLogByPhase_post}")
fi
page="$(
echo "# Build results for ${branch} [${fullCheckout}]"
echo '## Build status'
if [ "${build_pass}" = '1' ]; then
echo '- Build: PASS'
if [ -n "${tests_pass}" ]; then
if [ "${tests_pass}" = '1' ]; then
echo '- Tests: PASS'
else
echo '- Tests: FAIL'
fi
fi
else
echo '- Build: FAIL'
echo '- Tests: N/A'
fi
echo ''
echo '## Logs'
if [ "${#uploadedLogs[@]}" -gt 0 ]; then
if [ -n "${uploadedLogByPhase_build}" ]; then
echo "- [Build](/uv/${uploadedLogByPhase_build})"
fi
if [ -n "${uploadedLogByPhase_test}" ]; then
echo "- [Test](/uv/${uploadedLogByPhase_test})"
fi
if [ -n "${uploadedLogByPhase_post}" ]; then
echo "- [Post-build](/uv/${uploadedLogByPhase_post})"
fi
else
echo '*(none)*'
fi
echo ''
echo '## Artifacts'
if [ "${#buildArtifacts[@]}" -gt 0 ]; then
for artifact in "${buildArtifacts[@]}"; do
echo "- ${artifact} $(ls -l "${branchdir}/${artifact}" 2>/dev/null || echo '(not found)')"
done
done
else
echo '*(none)*'
fi
)"
fossilSetCheckinWikiPage "${fullCheckout}" "${page}"
fi
)
# 6. Create our user and set the default user to that
if ! fossil user default -R "${repository}" "${fossilUser}" >/dev/null 2>/dev/null; then
fossil user new -R "${repository}" "${fossilUser}" "${fossilUser}" "Fossil CI" --no-password >/dev/null 2>/dev/null
fossil user default -R "${repository}" "${fossilUser}" || (
echo "Failed to create default user ${fossilUser}" >&2
exit 1
) || exit 1
fi
# 6. Perform any configured initialization
for cmd in "${initCommands[@]}"; do
eval "${cmd}" >/dev/null 2>/dev/null || :
done
# 7. For each branch, attempt to build
for branch in "${branches[@]}"; do
## 7.a. Skip builders that we don't want
ignoreBranch='0'
for testBuilder in "${includedBuilders[@]}"; do
if echo "${branch}" | grep "${testBuilder}" >/dev/null; then
ignoreBranch='0'
break
fi
done
for testBuilder in "${excludedBuilders[@]}"; do
if echo "${branch}" | grep "${testBuilder}" >/dev/null; then
ignoreBranch='1'
break
fi
done
if [ "${ignoreBranch}" = '1' ]; then
continue
fi
unset ignoreBranch testBuilder
## 7.a. Only process specific branches
ignoreBranch='0'
for testBranch in "${includedBranches[@]}"; do
if echo "${branch}" | grep "${testBranch}" >/dev/null; then
ignoreBranch='0'
break
fi
done
for testBranch in "${excludedBranches[@]}"; do
if echo "${branch}" | grep "${testBranch}" >/dev/null; then
ignoreBranch='1'
break
fi
done
if [ "${ignoreBranch}" = '1' ]; then
continue
fi
unset ignoreBranch testBranch
runBranch "${branch}"
done
# 9. Clean up any branches that no longer exist
## XXX:TODO
|