forked from aquent/dotcms-bootcamp2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-script.sh
316 lines (296 loc) · 9.05 KB
/
deploy-script.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
## For testing only - do not put in Jenkins
## Jenkins Git plugin sets these variables for us
GIT_PREVIOUS_COMMIT="cf80ff6924f4b8afe7f08504ecb7ff546299223b"
GIT_COMMIT="ac354e5301fd51dee381577aa5079ae42ca81115"
## Start processing the commits
echo "Last Commit: $GIT_PREVIOUS_COMMIT"
echo "This Commit: $GIT_COMMIT"
## dotCMS's API Endpoint
api="http://dotcms-dev-app1a.aquent.com/api/content/publish/1"
## deleteFileResource API Endpoint
dapi="http://dotcms-dev-app1a.aquent.com/api/aqresource/delete-id/"
## The host where we have the get-file-id script
assets_host="http://dev-assets.aquent.com"
## Username and Password for dotCMS's API
## You should probably use a .netrc file for curl instead of hardcoding this here
api_user="[email protected]"
api_pass="****"
## List of Source Hosts
sourcehosts=(
"aquent.com"
"vitamintalent.com"
"thetalentcalendar.com"
)
## List of compiled files to upload for each host
sourcefiles=(
"/css:main.css"
"/css:main.min.css"
"/css:main.min.css.map"
"/js:app.js"
"/js:app.min.js"
"/js:app.min.js.map"
)
sfregex="(.*):(.*)"
## Flags for building source on each host
## Set in the script below if detected in changeset
has_aq_src=false
has_vt_src=false
has_tc_src=false
## If we have any source to build
## Set in the script below if detected in changeset
gulp_build=false
## Trim function
trim() {
local orig="$1"
local trmd=""
while true;
do
trmd="${orig#[[:space:]]}"
trmd="${trmd%[[:space:]]}"
test "$trmd" = "$orig" && break
orig="$trmd"
done
printf -- '%s\n' "$trmd"
}
## Get a file's id
getFileId() {
local host="$1"
local path="$2"
local filename="$3"
local resp=$(curl -s -u ${api_user}:${api_pass} "${assets_host}/apps/get-file-id?host=${host}&path=${path}/${filename}")
echo $(trim "${resp}")
}
## The format that getFileId() returns
host_file_regex=":(.*):(.*):"
## Upload a file to dotCMS
uploadFile() {
local host="$1"
local path="$2"
local filename="$3"
local filepath="$4"
local resp=$(getFileId "${host}" "${path}" "${filename}")
if [[ $resp =~ $host_file_regex ]] ; then
local host_id="${BASH_REMATCH[1]}"
local file_id="${BASH_REMATCH[2]}"
if [ ! -z $host_id ] ; then
if [ ! -z $file_id ] ; then
echo "** File ID = ${file_id} - Updating File"
curl -s -u ${api_user}:${api_pass} ${api} -XPUT \
-F 'json={stName:"fileAsset",hostFolder:"'"${host}:${path}/"'",title="'"${filename}"'",identifier="'"${file_id}"'",languageId:1};type=application/json' \
-F file=@"${filepath}"
else
echo "** No File ID - New File"
curl -s -u ${api_user}:${api_pass} ${api} -XPUT \
-F 'json={stName:"fileAsset",hostFolder:"'"${host}:${path}/"'",title="'"${filename}"'",languageId:1};type=application/json' \
-F file=@"${filepath}"
fi
else
echo "** There is no hostId so we can't do anything"
fi
else
echo "** Host:File Response did not match"
fi
}
## Delete a file in dotCMS
deleteFile() {
local host="$1"
local path="$2"
local filename="$3"
echo "** Deleting ${path}/${filename} to ${host} over ${dapi} with ${api_user}"
local resp=$(getFileId "${host}" "${path}" "${filename}")
if [[ $resp =~ $host_file_regex ]] ; then
local host_id="${BASH_REMATCH[1]}"
local file_id="${BASH_REMATCH[2]}"
if [ ! -z $host_id ] ; then
if [ ! -z $file_id ] ; then
echo "** File ID = ${file_id} - Deleting File"
curl -s -u ${api_user}:${api_pass} -XGET ${dapi}${file_id}
else
## Not necessarily an error because the file could've been manually deleted by a dev
echo "** No fileId so we can't delete it"
fi
else
echo "** There is no hostId so we can't do anything"
fi
else
echo "** Host:File Response did not match"
fi
}
## Get the list of changed files from github
files="$(git diff --name-status ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT})"
## Loop over the file changes
while read -r diff; do
## Parse the diff message
diff_regex="([MCRADU])[[:space:]]+(.*)"
if [[ $diff =~ $diff_regex ]] ; then
diff_type="${BASH_REMATCH[1]}"
file="${BASH_REMATCH[2]}"
path=$(dirname "${file}")
filename=$(basename "${file}")
is_src=false
deploy_site=""
echo "=========="
echo "Change type = ${diff_type}"
echo "Path = ${path}"
echo "Filename = ${filename}"
## See if this s a src file
if [[ $path == src/* ]] ; then
deploy_site=$(echo $path | perl -n -e'/src\/([^\/]*)(\/?.*)/ && print $1')
deploy_path=$(echo $path | perl -n -e'/src\/([^\/]*)(\/?.*)/ && print $2')
is_src=true
gulp_build=true
fi
## See if this is a dist file
if [[ $path == dist/* ]] ; then
deploy_site=$(echo $path | perl -n -e'/dist\/([^\/]*)(\/?.*)/ && print $1')
deploy_path=$(echo $path | perl -n -e'/dist\/([^\/]*)(\/?.*)/ && print $2')
fi
echo "Deploy Site = ${deploy_site}"
echo "Deploy Path = ${deploy_path}"
## if deploy site is not set then it isn't something we need to deploy
if [ -n $deploy_site ] ; then
## Handle the non src files
if [ "$is_src" = false ]
then
## handle each type of change
case $diff_type in
[MCRA])
## M=Modified, C=Copied+Changed, R=Renamed, A=Added
echo "Deploying ${deploy_path}/${filename} on ${deploy_site} ..."
uploadFile "${deploy_site}" "${deploy_path}" "${filename}" "${file}"
echo "... Finished"
## At some point we should try and see if we can handle R better, like remove the old file
;;
[D])
## D=Deleted
echo "Deleting ${deploy_path}/${filename} from ${deploy_site} ..."
deleteFile "${deploy_site}" "${deploy_path}" "${filename}"
echo "... Finished"
;;
*)
## Could be U=Unmerged or something went wrong
echo "Unrecognized Diff Type: ${diff_type}"
echo "Skipping ${deploy_path}/${filename} on ${deploy_site}"
;;
esac
else
echo "${deploy_path}/${filename} on ${deploy_site} is a source file"
## Check for the host
case $deploy_site in
aquent.com)
has_aq_src=true
;;
vitamintalent.com)
has_vt_src=true
;;
thetalentcalendar.com)
has_tc_src=true
;;
*)
echo "Unknown src host = ${deploy_site}"
;;
esac
fi
else
echo "Unknown Path"
fi
else
echo "Diff Pattern does not match - ${diff}"
fi
done <<< "$files"
## Check to see if we need to handle source files or not
echo "=========="
if [ "$gulp_build" = true ] ; then
## Need to compile the source and upload the CSS/JS files
echo "Building Source Files"
if [ "${has_aq_src}" = true ] ; then
echo "Building AQ src ..."
gulp -h "aquent.com"
echo "... Finished AQ src"
else
echo "No AQ src"
fi
echo "==="
if [ "${has_vt_src}" = true ] ; then
echo "Building VT src ..."
gulp -h "vitamintalent.com"
echo "... Finished VT src"
else
echo "No VT src"
fi
echo "==="
if [ "${has_tc_src}" = true ] ; then
echo "Building TC src ..."
gulp -h "thetalentcalendar.com"
echo "... Finished TC src"
else
echo "No TC src"
fi
echo "==="
echo "Finished Building Source Files"
echo "=========="
echo "Deploying Compiled Files"
## Loop through the hosts
for h in ${sourcehosts[*]} ; do
echo "====="
echo "Checking for deploy on ${h}"
## Make sure we are deploying to this host
doit=false
case $h in
aquent.com)
echo "${h} is AQ"
if [ "$has_aq_src" = true ] ; then
doit=true
echo "Deploying AQ src"
else
echo "Not Deploying AQ src"
fi
;;
vitamintalent.com)
echo "${h} is VT"
if [ "$has_vt_src" = true ] ; then
doit=true
echo "Deploying VT src"
else
echo "Not Deploying VT src"
fi
;;
thetalentcalendar.com)
echo "${h} is TC"
if [ "$has_tc_src" = true ] ; then
doit=true
echo "Deploying TC src"
else
echo "Not Deploying TC src"
fi
;;
*)
echo "Unknown src host = ${h}"
;;
esac
if [ "$doit" = true ] ; then
## Loop through the list and upload each one
for i in ${sourcefiles[*]} ; do
echo "==="
echo "Processing File: $i on $h"
## Parse the file into host/path/name
if [[ $i =~ $sfregex ]] ; then
file_path="${BASH_REMATCH[1]}"
file_name="${BASH_REMATCH[2]}"
file="dist/${h}${file_path}/${file_name}"
echo "Uploading File: ${file} ..."
uploadFile "${h}" "${file_path}" "${file_name}" "${file}"
echo "... Uploaded"
else
echo "No Match"
fi
done ## end loop through files
else
echo "Not Deploying to ${h}"
fi
done ## end loop through hosts
else
echo "No source Files to deploy"
fi
echo "=========="