This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.sh
executable file
·231 lines (196 loc) · 7.02 KB
/
deployment.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
#!/bin/bash
#
# Drupal deployment script to be called from Jenkins
#
source_dir=$1
target_dir=$2
config_file=$3
variables_file=$4
variables_overrides_in_settings=$5
DRUSH=/usr/share/drush5/drush
CLEANUP=/usr/local/share/deploy/cleanup.sh
# check our input arguments
if [[ -z $source_dir ]] || [[ -z $target_dir ]]; then
echo "Missing source or target dir arguments. Exit."
exit 1
fi
# include config file if it exists
if [ -f $config_file ]; then
echo "Using configuration from $config_file"
. $config_file
else
echo "Error: configuration file file not found"
exit 1
fi
# check that config file contains all required parameters
if [[ -z $DB_NAME ]]; then
echo "Config file does not contain database name parameter. Exit."
exit 1
fi
if [[ -z $DB_ADMIN_NAME ]]; then
echo "Config file does not user name which should be used to connect to the database. Exit."
exit 1
fi
# optional config parameters
if [[ -z $PROFILE ]]; then
PROFILE="default"
fi
if [[ -z $LOCALE ]]; then
LOCALE="en"
fi
if [[ -z $SITE_NAME ]]; then
SITE_NAME="My Site"
fi
if [[ -z $SITE_MAIL ]]; then
SITE_MAIL="site@example.com"
fi
if [[ -z $ACCOUNT_NAME ]]; then
ACCOUNT_NAME="admin"
fi
if [[ -z $ACCOUNT_PASS ]]; then
ACCOUNT_PASS="123"
fi
if [[ -z $ACCOUNT_MAIL ]]; then
ACCOUNT_MAIL="admin@example.com"
fi
if [[ -z $FILE_OWNER_USER ]]; then
FILE_OWNER_USER="www-data"
fi
if [[ -z $FILE_OWNER_GROUP ]]; then
FILE_OWNER_GROUP="www-data"
fi
if [[ -z $FULL_DEPLOY_TRIGGER_SUBSTRING ]]; then
FULL_DEPLOY_TRIGGER_SUBSTRING='#deploy'
fi
# find out if we need a full deploy or only file updates
NEED_FULL_DEPLOY="1"
if [ -n $FULL_DEPLOY_ONLY_ON_COMMIT_TOKEN ] && [ "$FULL_DEPLOY_ONLY_ON_COMMIT_TOKEN" = "1" ]; then
NEED_FULL_DEPLOY="0"
fi
if [ "$NEED_FULL_DEPLOY" = "1" ]; then
echo "Proceed with full deployment..."
else
echo "Configuration params are set to check if we actually need a full deployment."
echo "We will look for a '#deploy' substring in a commit message..."
# get the commit message from git log
if [[ -z $GIT_COMMIT ]]; then
echo "Unable to deterine git commit sha"
else
echo "Trying to find out a commit message for commit $GIT_COMMIT"
message="$(git log --format=%B -n 1 $GIT_COMMIT)"
if [[ "$message" == *"$FULL_DEPLOY_TRIGGER_SUBSTRING"* ]]; then
echo "Commit message contains build trigger substring $FULL_DEPLOY_TRIGGER_SUBSTRING - need full deployment"
NEED_FULL_DEPLOY="1"
else
echo "Don't need full deployment right now because git commit message does not contain trigger substring $FULL_DEPLOY_TRIGGER_SUBSTRING."
fi
fi
fi
# it is possible that we just need to update files and exit
if [ "$NEED_FULL_DEPLOY" = "0" ]; then
if [ -d "$target_dir" ]; then
# temp folder that will hold sites/default folder
timestamp=$(date +%F-%H-%M-%S)
temp_dir=/tmp/deploy/$timestamp
if [ -d $temp_dir ]; then
rm -rf $temp_dir
fi
mkdir -p $(dirname "$temp_dir/default")
echo "Created a temp dir: $temp_dir"
# copy sites/default folder to temp location
cp -Rf $target_dir/sites/default $temp_dir/default
echo "sites/default folder has been backed up. Ready to update files"
# run cleanup script
sudo $CLEANUP $source_dir $target_dir $FILE_OWNER_USER $FILE_OWNER_GROUP
echo "Restoring sites/default..."
# restore sites/default folder
cp -Rf $temp_dir/default $target_dir/sites/
echo "Restored."
# fix file permissions
echo "Changing file permissions..."
chown -Rf $FILE_OWNER_USER:$FILE_OWNER_GROUP $target_dir
chmod -Rf 777 $target_dir/sites/default/files
echo "Permissions have been set."
# maybe we need to modify RewriteBase in .htaccess?
if [ -n "${REWRITE_BASE}" ]; then
echo "Updating RewriteBase directive in .htaccess file..."
sed "s/# RewriteBase \/$/RewriteBase \/$REWRITE_BASE/" $target_dir/.htaccess > ~/tmp.out
cp -f ~/tmp.out $target_dir/.htaccess
rm ~/tmp.out
fi
echo "Going to $target_dir/sites/default..."
cd $target_dir/sites/default
echo "Now run drush updates..."
$DRUSH -y updb
rm -rf $temp_dir
echo "Temp directory deleted."
echo "Ready."
exit 0
else
echo "Target directory does not exist - $target_dir, redeploy from scratch"
NEED_FULL_DEPLOY="1"
fi
fi
# run cleanup script
sudo $CLEANUP $source_dir $target_dir $FILE_OWNER_USER $FILE_OWNER_GROUP
# append some additional content to settings.php file
if [ -f $variables_overrides_in_settings ]; then
echo "Append vaiables overrides to settings.php from $variables_overrides_in_settings"
# modify default.settings.php! because it will be used by drush site-install to create the real settings.php file
cat "$variables_overrides_in_settings" >> "$target_dir/sites/default/default.settings.php"
fi
# drop all tables in the database
echo "Dropping tables from $DB_NAME..."
$DRUSH sql-drop $DB_URL --yes
# run installation
echo "Starting Drupal installation using drush site-install..."
cd $target_dir
$DRUSH site-install $PROFILE --yes --site-name="$SITE_NAME" --site-mail="$SITE_MAIL" --db-url="$DB_URL" --account-mail="$ACCOUNT_MAIL" --account-name="$ACCOUNT_NAME" --account-pass="$ACCOUNT_PASS"
# update translations
if [[ -n $L10N_UPDATE ]]; then
# set proper permissions on translations dir
var="l10n_update_download_store"
$DRUSH vget $var --root="$target_dir" &> /dev/null
if [[ "$?" -eq 0 ]]; then
set `$DRUSH vget $var --root="$target_dir"`
l10n_update_dir=`echo "$2" | sed 's/"//g'`
# this is not needed anymore - persmissions are set by cleanup script because it executes under sudo
#echo "Changing file permissions for $l10n_update_dir..."
#chmod -Rf 775 $target_dir/$l10n_update_dir
fi
echo "Updating translations..."
$DRUSH l10n-update --root="$target_dir"
fi
# fix file permissions
echo "Changing file permissions..."
chown -Rf $FILE_OWNER_USER:$FILE_OWNER_GROUP $target_dir
chmod -Rf 777 $target_dir/sites/default/files
echo "Permissions have been set."
# try parse variables file
if [ -f $variables_file ]; then
echo "Using variables file $variables_file"
cat $variables_file |
while read line
do
chr=${line:0:1}
case $chr in
"#") # Currently we ignore commented lines
;;
* )
$DRUSH --root=$target_dir vset $line
;;
esac
done
else
echo "No variables file specified. But that's OK..."
fi
# maybe we need to modify RewriteBase in .htaccess?
if [ -n "${REWRITE_BASE}" ]; then
echo "Updating RewriteBase directive in .htaccess file..."
sed "s/# RewriteBase \/$/RewriteBase \/$REWRITE_BASE/" $target_dir/.htaccess > ~/tmp.out
cp -f ~/tmp.out $target_dir/.htaccess
rm ~/tmp.out
fi
echo "Installation ends."
echo "Exit"
exit 0