-
Notifications
You must be signed in to change notification settings - Fork 20
/
cheapci
executable file
·226 lines (201 loc) · 7.02 KB
/
cheapci
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
#!/bin/bash
# Simple CI script. Checks out a repository, checks for any changes, and runs a passed-in test command assumed to be in the repository.
# If the test passes,
# Options. Note the pipefail.
set -o pipefail -o nounset
# Global variables in bold by convention.
# Defaults
FORCE=0
VERBOSE=0
REPO=""
EMAIL=""
NAME="ci"
TEST_DIR="."
TEST_COMMAND="./test.sh"
MAIL_CMD="mail"
MAIL_CMD_ATTACH_FLAG="-A"
MAIL_CMD_RECIPIENTS_FLAG="-t"
MAIL_CMD_SUBJECT_FLAG="-s"
PRE_SCRIPT="/bin/true"
POST_SCRIPT="/bin/true"
TIMEOUT_S=86400
# Variable defaults
PAGER=${PAGER:-more}
# Functions
function show_help() {
# Here doc, note _not_ in quotes, as we want vars to be substituted in.
# Note that the here doc uses <<- to allow tabbing (must use tabs)
# Note argument zero used here
cat > /dev/stdout <<- END
${0} -r <repo> -l <local_checkout> [-q <pre-script>] [-w <post-script>]
[-m <email>] [-a <mail command>] [-t <mail command attach flag>]
[-s <mail command subject flag] [-e <recipients flag>] [-n name] [-d <dir>]
[-c <command>] [-f] [-v] [-h]
REQUIRED ARGS:
-r - git repository, eg https://github.com/myname/myproj (required)
-l - local checkout of code (that gets updated to determine whether a run is needed) (required)
OPTIONAL ARGS:
-q - script to run just before actually performing test (default ${PRE_SCRIPT})
-w - script to run just after actually performing test (default ${POST_SCRIPT})
-m - email address to send to using "mail" command (default logs to stdout)
-a - mail command to use (default=${MAIL_CMD})
-n - name for ci (unique, must be a valid directory name), eg myproj (default=${NAME})
-d - directory within repository to navigate to (default=${TEST_DIR})
-c - test command to run from -d directory (default=${TEST_COMMAND})
-t - attach argument flag for mail command (default=${MAIL_CMD_ATTACH_FLAG}, empty string means no-attach)
-s - subject flag for mail command (default=${MAIL_CMD_RECIPIENTS_FLAG})
-e - recipients flag (default=${MAIL_CMD_RECIPIENTS_FLAG}, empty string means no flag needed)
-f - force a run even if repo has no updates (default off)
-v - verbose logging (default off)
-i - timeout in seconds (default 86400, ie one day, does KILL one hour after that)
-h - show help
EXAMPLES
- "Clone -r https://github.com/ianmiell/shutit.git if a git pull on /space/git/shutit indicates there's been an update.
Then navigate to test, run ./test.sh and mail ian.miell@gmail.com if there are any issues"
./cheapci -r https://github.com/ianmiell/shutit.git -l /space/git/shutit -d test -c ./test.sh -m ian.miell@gmail.com
- "Run this continuously in a crontab."
Crontab line:
* * * * * cd /path/to/cheapci && ./cheapci -r https://github.com/ianmiell/shutit.git -l /space/git/shutit -d test -c ./test.sh -m ian.miell@gmail.com
END
}
# while loop, and getopts
while getopts "h?vfm:n:d:r:l:c:a:q:w:t:e:s:i:" opt
do
# case statement
case "${opt}" in
h|\?)
show_help
# exit code
exit 0
;;
v) VERBOSE=1 ;;
f) FORCE=1 ;;
r) REPO=${OPTARG} ;;
m) EMAIL=${OPTARG} ;;
n) NAME=${OPTARG} ;;
d) TEST_DIR=${OPTARG} ;;
l) LOCAL_CHECKOUT=${OPTARG} ;;
c) TEST_COMMAND=${OPTARG} ;;
q) PRE_SCRIPT=${OPTARG} ;;
w) POST_SCRIPT=${OPTARG} ;;
a) MAIL_CMD=${OPTARG} ;;
t) MAIL_CMD_ATTACH_FLAG=${OPTARG} ;;
e) MAIL_CMD_RECIPIENTS_FLAG=${OPTARG} ;;
s) MAIL_CMD_SUBJECT_FLAG=${OPTARG} ;;
i) TIMEOUT_S=${OPTARG} ;;
esac
done
# shift builtin - look it up
shift "$((OPTIND-1))"
# if statement, double square bracket
if [[ ${REPO} = "" ]]
then
show_help
# exit with a non-zero exit code
exit 1
fi
# Set verbosity on if wanted
if [[ ${VERBOSE} -gt 0 ]]
then
set -x
fi
# readonly, best practice as these should not get overwritten
readonly BUILD_DIR_BASE="/tmp/${NAME}"
readonly BUILD_DIR="${BUILD_DIR_BASE}/${NAME}_builddir"
mkdir -p "${BUILD_DIR}"
readonly LOG_FILE="${BUILD_DIR}/${NAME}_build_${RANDOM}.log.txt"
readonly BUILD_LOG_FILE="${BUILD_DIR}/${NAME}_build.log.txt"
readonly LOCK_FILE="${BUILD_DIR}/${NAME}_ci.lck"
function cleanup() {
rm -rf "${BUILD_DIR}"
rm -f "${LOCK_FILE}"
# Get rid of any possible build folder detritus from eg failed builds, leaving anything accessed < 1 days ago
# Redirect errors to special /dev/null file
find "${BUILD_DIR_BASE}"/* -type d -atime -1 -exec rm {} -rf \; 2>/dev/null
echo "cleanup done"
}
# Send email. First argument is expected to be the message.
function send_mail() {
local msg
msg="${1}"
if [[ ${LOG_FILE} != "" ]] && [[ ${MAIL_CMD_ATTACH_FLAG} != "" ]]
then
# parentheses used to group quoted items
log_file_arg=("${MAIL_CMD_ATTACH_FLAG}" "${LOG_FILE}")
fi
if [[ "${EMAIL}" != "" ]] && [[ "${MAIL_CMD}" != "" ]]
then
echo "${msg}" | ${MAIL_CMD} "${MAIL_CMD_SUBJECT_FLAG}" "${msg}" "${log_file_arg[@]}" "${MAIL_CMD_RECIPIENTS_FLAG}" "${EMAIL}"
else
echo "${msg}"
fi
}
# Redirect stderr to whatever stdout is pointed at. Pipe everything using tee to append to the log file.
date 2>&1 | tee -a "${BUILD_LOG_FILE}"
# If the lockfile exists, quite
if [[ -a "${LOCK_FILE}" ]]
then
echo "Another instance of $0 is already running, quitting" | tee -a "${BUILD_LOG_FILE}"
exit
fi
# Trap any signals, calling the cleanup function defined above.
trap cleanup TERM INT QUIT EXIT
# Create the lock file
touch "${LOCK_FILE}"
pushd "${LOCAL_CHECKOUT}" > /dev/null || exit 1
# Fetch changes
git fetch origin master 2>&1 | tee -a "${BUILD_LOG_FILE}"
# See if there are any changes since last run
readonly UPDATES="$(git log HEAD..origin/master --oneline | wc -l)"
echo "Number of updates: ${UPDATES}" | tee -a "${BUILD_LOG_FILE}"
# If there are updates, or we have specified to force a run...
if [[ ${UPDATES} -gt 0 ]] || [[ ${FORCE} -gt 0 ]]
then
touch "${LOG_FILE}"
pushd "${LOCAL_CHECKOUT}" > /dev/null || exit 1
echo "Pulling" | tee -a "${LOG_FILE}"
git pull origin master 2>&1 | tee -a "${LOG_FILE}"
popd > /dev/null || exit 1
# This won't exist in a bit so no point pushd'ing
pushd "${BUILD_DIR}" > /dev/null || exit 1
# Clone to NAME
git clone "${REPO}" "${NAME}"
popd > /dev/null || exit 1
${PRE_SCRIPT} 2>&1 | tee -a "${LOG_FILE}"
EXIT_CODE="${?}"
if [[ ${EXIT_CODE} -ne 0 ]]
then
MSG="ANGRY ${NAME} on $(hostname)"
fi
# Go to the test directory.
pushd "${BUILD_DIR}"/"${NAME}"/"${TEST_DIR}" > /dev/null || exit 1
# Timeout runs a command with a time limit. Run the test command.
timeout "${TIMEOUT_S}" "${TEST_COMMAND}" 2>&1 | tee -a "${LOG_FILE}"
# Get the exit code of the test command.
EXIT_CODE=$?
popd > /dev/null || exit 1
# If it did not pass, report as such.
if [[ ${EXIT_CODE} -ne 0 ]]
then
# 124 is the exit code for a timeout, so add that to the message.
if [[ ${EXIT_CODE} -eq 124 ]]
then
MSG="ANGRY (TIMEOUT) ${NAME} on $(hostname)"
else
MSG="ANGRY ${NAME} on $(hostname)"
fi
else
MSG="HAPPY ${NAME} on $(hostname)"
fi
# Run the post script
${POST_SCRIPT} 2>&1 | tee -a "${LOG_FILE}"
EXIT_CODE=$?
if [[ ${EXIT_CODE} -ne 0 ]]
then
MSG="ANGRY ${NAME} on $(hostname) for post_script: ${POST_SCRIPT}"
fi
# Send the email
send_mail "${MSG}"
fi
# Pop the pushd we did before the git fetch above.
popd > /dev/null || exit 1