-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvoy.blade.php
224 lines (187 loc) · 6.63 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
@setup
require __DIR__.'/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
try {
$dotenv->load();
$dotenv->required(['DEPLOY_SERVER', 'DEPLOY_REPOSITORY', 'DEPLOY_PATH'])->notEmpty();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
$server = $_ENV['DEPLOY_SERVER'] ?? null;
$repo = $_ENV['DEPLOY_REPOSITORY'] ?? null;
$path = $_ENV['DEPLOY_PATH'] ?? null;
$healthUrl = $_ENV['DEPLOY_HEALTH_CHECK'] ?? null;
$branch = $_ENV['DEPLOY_BRANCH'] ?: 'master';
$supervisorCommandName = $_ENV['DEPLOY_SUPERVISOR_COMMAND_NAME'];
$phpVersion = $_ENV['DEPLOY_PHP_VERSION'];
if (!$supervisorCommandName) {
throw new Exception('No supervisor command name specified');
}
if (substr($path, 0, 1) !== '/') {
throw new Exception('Careful - your deployment path does not begin with /');
}
$date = (new DateTime('now', new DateTimeZone('Europe/Berlin')))->format('YmdHis');
$env = isset($env) ? $env : 'production';
$path = rtrim($path, '/');
$release = $path . '/releases/' . $date;
@endsetup
@servers(['web' => $server])
@task('init')
if [ ! -d {{ $path }}/storage ]; then
cd {{ $path }}
git clone {{ $repo }} --branch={{ $branch }} --depth=1 -q {{ $release }}
echo "Repository cloned"
mv {{ $release }}/storage {{ $path }}/storage
ln -s {{ $path }}/storage {{ $release }}/storage
echo "Storage directory set up"
cp {{ $release }}/.env.example {{ $path }}/.env
ln -s {{ $path }}/.env {{ $release }}/.env
echo "Environment file set up"
rm -rf {{ $release }}
echo "Deployment path initialised. Edit {{ $path }}/.env then run 'envoy run deploy'."
else
echo "Deployment path already initialised (storage directory exists)!"
fi
@endtask
@story('deploy')
deployment_start
stop_queue
change_storage_owner_to_deployment_user
deployment_links
deployment_composer
deployment_migrate
deployment_cache
deployment_npm
deployment_finish
change_storage_owner_to_www_data
restart_php_fpm
start_queue
health_check
deployment_option_cleanup
@endstory
@story('rollback')
deployment_rollback
health_check
@endstory
@story('list_releases')
list_releases
@endstory
@story('cleanup')
deployment_cleanup
@endstory
@task('deployment_start')
cd {{ $path }}
echo "Deployment ({{ $date }}) started"
git clone {{ $repo }} --branch={{ $branch }} --depth=1 -q {{ $release }}
echo "Repository cloned"
@endtask
@task('stop_queue')
echo "Stopping supervisor command..."
sudo supervisorctl stop {{ $supervisorCommandName }}
@endtask
@task('start_queue')
echo "Starting supervisor command..."
sudo supervisorctl start {{ $supervisorCommandName }}
@endtask
@task('change_storage_owner_to_deployment_user')
sudo chown -R forge:forge {{ $path }}/storage/*
echo "Changed storage owner to deployment user"
sudo chown -R forge:forge {{ $release }}/bootstrap/cache
echo "Changed bootstrap/cache owner to deployment user"
@endtask
@task('deployment_links')
cd {{ $path }}
rm -rf {{ $release }}/storage
ln -s {{ $path }}/storage {{ $release }}/storage
echo "Storage directories set up"
ln -s {{ $path }}/.env {{ $release }}/.env
echo "Environment file set up"
@endtask
@task('deployment_composer')
echo "Installing composer dependencies..."
cd {{ $release }}
composer install --no-interaction --quiet --no-dev --prefer-dist --optimize-autoloader
@endtask
@task('deployment_migrate')
php {{ $release }}/artisan migrate --env={{ $env }} --force --no-interaction
@endtask
@task('deployment_npm')
echo "Installing npm dependencies..."
cd {{ $release }}
npm install --no-audit --no-fund --no-optional
echo "Running npm..."
npm run {{ $env === 'production' ? 'build' : 'dev' }} --silent
@endtask
@task('deployment_cache')
php {{ $release }}/artisan view:clear --quiet
php {{ $release }}/artisan cache:clear --quiet
php {{ $release }}/artisan config:cache --quiet
php {{ $release }}/artisan route:cache --quiet
php {{ $release }}/artisan view:cache --quiet
echo "Cache cleared"
sudo chown -R www-data:www-data {{ $release }}/storage/*
@endtask
@task('deployment_finish')
php {{ $release }}/artisan storage:link
echo "Storage symbolic links created"
ln -nfs {{ $release }} {{ $path }}/current
echo "Deployment ({{ $date }}) finished"
@endtask
@task('change_storage_owner_to_www_data')
sudo chown -R www-data:www-data {{ $path }}/storage/*
echo "Changed storage owner to www-data"
sudo chown -R www-data:www-data {{ $release }}/bootstrap/cache
echo "Changed bootstrap/cache owner to www-data"
@endtask
@task('deployment_cleanup')
cd {{ $path }}/releases
find . -maxdepth 1 -name "20*" | sort | head -n -4 | xargs -I '{}' sudo chown -R forge:forge '{}'
echo "Changed releases owner to deployment user"
find . -maxdepth 1 -name "20*" | sort | head -n -4 | xargs rm -Rf
echo "Cleaned up old deployments"
@endtask
@task('deployment_option_cleanup')
cd {{ $path }}/releases
@if (!isset($noCleanup) && !$noCleanup)
find . -maxdepth 1 -name "20*" | sort | head -n -4 | xargs -I '{}' sudo chown -R forge:forge '{}'
echo "Changed releases owner to deployment user"
find . -maxdepth 1 -name "20*" | sort | head -n -4 | xargs rm -Rf
echo "Cleaned up old deployments"
@endif
@endtask
@task('restart_php_fpm')
echo "Restarting php-fpm..."
sudo service php{{ $phpVersion }}-fpm restart
@endtask
@task('health_check')
@if (! empty($healthUrl))
if [ "$(curl --write-out "%{http_code}\n" --silent --output /dev/null {{ $healthUrl }})" == "200" ]; then
printf "\033[0;32mHealth check to {{ $healthUrl }} OK\033[0m\n"
else
printf "\033[1;31mHealth check to {{ $healthUrl }} FAILED\033[0m\n"
fi
@else
echo "No health check set"
@endif
@endtask
@task('deployment_rollback')
cd {{ $path }}/releases
@if($name)
ln -nfs {{ $path }}/releases/{{ $name }} {{ $path }}/current
echo "Rolled back to {{ $name }}"
@else
ln -nfs {{ $path }}/releases/$(find . -maxdepth 1 -name "20*" | sort | tail -n 2 | head -n1) {{ $path }}/current
echo "Rolled back to $(find . -maxdepth 1 -name "20*" | sort | tail -n 2 | head -n1)"
@endif
@endtask
@task('list_releases')
cd {{ $path }}/releases
echo "Releases:"
ls -d -t */ | cut -f1 -d'/'
@endtask
{{--
@finished
@slack($slack, '#deployments', "Deployment on {$server}: {$date} complete")
@endfinished
--}}