Skip to content

Commit 4eff7d6

Browse files
committed
Clean up CI script
1 parent 68ce002 commit 4eff7d6

1 file changed

Lines changed: 83 additions & 29 deletions

File tree

.travis.sh

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,95 @@
11
#!/bin/bash
22

3-
# Abort on Error
4-
set -e
3+
# DESCRIPTION #
4+
5+
# Script to run continuous integration on Travis CI.
6+
7+
8+
# VARIABLES #
9+
10+
# Define a heartbeat interval to prevent Travis CI from prematurely ending due to long running commands:
11+
export HEARTBEAT_INTERVAL=30s
12+
13+
# Define the number of lines of logged output to print upon completion:
14+
export TAIL_LINES=500
15+
16+
# Get the current working directory:
17+
export WORKING_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
518

6-
export PING_SLEEP=30s
7-
export WORKDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
8-
export BUILD_OUTPUT=$WORKDIR/build.out
19+
# Define an output file to store log output:
20+
export CI_OUT=$WORKING_DIR/ci.log
921

10-
touch $BUILD_OUTPUT
1122

12-
dump_output() {
13-
echo Tailing the last 500 lines of output:
14-
tail -500 $BUILD_OUTPUT
23+
# FUNCTIONS #
24+
25+
# Starts a heartbeat.
26+
start_heartbeat() {
27+
echo 'Starting heartbeat...'
28+
29+
# Create a heartbeat and send to background:
30+
heartbeat &
31+
32+
# Capture the heartbeat pid:
33+
HEARTBEAT_PID=$!
34+
echo "Heartbeat pid: $HEARTBEAT_PID"
1535
}
16-
error_handler() {
17-
echo ERROR: An error was encountered with the build.
18-
dump_output
19-
kill $PING_LOOP_PID
20-
exit 1
36+
37+
# Runs an infinite print loop in which output is periodically written to `stdout`.
38+
heartbeat() {
39+
while true; do
40+
echo "$(date) - heartbeat...";
41+
sleep $HEARTBEAT_INTERVAL;
42+
done
43+
}
44+
45+
# Stops the heartbeat print loop.
46+
stop_heartbeat() {
47+
echo 'Stopping heartbeat...'
48+
kill $HEARTBEAT_PID
2149
}
22-
# If an error occurs, run our error handler to output a tail of the build
23-
trap 'error_handler' ERR
2450

25-
# Set up a repeating loop to send some output to Travis.
51+
# Defines an error handler.
52+
on_error() {
53+
echo 'ERROR: An error was encountered during execution.'
54+
cleanup
55+
exit 1
56+
}
57+
58+
# Tails the log output.
59+
tail_output() {
60+
echo "Printing the last $TAIL_LINES lines of log output..."
61+
tail -$TAIL_LINES $CI_OUT
62+
}
63+
64+
# Runs clean-up tasks.
65+
cleanup() {
66+
tail_output
67+
stop_heartbeat
68+
}
69+
70+
71+
# MAIN #
72+
73+
# Exit immediately if one of the executed commands exits with a non-zero status:
74+
set -e
75+
76+
# Set an error handler to print captured output and perform any clean-up tasks:
77+
trap 'on_error' ERR
78+
79+
# Create an output log file:
80+
touch $CI_OUT
81+
82+
# Start a heartbeat:
83+
start_heartbeat
2684

27-
bash -c "while true; do echo \$(date) - building ...; sleep $PING_SLEEP; done" &
28-
PING_LOOP_PID=$!
29-
echo $PING_LOOP_PID
85+
# Run CI commands, merging `stderr` into `stdout` and redirecting logged output to file...
86+
echo 'Running local tests...'
87+
make test-local >> $CI_OUT 2>&1
3088

31-
# Build commands:
32-
echo Running local tests...
33-
make test-local >> $BUILD_OUTPUT 2>&1
34-
echo Running coverage...
35-
make coverage >> $BUILD_OUTPUT 2>&1
89+
echo 'Running coverage...'
90+
make coverage >> $CI_OUT 2>&1
3691

37-
# The build finished without returning an error so dump a tail of the output
38-
dump_output
92+
echo 'Success!'
3993

40-
# nicely terminate the ping output loop
41-
kill $PING_LOOP_PID
94+
# Run cleanup tasks:
95+
cleanup

0 commit comments

Comments
 (0)