-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.php
More file actions
261 lines (208 loc) · 7.72 KB
/
deploy.php
File metadata and controls
261 lines (208 loc) · 7.72 KB
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
<?php
namespace Deployer;
require 'recipe/common.php';
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Console\Input\InputOption;
// Project root (not vendor dir)
$projectRoot = getcwd();
// Load deploy.yml
$configFile = $projectRoot . '/deploy.yml';
if (!file_exists($configFile)) {
throw new \RuntimeException("Missing deploy.yml file in project root.");
}
set('ssh_multiplexing', true);
$config = Yaml::parseFile($configFile);
// Merge excludes
$exclude = $config['exclude'] ?? [];
$deployIgnoreFile = $projectRoot . '/.deployignore';
if (file_exists($deployIgnoreFile)) {
$lines = file($deployIgnoreFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$deployIgnore = array_filter($lines, fn ($line) => strpos(trim($line), '#') !== 0);
$exclude = array_merge($exclude, $deployIgnore);
}
$exclude = array_unique($exclude);
// Disable remote git clone, since we upload built files
set('repository', '');
// Register --stage as a recognised Deployer option
option('stage', null, InputOption::VALUE_REQUIRED, 'Deployment stage (staging or prod)', 'staging');
// Basic settings from YAML config
set('application', $config['project']['name']);
set('shared_dirs', $config['shared_dirs'] ?? []);
set('shared_files', $config['shared_files'] ?? []);
set('writable_dirs', $config['writable'] ?? []);
set('keep_releases', 2);
set('git_tty', false);
// Define host — deploy_path is set at runtime by deploy:resolve_stage
host($config['project']['name'])
->setHostname($config['server']['host'])
->setRemoteUser($config['server']['user'])
->setPort($config['server']['port'] ?? 22)
->setDeployPath('{{deploy_path}}');
/**
* Resolve stage and set deploy_path before anything else runs
*/
task('deploy:resolve_stage', function () use ($config) {
$stage = input()->getOption('stage');
$validStages = ['staging', 'prod'];
if (!in_array($stage, $validStages, true)) {
throw new \RuntimeException("Invalid stage '$stage'. Valid options: " . implode(', ', $validStages));
}
$deployPathKey = $stage . '_deploy_path';
if (empty($config['server'][$deployPathKey])) {
throw new \RuntimeException("Missing '$deployPathKey' in deploy.yml server config.");
}
set('deploy_path', $config['server'][$deployPathKey]);
set('current_stage', $stage);
});
/**
* Interactive confirmation before deployment
*/
task('deploy:confirm', function () {
$branch = trim(runLocally('git rev-parse --abbrev-ref HEAD'));
writeln("<comment>You are about to deploy branch: <info>$branch</info></comment>");
writeln("<comment>Stage: <info>{{current_stage}}</info></comment>");
writeln("<comment>Server: <info>{{hostname}}</info></comment>");
writeln("<comment>Deploy path: <info>{{deploy_path}}</info></comment>");
writeln("");
if (!askConfirmation('Do you wish to continue?', false)) {
throw new \RuntimeException('Deployment cancelled by user.');
}
});
/**
* Ensure git working tree is clean before deploying
*/
task('git:check_clean', function () {
$status = trim(runLocally('git status --porcelain'));
if ($status !== '') {
throw new \RuntimeException(
"Working tree is not clean.\nCommit or stash changes before deploying."
);
}
});
// Local build task
task('build:local', function () use ($config) {
foreach ($config['build']['local'] ?? [] as $command) {
writeln("<info>Running locally:</info> $command");
runLocally($command);
}
});
// Rsync task with excludes
task('deploy:rsync', function () use ($exclude, $config, $projectRoot) {
$src = rtrim(realpath($projectRoot), '/') . '/';
$dest = '{{release_path}}';
$excludeArgs = '';
foreach ($exclude as $item) {
$excludeArgs .= " --exclude='$item'";
}
$host = $config['server']['host'];
$user = $config['server']['user'];
$port = $config['server']['port'] ?? 22;
runLocally(
"rsync -az --delete $excludeArgs -e 'ssh -p $port' $src $user@$host:$dest"
);
});
// Production commands before symlink switch
task('deploy:before_symlink', function () use ($config) {
foreach ($config['deploy']['before_symlink'] ?? [] as $cmd) {
run("cd {{release_path}} && $cmd");
}
});
// Production commands after symlink switch
task('deploy:after_symlink', function () use ($config) {
foreach ($config['deploy']['after_symlink'] ?? [] as $cmd) {
run("cd {{release_path}} && $cmd");
}
});
task('deploy:update_code', function () {
writeln('Skipping deploy:update_code - deploying local build via rsync.');
});
desc('Install composer dependencies inside PHP container');
task('deploy:composer', function () use ($config) {
$composerConfig = $config['composer'] ?? [];
$shouldInstall = $composerConfig['install'] ?? true;
if (!$shouldInstall) {
writeln('<comment>Skipping composer install (disabled in deploy.yml)</comment>');
return;
}
$installDev = $composerConfig['install_dev'] ?? false;
$showOutput = $composerConfig['show_output'] ?? false;
$releasePath = get('release_path');
$phpImage = sprintf(
'%s.dkr.ecr.%s.amazonaws.com/php-fpm-%s',
$config['aws']['aws_account_id'],
$config['aws']['aws_region'],
$config['php']['version']
);
$cacheDir = '/home/ubuntu/.composer/cache';
$devFlag = $installDev ? '' : '--no-dev';
$exec = function (string $cmd) use ($showOutput): void {
$output = run($cmd);
if ($showOutput && $output !== '') {
writeln($output);
}
};
$exec("mkdir -p {$cacheDir}");
$exec("docker pull {$phpImage}");
$exec("
docker run --rm \
--user 1000:1000 \
-v {$releasePath}:/app \
-v {$cacheDir}:/tmp/composer-cache \
-e COMPOSER_CACHE_DIR=/tmp/composer-cache \
-w /app \
{$phpImage} \
composer install \
{$devFlag} \
--prefer-dist \
--no-interaction \
--no-progress \
--optimize-autoloader \
--classmap-authoritative
");
});
desc('Run SilverStripe dev/build inside PHP container');
task('deploy:silverstripe_build', function () use ($config) {
$ssConfig = $config['silverstripe'] ?? [];
$devBuild = $ssConfig['dev_build'] ?? false;
if (!$devBuild) {
writeln('<comment>Skipping SilverStripe dev/build (disabled in deploy.yml)</comment>');
return;
}
$showOutput = $ssConfig['show_output'] ?? false;
$releasePath = get('release_path');
$phpImage = sprintf(
'%s.dkr.ecr.%s.amazonaws.com/php-fpm-%s',
$config['aws']['aws_account_id'],
$config['aws']['aws_region'],
$config['php']['version']
);
$cacheDir = '/home/ubuntu/.composer/cache';
$exec = function (string $cmd) use ($showOutput): void {
$output = run($cmd);
if ($showOutput && $output !== '') {
writeln($output);
}
};
$exec("
docker run --rm \
--user 1000:1000 \
-v {$releasePath}:/app \
-v {$cacheDir}:/tmp/composer-cache \
-e COMPOSER_CACHE_DIR=/tmp/composer-cache \
-w /app \
{$phpImage} \
vendor/bin/sake dev/build 'flush=1'
");
});
// Hooks wiring
before('deploy:confirm', 'deploy:resolve_stage');
before('deploy', 'deploy:confirm');
before('build:local', 'git:check_clean');
before('deploy:prepare', 'build:local');
before('deploy:shared', 'deploy:rsync');
before('deploy:symlink', 'deploy:before_symlink');
after('deploy:symlink', 'deploy:after_symlink');
after('deploy:shared', 'deploy:composer');
after('deploy:composer', 'deploy:silverstripe_build');
// Cleanup on failure
after('deploy:failed', 'deploy:unlock');