-
Notifications
You must be signed in to change notification settings - Fork 6
/
Envoy.blade.php
481 lines (386 loc) · 11.5 KB
/
Envoy.blade.php
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
@version('exolnet/laravel-envoy 1.18.0')
@setup
$deploy = new Exolnet\Envoy\ConfigDeploy(get_defined_vars());
$environment = $deploy->getEnvironment();
extract($environment->extractVariables());
@endsetup
@servers(['web' => $serverString])
@task('releases:current')
basename "$(realpath "{{ $currentPath }}")"
@endtask
@setup
if ($current === true) {
$environment->detectCurrentRelease();
extract($environment->extractVariables());
}
@endsetup
@task('assert:commit')
@if (! $commit)
echo "Commit not defined." 1>&2
exit 1
@else
echo "Deploying release {{ $release }} with tree-ish {{ $commit }} to environment {{ $environment->getName() }}..."
@endif
@endtask
@macro('backups')
backups:list
@endmacro
@task('backups:list')
ls -1 "{{ $backupsPath }}"
@endtask
@macro('deploy')
{{-- BEGIN deploy:prepare --}}
assert:commit
deploy:starting
deploy:check
deploy:backup
deploy:started
deploy:provisioning
deploy:fetch
deploy:release
deploy:git
deploy:link
deploy:copy
deploy:composer
deploy:npm
deploy:provisioned
deploy:building
deploy:build
deploy:built
{{-- END deploy:prepare --}}
{{-- BEGIN deploy:complete --}}
deploy:publishing
deploy:symlink
deploy:publish
deploy:cronjobs
deploy:published
deploy:finishing
deploy:cleanup
deploy:finished
{{-- END deploy:complete --}}
@endmacro
@macro('deploy:prepare')
assert:commit
deploy:starting
deploy:check
deploy:backup
deploy:started
deploy:provisioning
deploy:fetch
deploy:release
deploy:git
deploy:link
deploy:copy
deploy:composer
deploy:npm
deploy:provisioned
deploy:building
deploy:build
deploy:built
@endmacro
@macro('deploy:complete')
deploy:publishing
deploy:symlink
deploy:publish
deploy:cronjobs
deploy:published
deploy:finishing
deploy:cleanup
deploy:finished
@endmacro
@task('deploy:starting')
true
@endtask
@task('deploy:check')
if [ ! -d "{{ $repositoryPath }}" ]; then
echo "Repository path not found." 1>&2
exit 1
fi
if [ "$(git --git-dir {{ $repositoryPath }} rev-parse --is-bare-repository)" != "true" ]; then
echo "Repository is not bare." 1>&2
exit 1
fi
if [ ! -d "{{ $releasesPath }}" ]; then
echo "Releases path not found." 1>&2
exit 1
fi
if [ ! -d "{{ $sharedPath }}" ]; then
echo "Shared path not found." 1>&2
exit 1
fi
if [ ! -d "{{ $backupsPath }}" ]; then
echo "Backups path not found." 1>&2
exit 1
fi
echo "All checks passed!"
@endtask
@task('deploy:backup')
true
@endtask
@task('deploy:started')
true
@endtask
@task('deploy:provisioning')
true
@endtask
@task('deploy:fetch')
@if ($gitSshCommand)
export GIT_SSH_COMMAND="{{ $gitSshCommand }}"
@endif
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" remote set-url origin "{{ $repositoryUrl }}"
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" fetch origin +refs/heads/*:refs/heads/* +refs/tags/*:refs/tags/* --prune
@endtask
@task('deploy:release')
mkdir "{{ $releasePath }}"
cd "{{ $releasePath }}"
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" --work-tree "{{ $releasePath }}" checkout -f {{ $commit }}
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" --work-tree "{{ $releasePath }}" rev-parse HEAD > "{{ $releasePath }}/REVISION"
@endtask
@task('deploy:git')
cd "{{ $releasePath }}"
cat > "{{ $releasePath }}/git" <<'EOL'
#!/usr/bin/env bash
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" --work-tree "{{ $releasePath }}" "$@"
EOL
chmod +x "{{ $releasePath }}/git"
@endtask
@task('deploy:link')
@run('deploy:link:dirs')
@run('deploy:link:files')
@endtask
@task('deploy:link:dirs')
@foreach ($linkedDirs as $dir)
echo "Linking directory {{ $releasePath }}/{{ $dir }} to {{ $sharedPath }}/{{ $dir }}"
mkdir -p `dirname "{{ $sharedPath }}/{{ $dir }}"`
if [ -d "{{ $releasePath }}/{{ $dir }}" ]; then
if [ ! -d "{{ $sharedPath }}/{{ $dir }}" ]; then
cp -r "{{ $releasePath }}/{{ $dir }}" "{{ $sharedPath }}/{{ $dir }}"
fi
rm -rf "{{ $releasePath }}/{{ $dir }}"
fi
if [ ! -d "{{ $sharedPath }}/{{ $dir }}" ]; then
mkdir "{{ $sharedPath }}/{{ $dir }}"
fi
mkdir -p `dirname "{{ $releasePath }}/{{ $dir }}"`
ln -srfn "{{ $sharedPath }}/{{ $dir }}" "{{ $releasePath }}/{{ $dir }}"
@endforeach
@endtask
@task('deploy:link:files')
@foreach ($linkedFiles as $file)
echo "Linking file {{ $releasePath }}/{{ $file }} to {{ $sharedPath }}/{{ $file }}"
mkdir -p `dirname "{{ $sharedPath }}/{{ $file }}"`
if [ -f "{{ $releasePath }}/{{ $file }}" ]; then
if [ ! -f "{{ $sharedPath }}/{{ $file }}" ]; then
cp "{{ $releasePath }}/{{ $file }}" "{{ $sharedPath }}/{{ $file }}"
fi
rm -f "{{ $releasePath }}/{{ $file }}"
fi
if [ ! -f "{{ $sharedPath }}/{{ $file }}" ]; then
touch "{{ $sharedPath }}/{{ $file }}"
fi
mkdir -p `dirname "{{ $releasePath }}/{{ $file }}"`
ln -srfn "{{ $sharedPath }}/{{ $file }}" "{{ $releasePath }}/{{ $file }}"
@endforeach
@endtask
@task('deploy:copy')
@run('deploy:copy:dirs')
@run('deploy:copy:files')
@endtask
@task('deploy:copy:dirs')
@foreach ($copiedDirs as $dir)
echo "Copying directory {{ $currentPath }}/{{ $dir }} to {{ $releasePath }}/{{ $dir }}"
mkdir -p `dirname "{{ $releasePath }}/{{ $dir }}"`
if [ -d "{{ $currentPath }}/{{ $dir }}" ]; then
rsync -a "{{ $currentPath }}/{{ $dir ? rtrim($dir, '/') .'/' : '' }}" "{{ $releasePath }}/{{ $dir }}"
fi
@endforeach
@endtask
@task('deploy:copy:files')
@foreach ($copiedFiles as $file)
echo "Copying file {{ $currentPath }}/{{ $file }} to {{ $releasePath }}/{{ $file }}"
mkdir -p `dirname "{{ $releasePath }}/{{ $file }}"`
if [ -f "{{ $currentPath }}/{{ $file }}" ]; then
rsync -a "{{ $currentPath }}/{{ $file }}" "{{ $releasePath }}/{{ $file }}"
fi
@endforeach
@endtask
@task('deploy:composer')
@foreach (array_unique([$releasePath, $assetsPath]) as $path)
cd "{{ $path }}"
if [ -f "composer.json" ]; then
{{ $cmdComposer }} install {{ $cmdComposerOptions }} --prefer-dist --optimize-autoloader --no-progress --no-interaction
fi
@endforeach
@endtask
@task('deploy:npm')
cd "{{ $assetsPath }}"
if [ -f "package.json" ]; then
if [ -f "yarn.lock" ]; then
{{ $cmdYarn }} install --immutable
else
{{ $cmdNpm }} install
fi
fi
@endtask
@task('deploy:provisioned')
true
@endtask
@task('deploy:building')
true
@endtask
@task('deploy:build')
cd "{{ $assetsPath }}"
if [ -f "package.json" ]; then
if [ -f "yarn.lock" ]; then
{{ $cmdYarn }} run production
else
{{ $cmdNpm }} run production
fi
fi
@endtask
@task('deploy:built')
true
@endtask
@task('deploy:publishing')
true
@endtask
@task('deploy:symlink')
echo "Linking directory {{ $releasePath }} to {{ $currentPath }}"
if [ ! -d "{{ $releasePath }}" ]; then
echo "Release directory not found." 1>&2
exit 1
fi
ln -srfn "{{ $releasePath }}" "{{ $currentPath }}"
@endtask
@task('deploy:publish')
true
@endtask
@task('deploy:cronjobs')
FILE=$(mktemp)
crontab -l > $FILE || true
sed -i '/# EXOLNET-LARAVEL-ENVOY BEGIN {{ $fingerprint }}/,/# EXOLNET-LARAVEL-ENVOY END {{ $fingerprint }}/d' $FILE
@if (is_array($cronJobs) && count($cronJobs) > 0)
echo '# EXOLNET-LARAVEL-ENVOY BEGIN {{ $fingerprint }}' >> $FILE
echo 'SHELL="/bin/bash"' >> $FILE
echo 'MAILTO="{{ $cronMailTo }}"' >> $FILE
@foreach ($cronJobs as $cronJob)
echo {{ escapeshellarg($cronJob) }} >> $FILE
@endforeach
echo '# EXOLNET-LARAVEL-ENVOY END {{ $fingerprint }}' >> $FILE
@endif
if [ -s $FILE ]; then
crontab $FILE
else
crontab -r || true
fi
rm $FILE
@endtask
@task('purge:cronjobs')
FILE=$(mktemp)
crontab -l > $FILE || true
sed -i '/# EXOLNET-LARAVEL-ENVOY BEGIN {{ $fingerprint }}/,/# EXOLNET-LARAVEL-ENVOY END {{ $fingerprint }}/d' $FILE
if [ -s $FILE ]; then
crontab $FILE
else
crontab -r || true
fi
rm $FILE
@endtask
@task('deploy:published')
true
@endtask
@task('deploy:finishing')
true
@endtask
@task('deploy:cleanup')
cd "{{ $releasesPath }}"
for RELEASE in $(ls -1d * | head -n -{{ $keepReleases }}); do
echo "Deleting old release $RELEASE"
rm -rf "$RELEASE"
done
@endtask
@task('deploy:finished')
true
@endtask
@macro('releases')
releases:list
@endmacro
@task('releases:list')
ls -1 "{{ $releasesPath }}"
@endtask
@macro('rollback')
deploy:starting
deploy:check
deploy:backup
deploy:started
deploy:publishing
rollback:symlink
deploy:publish
deploy:cronjobs
deploy:published
deploy:finishing
deploy:finished
@endmacro
@task('rollback:symlink')
@if (isset($release))
RELEASE="{{ $release }}"
@else
cd "{{ $releasesPath }}"
RELEASE=`ls -1d */ | head -n -1 | tail -n 1 | sed "s/\/$//"`
@endif
if [ ! -d "{{ $releasesPath }}/$RELEASE" ]; then
echo "Release $RELEASE not found. Could not rollback."
exit 1
fi
echo "Linking directory {{ $releasesPath }}/$RELEASE to {{ $currentPath }}"
ln -srfn "{{ $releasesPath }}/$RELEASE" "{{ $currentPath }}"
@endtask
@macro('setup')
setup:repository
setup:directories
@endmacro
@task('setup:repository')
@if ($gitSshCommand)
export GIT_SSH_COMMAND="{{ $gitSshCommand }}"
@endif
if [ -d "{{ $repoPath }}" ]; then
echo "Deleting directory {{ $repoPath }}" 1>&2
rm -rf "{{ $repoPath }}"
fi
if [ ! -d "{{ $repositoryPath }}" ]; then
{{ $cmdGit }} clone --bare "{{ $repositoryUrl }}" "{{ $repositoryPath }}"
{{ $cmdGit }} --git-dir "{{ $repositoryPath }}" config advice.detachedHead false
fi
@endtask
@task('setup:directories')
if [ ! -d "{{ $releasesPath }}" ]; then
mkdir -v "{{ $releasesPath }}"
fi
if [ ! -d "{{ $sharedPath }}" ]; then
mkdir -v "{{ $sharedPath }}"
fi
if [ ! -d "{{ $backupsPath }}" ]; then
mkdir -v "{{ $backupsPath }}"
fi
@endtask
@task('fetch:app_url')
grep -e "^APP_URL" "{{ $releasePath }}/.env" | cut -d '=' -f 2- | tr -d '"' | tr -d "'"
@endtask
@error
if ($task === 'assert:commit') {
throw new Exception("No tree-ish specified to deploy. Please provide one using '--commit=tree-ish'.");
} elseif ($task === 'deploy:check') {
throw new Exception("Unmet prerequisites to deploy. Have you run 'setup' ?");
} else {
throw new Exception('Whoops, looks like something went wrong.');
}
@enderror
@finished
if (Illuminate\Support\Str::startsWith($task, ['deploy', 'rollback'])) {
$deploy->detectSlack();
if ($deploy->has('slack_url')) {
$environment->detectAppUrl();
Exolnet\Envoy\Slack::make($environment, $deploy, $task)->send();
}
}
@endfinished