-
Notifications
You must be signed in to change notification settings - Fork 5
/
post-receive
49 lines (40 loc) · 1.82 KB
/
post-receive
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
#!/bin/bash
#
# Git post-receive hook
#
# Check whether build server is alive. If not, issue Wake-on-Lan packet and wait up to 60 seconds
# for port 22 to become available. Wait up to another minute for the Jenkins Dashboard to be returned
# (over HTTPs) and then trigger a build with git commit message as cause.
#
read oldrev newrev refname
buildServer="jenkins.bikeshed.internal"
token="myJenkinsToken"
project="biscuit.ninja%20website"
/bin/nc -z -w 1 $buildServer 22 && buildServerOnline=true
[ $buildServerOnline ] || echo "!! #>>>> INFO: Build server off-line. Issuing WoL packet <<<<# !!"
for ((i=1;i<=10;i++)) ; do
[ $buildServerOnline ] && break
echo "!! #>>>> INFO: ${buildserver} off-line. Retry in 6s. Attempt ${i}/10 <<<<# !!"
/usr/bin/wakeonlan c0:3f:d5:64:b7:b7 &> /dev/null
sleep 6
/bin/nc -z -w 1 $buildServer 22 && buildServerOnline=true
done
if ! [ $buildServerOnline ] ; then
echo "!! #>>>> INFO: Build server unavailable. <<<<# !!"
echo "!! #>>>> INFO: No build triggered. <<<<# !!"
exit
fi
for ((i=1;i<=10;i++)) ; do
/usr/bin/curl -ks https://$buildServer | grep 'Dashboard \[Jenkins\]' &>/dev/null && jenkinsDashboardAvailable=true
[ $jenkinsDashboardAvailable ] && break
echo "!! #>>>> INFO: Jenkins off-line. Retry in 6s. Attempt ${i}/10 <<<<# !!"
sleep 6
done
if ! [ $jenkinsDashboardAvailable ] ; then
echo "!! #>>>> INFO: Jenkins dashboard unresponsive. <<<<# !!"
echo "!! #>>>> INFO: No build triggered. <<<<# !!"
exit
fi
echo "!! #>>>> INFO: Build server is running. Jenkins dashboard available. <<<<# !!"
cause=$(echo $(git log $oldrev..$newrev --pretty="format:%ci %cn %h %s" | /usr/bin/xxd -plain | /usr/bin/tr -d '\n' | sed 's/\(..\)/%\1/g'))
/usr/bin/curl -s -k "https://${buildServer}/job/${project}/build?token=${token}&cause=${cause}"