-
Notifications
You must be signed in to change notification settings - Fork 1
/
log-ci.sh
executable file
·102 lines (74 loc) · 1.73 KB
/
log-ci.sh
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
#!/usr/bin/env bash
set -e
log="log.txt"
# Use the first argument as the
# project directory.
dir=$1
if [ ! -d "$dir" ]; then
echo "Project directory $dir does not exist"
exit 1
fi
if [ ! -w "$log" ]; then
echo "Log file $log is not writable"
exit 1
fi
config="$dir/.log-ci-config"
last_build="$dir/.log-ci-last-build"
# Loads the project configuration.
if [ ! -r "$config" ]; then
echo "Directory $dir contains no config file"
exit 1
fi
source "$config"
# Checks configuration.
if [[ -z "$project" ]]; then
echo "Project name is not set"
exit 1
fi
if [[ -z "$build" ]]; then
echo "Build command is not set"
exit 1
fi
if [[ -z "$remote" ]]; then
echo "Git remote name is not set"
exit 1
fi
if [[ -z "$branch" ]]; then
echo "Git branch name is not set"
exit 1
fi
# Redirects all log into the log file.
exec >> $log 2>&1
# Updates the local copy.
# Prepends the project name to log messages.
git --git-dir="$dir/.git" \
--work-tree="$dir" \
pull \
"$remote" \
"$branch" 2>&1 \
| sed "s/^/\[$project\]: /g"
# Finds the last commit date.
commit=`git --git-dir="$dir/.git" \
--work-tree="$dir" \
log -1 --format=%ci`
# Finds the last build date.
last="0000-00-00 00:00:00 +0000"
if [ -e "$last_build" ]; then
last=`cat "$last_build"`
fi
# Builds the project when there are
# new changes.
if [[ "$commit" > "$last" ]]; then
echo "[$project]: new changes"
echo "[$project]: building"
cd "$dir"
eval "$build" 2>&1 | sed "s/^/\[$project\]: /g"
if [ ${PIPESTATUS[0]} -eq 0 ]; then
echo "[$project]: success"
echo "$commit" > "$last_build"
else
echo "[$project]: fail"
fi
else
echo "[$project]: no new changes"
fi