-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·302 lines (262 loc) · 7.84 KB
/
deploy.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
set -e
#
# Calls build.sh and git clone on the specified hosting repository;
# uses git to gather changes from host repository in the current build;
# adds changes to host's git repository
#
# Usage: ./scripts/deploy.sh from the profile main directory.
#
ORIGIN=$(pwd)
SKIP=$1
source settings/config.sh
usage()
{
cat << EOF
usage: $0 options
./scripts/deploy.sh # of commits from made directly on host since last push
./scripts/deploy.sh -b <branch>
OPTIONS:
-h Show this message
-b Branch to checkout (optional)
-m Commit message (optional)
-y Skip confirmation
EOF
}
confirmpush () {
echo "Git add & commit completed. Ready to push to Repo at $GITREPO."
read -r -p "Push? [y/n] " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
confirmcommitmsg () {
echo $'\n'
echo "Review commit message."
echo $'\n'
cat $TEMP_BUILD/commitmessage
echo $'\n'
read -r -p "Commit message is correct? [y/n] " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
librariescheck () {
git diff --name-status $TEMP_BUILD/drupal/profiles/$PROJECT/libraries >> $TEMP_BUILD/librariesdiff #--diff-filter=ACMRTUXB
WORDCOUNT=`cat $TEMP_BUILD/librariesdiff | wc -l`
if [ $WORDCOUNT -gt 0 ]; then
echo $'\n'
echo 'Library files changed:'
echo $'\n'
cat $TEMP_BUILD/librariesdiff
echo $'\n'
read -r -p "Are you sure you want to update libraries? [y/n] " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
fi
}
BRANCH=
COMMITMESSAGE=
SKIP_CONFIRMATION=false
while getopts "h:b:m:y" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
b)
BRANCH=$OPTARG
;;
m)
COMMITMESSAGE=$OPTARG
;;
y)
SKIP_CONFIRMATION=true
;;
?)
usage
exit 1
;;
esac
done
if [[ -z $GITREPO ]]; then
echo 'Set $GITREPO in scripts/config.sh'
exit 1
fi
if [ "x$HOSTTYPE" == "x" ]; then
usage
fi
if [ "x$SKIP" == "x" ]; then
SKIP=0
fi
if [ ! -f drupal-org.make ]; then
echo "[error] Run this script from the distribution base path."
exit 1
fi
case $OSTYPE in
darwin*)
TEMP_BUILD=`mktemp -d -t tmpbuild`
;;
*)
TEMP_BUILD=`mktemp -d`
;;
esac
# Clone the hosting Repo first, as a bad argument can cause this to fail
echo "Cloning $HOSTTYPE Git Repo..."
# If host allows for multidev, check if a branch is specified
if [[ -z $BRANCH ]]; then
git clone --depth=1 $GITREPO $TEMP_BUILD/$HOSTTYPE
else
echo "Checkout $BRANCH branch..."
git clone --depth=1 --branch $BRANCH $GITREPO $TEMP_BUILD/$HOSTTYPE
fi
echo "$HOSTTYPE Clone complete, calling build.sh -y $TEMP_BUILD/drupal..."
./scripts/build.sh -y $TEMP_BUILD/drupal
# Remove the scripts, vendor, & settings folders for security purposes:
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/scripts
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/scripts"
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/settings
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/settings"
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.circleci
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.circleci"
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.github
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.github"
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/vendor
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/vendor"
# Remove files used by these scripts:
rm -f $TEMP_BUILD/drupal/sites/default/config.sh
echo "rm -f $TEMP_BUILD/drupal/sites/default/config.sh"
rm -f $TEMP_BUILD/drupal/sites/default/settings_additions.php
echo "rm -f $TEMP_BUILD/drupal/sites/default/settings_additions.php"
rm -f $TEMP_BUILD/drupal/profiles/$PROJECT/composer.json
echo "rm -f $TEMP_BUILD/drupal/profiles/$PROJECT/composer.json"
rm -f $TEMP_BUILD/drupal/profiles/$PROJECT/composer.lock
echo "rm -f $TEMP_BUILD/drupal/profiles/$PROJECT/composer.lock"
# Make sure no local settings are committed to pantheon
rm -f $TEMP_BUILD/drupal/sites/default/local.settings.php
echo "rm -f $TEMP_BUILD/drupal/sites/default/local.settings.php"
# Remove .git and .gitignore files
rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.git
echo "rm -rf $TEMP_BUILD/drupal/profiles/$PROJECT/.git"
rm -rf $TEMP_BUILD/drupal/.git
echo "rm -rf $TEMP_BUILD/drupal/.git"
find $TEMP_BUILD/drupal | grep '\.git' | xargs rm -rf
echo "find $TEMP_BUILD/drupal | grep '\.git' | xargs rm -rf"
# Move the remote .git & .gitignore into the drupal root
mv $TEMP_BUILD/$HOSTTYPE/.git $TEMP_BUILD/drupal/.git
echo "mv $TEMP_BUILD/$HOSTTYPE/.git $TEMP_BUILD/drupal/.git"
if [ -f $TEMP_BUILD/$HOSTTYPE/.gitignore ]; then
mv $TEMP_BUILD/$HOSTTYPE/.gitignore $TEMP_BUILD/drupal/.gitignore
echo "mv $TEMP_BUILD/$HOSTTYPE/.gitignore $TEMP_BUILD/drupal/.gitignore"
fi
if [[ -z $COMMITMESSAGE ]]; then
# Now let's build our commit message.
# git plumbing functions don't attend properly to --exec-path
# so we end up jumping around the directory structure to make git calls
# First, get the last hosting repo commit date so we know where to start
# our amalgamated commit comments from:
cd $TEMP_BUILD/drupal
COMMIT=`git rev-list HEAD --timestamp --max-count=1 --skip=$SKIP`
cd $ORIGIN
FILTER=" *"
COMMITDATEUNIX=${COMMIT%%$FILTER}
COMMITDATE=`date -r $COMMITDATEUNIX '+%m/%d/%Y %H:%M:%S'`
# Git log for commit message
cd $ORIGIN
URLINFO=`cat .git/config | grep url`
BUILDREPO=${URLINFO##*@}
# Now we start building the commit message
echo "Commit generated by ThinkShout's deploy.sh script." > $TEMP_BUILD/commitmessage
echo "Amalgamating the following commits from $BUILDREPO:" >> $TEMP_BUILD/commitmessage
echo "Amalgamating commit comments since: $COMMITDATE"
git log --pretty=format:"%h %s" --since="$COMMITDATE" >> $TEMP_BUILD/commitmessage
if [[ -z $EDITOR ]]; then
echo "Running vi to customize commit message: close editor to continue script."
vi $TEMP_BUILD/commitmessage
else
echo "Running $EDITOR to customize commit message: close editor to continue script."
$EDITOR $TEMP_BUILD/commitmessage
fi
else
echo $COMMITMESSAGE >> $TEMP_BUILD/commitmessage
fi
cd $TEMP_BUILD/drupal
if [[ -z $SKIP_CONFIRMATION ]]; then
if confirmcommitmsg; then
echo "Commit message approved."
else
echo "Commit message not approved."
exit 1
fi
if librariescheck; then
echo $'\n'
else
echo "Libraries updates not approved."
exit 1
fi
fi
echo "Writing git ls-files -mo to $TEMP_BUILD/changes"
# Checkout files that we don't want removed from the host, like settings.php.
# This function should be defined in the host include script.
if [ "`type -t protectfiles`" = 'function' ]; then
protectfiles
fi
# Don't bother sending .htaccess changes to Pantheon, since it uses nginx instead of apache
git checkout .htaccess
if [ -z "$(git status --porcelain)" ]; then
echo "Nothing to commit. Skipping deploy."
else
# Prepare commit
git ls-files -d --exclude-standard > $TEMP_BUILD/deletes
echo "Adding file deletions to GIT"
while read LINE
do
echo "Deleted: $LINE";
git rm "$LINE";
done < $TEMP_BUILD/deletes
git ls-files -mo --exclude-standard > $TEMP_BUILD/changes
echo "Adding new and changed files to GIT"
while read LINE
do
echo "Adding $LINE";
git add "$LINE";
done < $TEMP_BUILD/changes
git status
echo "Committing changes"
git commit --file=$TEMP_BUILD/commitmessage >> /dev/null
git log --max-count=1
git diff HEAD^ > $TEMP_BUILD/diff.patch
echo $'\n'
if [[ -z $SKIP_CONFIRMATION ]]; then
if confirmpush; then
git push
else
echo "Changes have not been pushed to Git Repository at $GITREPO."
echo "To push changes:"
echo "> cd $TEMP_BUILD/drupal"
echo "> git push"
fi
else
git push
fi
fi
echo "Build script complete. Clean up temp files with:"
echo "rm -rf $TEMP_BUILD"
cd $ORIGIN