apenwarr / gitbuilder

Auto-builds and tests all the branches of your git projects, showing pass/fail results on a web page/RSS feed. Isolates failures to the first commit that caused the problem.

gitbuilder / run-build.sh
100755 83 lines (67 sloc) 1.476 kb
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
#!/bin/bash
 
if [ -z "$1" ]; then
echo "Usage: $0 <commitid>" >&2
echo " Build the tree in build/ as of the given commitid."
exit 1
fi
 
ref="$1"
 
mkdir -p out/fail out/pass
chmod 777 out/fail
 
log()
{
(echo; echo ">>> $@") # log file
}
 
_run()
{
log "Starting at: $(date)"
commit="$1"
log "Commit: $commit"
 
cd build || return 10
 
log "Switching git branch..."
git checkout . # in case there were modified files
git checkout "$commit" &&
git reset --hard $commit ||
git reset --hard $commit || return 20
 
log "Cleaning..."
git clean -q -f -x -d ||
git clean -q -f -x -d || return 30
 
log "Building..."
../build.sh 2>&1 || return 40
 
log "Done at: $(date)"
return 0
}
 
run()
{
( _run "$@" )
CODE=$?
log "Result code: $CODE"
return $CODE
}
 
go()
{
echo $ref >out/.doing
rm -f out/pass/$ref out/fail/$ref
run $ref
CODE=${PIPESTATUS[0]}
 
# This whole program's output is being dumped in log.out. Unix
# lets us rename open files, so we can do that here.
# FIXME: it would be cleaner if the caller renamed the output
# file based on our result code, however.
if [ "$CODE" = 0 ]; then
echo PASS
mv -v log.out out/pass/$ref
else
echo FAIL
mv -v log.out out/fail/$ref
fi
 
echo "Done: $ref"
rm -f out/.doing
return $CODE
}
 
set -m
go &
XPID=$!
trap "echo 'Killing (SIGINT)'; kill -TERM -$XPID; exit 1" SIGINT
trap "echo 'Killing (SIGTERM)'; kill -TERM -$XPID; exit 1" SIGTERM
wait $XPID
# return exit code from previous command