-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-receive
executable file
·153 lines (114 loc) · 4.14 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
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
#!/bin/sh
#
# Copyright (c) 2012 Errol Sayre
#
# A globally usable post-receive deployment hook for Git, designed
# to be used within Gitolite.
#
# This script is freely available for your modification with
# attribution.
#
# This script utilizes git config values to determine how/where to
# export content from a bare repo to some other destination via
# rsync+ssh.
#
# Namespace your config section however you wish by changing the value
# below in GIT_CONF_SECTION.
#
# At minimum, you must set simpledeploy.enable=true and
# simpledeploy.rsync-destination to an rsync+ssh "user@host:path" value.
# Utilizing this default, rsync will use .gitignore as the argument for
# --exclude-from unless one is set via *.rsync-exclude configuration.
#
# Since the default rsync operation uses --delete, it is required that
# any files on the destination that must not be removed (or overwritten)
# be listed in .gitignore or another exclude file as configured.
# --------------------#
# Begin Configuration #
# --------------------#
GIT_CONF_SECTION="simpledeploy"
EMAIL_SCRIPT="$HOME/.gitolite/local-code/hooks/common/post-receive-email"
# ------------------#
# End Configuration #
# ------------------#
# look for the git directory to make use of config values
GIT_DIR=`git rev-parse --git-dir 2>/dev/null`
if [ -z "$GIT_DIR" ]; then
echo >&2 "SimpleDeploy failed: Unable to locate Git directory."
exit 1
fi
# Read Configuration Data for deployment
deploy=`git config "$GIT_CONF_SECTION".enable`
: ${deploy:="false"}
destination=`git config "$GIT_CONF_SECTION".rsync-destination`
exclude_file=`git config "$GIT_CONF_SECTION".rsync-exclude`
: ${exclude_file:=".gitignore"}
post_sync_host=`git config "$GIT_CONF_SECTION".post-sync-host`
email=`git config hooks.mailinglist`
# Check to see if the email script should execute
# Do this first since this email doesn't include any output from the deployment. At a later date email functionality may be built-in.
if [ "$email" ]; then
. "$EMAIL_SCRIPT"
fi
# Check for configuration before continuing
if [ "$deploy" != "true" ]; then
echo "SimpleDeploy is not enabled on this (remote) repo."
exit 0
fi
# Pre-sync commands
# None currently supported.
# Before creating a temp directory, ensure the requested destination is supported
if [ -z "$destination" ]; then
echo "SimpleDeploy failed: Currently only rsync+ssh URLs are supported."
echo " git config ${GIT_CONF_SECTION}.rsync-destination user@host:path"
exit 1
fi
# Only work on bare repositories, creating a temporary working directory
working=""
# This mktemp call has a wonky structure because I needed it to function the same on OS X and Solaris.
if [ `git rev-parse --is-bare-repository` ]; then
temp=`mktemp -d -t git-working.XXXXXXXX`
working="$temp/contents"
mkdir "$working"
if [ ! -d "$working" ]; then
echo >&2 "SimpleDeploy failed: Unable to create working directory."
exit 1
fi
else
echo >&2 "SimpleDeploy failed: Currently only bare repositories are supported."
exit 1
fi
# Export the current contents to the working directory
echo "Exporting content via git checkout -f"
GIT_WORK_TREE="$working" git checkout -f
# Look for an rsync exclude file as defined in the config
exclude=""
exclude_test="${working}/${exclude_file}"
if [ -f "$exclude_test" ]; then
exclude="$exclude_test"
fi
# Issue the rsync command if applicable
if [ "$destination" ]; then
echo "Syncing content from working directory to destination:"
if [ "$exclude" ]; then
echo " rsync -av --delete --exclude-from=$exclude $working/ $destination"
rsync -av --delete --exclude-from="$exclude" "$working/" "$destination"
else
echo " rsync -av --delete $working/ $destination"
rsync -av --delete "$working/" "$destination"
fi
fi
# Remove the temporary directory if used
if [ -d "$temp" ]; then
rm -rf "$temp"
fi
# Post-sync commands
# Currently only crontab deployment is supported.
if [ "$post_sync_host" ]; then
echo "Checking for post-sync commands to send to $post_sync_host"
post_sync_crontab=`git config "$GIT_CONF_SECTION".post-sync-crontab`
if [ "$post_sync_crontab" ]; then
echo "Post-sync crontab specified"
ssh "$post_sync_host" "crontab $post_sync_crontab;crontab -l"
fi
fi