-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
129 lines (97 loc) · 2.13 KB
/
build
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
#
# Perform builds, based on a user-provided script
#
#
# Grab a directory necessary for a build
#
allocate()
{
local WHEN=$(date +%Y%m%d)
while read COUNT; do
TRY="$BUILDS/$WHEN.$COUNT"
if mkdir "$TRY" 2> /dev/null; then
echo "$TRY"
return 0
fi
done < <(seq --equal-width 0 99)
error "Too many builds today"
return 1
}
#
# This is the message mailed to the user
#
message()
{
local FINGERPRINT="$1"; shift
local SCRATCH="$1"; shift
local LOG="$1"; shift
local FULL=$(readlink -f "$LOG")
echo "Build failure, fingerprint $FINGERPRINT"
echo "-> $SCRATCH"
echo
cat "$LOG"
echo
echo "-- "
echo "luthier"
}
#
# Some commands always run on exit, whether success or failure
#
cleanup()
{
# If any premature exit happens, our default action
# is to remove trace of our action.
if [ -n "$SCRATCH" ]; then
rm -rf "$SCRATCH"
fi
}
trap cleanup 0
#
# The build directory tracks some state between builds
#
if ! [ -e "$STATE" ]; then
verbose "Bootstrapping new build directory"
mkdir "$STATE" # don't make the tree; the parent must exit
touch "$STATE/current"
fi
SCRATCH=$(allocate)
METADATA="$SCRATCH/.luthier"
mkdir "$METADATA"
verbose "Fetching sources to $SCRATCH"
if ! callout fetch "$METADATA/fetch"; then
cat "$METADATA/fetch" # the scratch dir will be discarded
exit 1
fi
#
# Don't take up disk space if there's nothing to do
#
NEXT=$(callout fingerprint)
PREV=$(cat "$STATE/current")
if [ "$NEXT" = "$PREV" ]; then
verbose "No change to sources; no work to do"
exit 0
fi
echo "$NEXT" > "$STATE/current"
echo "$NEXT" > "$METADATA/fingerprint"
#
# Work out who we would notify for failures. Always do this;
# we don't want to find out on a failure that it's broken.
#
BLAME=$(callout blame)
#
# Go ahead and action the build
#
verbose "Building"
if callout build "$METADATA/build"; then
verbose "Build was successful"
STATUS=complete
else
verbose "Build failed; check $METADATA/build"
STATUS=fail
message "$NEXT" "$SCRATCH" "$METADATA/build" |
mail -s "Build failure: $NEXT" $BLAME
fi
echo "$STATUS" > "$METADATA/status"
echo "$STATUS" > "$STATE/status"
# This process completed, so keep the data
SCRATCH=