-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.php
916 lines (726 loc) · 22.5 KB
/
deploy.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
<?php
namespace Deployer;
use Deployer\Exception\Exception;
use Deployer\Task\Context;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Exception\RuntimeException;
use function dirname;
require 'recipe/common.php';
set('git_tty', false); // [Optional] Allocate tty for git on first deployment
set('ssh_multiplexing', true);
$tzName = date_default_timezone_get();
$tz = new \DateTimeZone($tzName);
set('core_path', 'core');
set('core_repository', '[email protected]:spotman/betakiller.git');
// Default application path
set('app_path', 'app');
set('app_git_root', 'app');
set('git_exec', '%s'); // Nothing by default, just change dir on the server and call git command directly
set('repo_base_path', function () {
return getLatestReleasePath();
});
// Option for GIT management
option('repo', null, InputOption::VALUE_OPTIONAL, 'Tag to deploy.', 'app');
// Option --to for migrations:down
option('to', null, InputOption::VALUE_OPTIONAL, 'Target migration');
// Option for Minion tasks direct calls
//option('task', null, InputOption::VALUE_OPTIONAL, 'Minion task name');
// Optional branches for staging/testing env
option('app-branch', null, InputOption::VALUE_OPTIONAL, 'Target branch for the app repo');
option('core-branch', null, InputOption::VALUE_OPTIONAL, 'Target branch for the core repo');
\define('DEPLOYER_STAGE_DEV', 'development');
\define('DEPLOYER_STAGE_TESTING', 'testing');
\define('DEPLOYER_STAGE_STAGING', 'staging');
\define('DEPLOYER_STAGE_PRODUCTION', 'production');
\define('DEPLOYER_HOST_DEV', 'dev');
\define('DEPLOYER_HOST_TESTING', 'test');
set('default_stage', DEPLOYER_STAGE_DEV);
// Local server for creating migrations, git actions, etc
localhost(DEPLOYER_HOST_DEV)
->stage(DEPLOYER_STAGE_DEV);
// Local server for testing deployment tasks in dev environment
localhost(DEPLOYER_HOST_TESTING)
->set('deploy_path', sys_get_temp_dir().DIRECTORY_SEPARATOR.'deployer-testing')
->stage(DEPLOYER_STAGE_TESTING);
$serversFile = getcwd().'/hosts.yml';
if (file_exists($serversFile)) {
// Process app servers list
inventory($serversFile);
}
set('keep_releases', 3);
/**
* Check environment before real deployment starts
*/
task('check', static function () {
if (stage() === DEPLOYER_STAGE_DEV) {
throw new RuntimeException('Can not deploy to a local stage');
}
if (!get('app_repository')) {
throw new RuntimeException('Please, set up GIT repo via set("app_repository")');
}
if (!get('app_path')) {
throw new RuntimeException('Please, set up site path via set("app_path")');
}
$host = Context::get()->getHost();
$appBranch = $host->has('app_branch') ? $host->get('app_branch') : input()->getOption('app-branch');
$coreBranch = $host->has('core_branch') ? $host->get('core_branch') : input()->getOption('core-branch');
if (!$appBranch) {
$appBranch = askForRemoteBranch('app');
}
if (!$coreBranch) {
$coreBranch = askForRemoteBranch('core');
}
if (!$appBranch) {
throw new RuntimeException('App target branch must be set in hosts.yml or provided via args');
}
if (!$coreBranch) {
throw new RuntimeException('Core target branch must be set in hosts.yml or provided via args');
}
set('app_branch', $appBranch);
set('core_branch', $coreBranch);
// Check is assets:build task defined
// task('assets:build');
})->shallow();
// Prepare app env
task('deploy:repository:prepare:app', static function () {
set('repository', get('app_repository'));
set('repository_path', get('app_path'));
set('repository_branch', get('app_branch'));
})->desc('Prepare app repository');
// Prepare BetaKiller env
task('deploy:repository:prepare:betakiller', static function () {
set('repository', get('core_repository'));
set('repository_path', get('core_path'));
set('repository_branch', get('core_branch'));
})->desc('Prepare BetaKiller repository');
function askForRemoteBranch(string $scope): string
{
$gitRepo = match($scope) {
'app' => get('app_repository'),
'core' => get('core_repository'),
default => throw new Exception('Wrong project scope')
};
// Filter real branches only
$refToken = 'refs/heads/';
// List all branches
$gitOutput = runLocally("git ls-remote $gitRepo | grep $refToken"); //, null, true, false);
// Split into lines
$gitLines = explode(PHP_EOL, $gitOutput);
// Cleanup
$branches = array_map(fn(string $line) => explode($refToken, $line, 2)[1], $gitLines);
$selected = ask("Select $scope target branch", $branches[0], $branches);
if (!$selected) {
throw new Exception('Target branch is required');
}
return $selected;
}
function cd_repository_path_cmd(): string
{
return 'cd {{release_path}}/{{repository_path}}';
}
// Clone repo task
task('deploy:repository:clone', static function () {
cd('{{release_path}}');
run('git clone {{repository}} {{repository_path}}');
})->desc('Fetch repository'); //.env()->parse('{{repository_path}}')
// Fetch repo branch task
task('deploy:repository:checkout', static function () {
run(cd_repository_path_cmd().' && git checkout {{repository_branch}}');
})->desc('Checkout repository branch'); // .env()->parse('{{repository_path}}')
// Update repo task
task('deploy:repository:update', static function () {
run(cd_repository_path_cmd().' && git pull && git submodule update --init --recursive');
})->desc('Update repository'); // .env()->parse('{{repository_path}}')
// Installing vendors in BetaKiller
task('deploy:vendors:betakiller', static function () {
process_vendors('core');
})->desc('Process Composer inside BetaKiller repository');
// Installing vendors in app
task('deploy:vendors:app', static function () {
process_vendors('app');
})->desc('Process Composer inside app repository');
/**
* @param string $repo
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function process_vendors(string $repo)
{
$path = getRepoPath($repo);
$result = run("cd $path && {{bin/composer}} {{composer_options}}");
if (isVerbose()) {
write($result);
}
return $result;
}
// Deploy app
task('deploy:app', [
'deploy:repository:prepare:app',
'deploy:repository:clone',
'deploy:repository:checkout',
'deploy:repository:update',
'deploy:vendors:app',
])->desc('Deploy app repository');
// Deploy BetaKiller
task('deploy:betakiller', [
'deploy:repository:prepare:betakiller',
'deploy:repository:clone',
'deploy:repository:checkout',
'deploy:repository:update',
'deploy:vendors:betakiller',
])->desc('Deploy BetaKiller repository');
/**
* BetaKiller shared dirs
*/
set('betakiller_shared_dirs', [
// On some servers exception is thrown if these two lines are uncommented
'{{core_path}}/application/logs',
'{{app_path}}/logs',
]);
set('betakiller_shared_files', []);
task('deploy:betakiller:shared', static function () {
set('shared_dirs', get('betakiller_shared_dirs'));
add('shared_files', get('betakiller_shared_files'));
})->desc('Process BetaKiller shared files and dirs');
after('deploy:betakiller:shared', 'deploy:shared');
/**
* BetaKiller writable dirs
*/
set('betakiller_writable_dirs', [
'{{core_path}}/application/cache',
'{{core_path}}/application/logs',
'{{app_path}}/logs',
'{{app_path}}/cache',
'{{app_path}}/public/assets/static',
'{{app_path}}/assets',
]);
task('deploy:betakiller:writable', static function () {
set('writable_dirs', get('betakiller_writable_dirs'));
})->desc('Process BetaKiller writable dirs');
after('deploy:betakiller:writable', 'deploy:writable');
/**
* PHP tasks
*/
task('php:version', static function () {
writeln(run('{{bin/php}} -v'));
})->desc('PHP version');
/**
* Apache tasks
*/
task('httpd:reload', static function () {
// TODO https://ubuntuforums.org/showthread.php?t=1505075
run('sudo service httpd reload');
})->desc('Reload Apache config');
task('httpd:restart', static function () {
run('sudo service httpd restart');
})->desc('Restart Apache');
/**
* GIT tasks
*/
task('git:config:user', static function () {
$name = ask('Enter git name:', stage());
gitConfig('user.name', $name);
$email = ask('Enter git email:');
gitConfig('user.email', $email);
})->desc('set global git properties like user.email');
task('git:status', static function () {
gitStatus();
})->desc('git status');
task('git:check-no-changes', static function () {
if (gitHasLocalChanges()) {
throw new \RuntimeException('Git repo is not clean; commit changes or stash them');
}
})->desc('git status --porcelain');
task('git:add', static function () {
gitAdd();
})->desc('git add');
task('git:commit', static function () {
gitCommit();
})->desc('git commit -m "Commit message"');
task('git:commit:all', static function () {
gitCommitAll();
})->desc('git add . && git commit -m "Commit message"');
task('git:push', static function () {
gitPush();
})->desc('git push');
task('git:pull', static function () {
gitPull();
})->desc('git pull');
/**
* Makes git:check inside {current} directory
*/
task('git:check', static function () {
$out = gitStatus();
if (stripos($out, 'nothing to commit (working directory clean)') !== false) {
return;
}
if (askConfirmation('Commit changes?')) {
gitCommitAll();
gitPush();
} else {
writeln('Exiting...');
}
})->desc('Checks for new files that not in GIT and commits them');
task('git:checkout', static function () {
gitCheckout();
})->desc('git checkout _branch_ (use --branch option)');
/**
* Custom Minion tasks
*/
//task('minion', static function () {
// if (!input()->hasOption('task')) {
// throw new Exception('Specify task name via --task option');
// }
//
// $name = input()->getOption('task');
//
// runMinionTask($name, false, true);
//})->desc('Run Minion task by its name');
/**
* Create table with migrations
*/
task('migrations:install', static function () {
runMinionTask('migrations:install');
})->desc('Install migrations table');
/**
* Create new migration
*/
task('migrations:create', static function () {
$scope = ask('Enter scope (app, core, app:module:moduleName or core:module:moduleName)', 'app');
if (!$scope) {
throw new Exception('Migration scope is required');
}
$name = ask('Enter migration short name (3-128 characters, [A-Za-z0-9-_]+)');
if (!$name) {
throw new Exception('Migration name is required');
}
$desc = ''; // Prevent CLI interaction from Minion task
$output = runMinionTask("migrations:create --name=$name --description=$desc --scope=$scope", false, true);
if (!$output) {
throw new Exception('Empty output from Minion task');
}
$outArr = explode('Done! Check ', $output, 2);
$filePath = $outArr ? trim($outArr[1]) : null;
if (!$filePath) {
throw new Exception('Can not detect migration file path');
}
if (!file_exists($filePath)) {
throw new Exception('Migration file does not exists on '.$filePath);
}
// $isGit = runGitCommand('rev-parse --is-inside-work-tree 2>/dev/null && echo "true"', null, true, true) === 'true';
//
// if ($isGit) {
// runGitCommand('add .', dirname($filePath), true);
// }
})->onStage(DEPLOYER_STAGE_DEV)->desc('Create migration')->shallow();
/**
* Apply migrations
*/
task('migrations:up', static function () {
runMinionTask('migrations:up');
})->desc('Apply migrations')->shallow();
/**
* Rollback migrations
*/
task('migrations:down', static function () {
$to = input()->getOption('to');
runMinionTask("migrations:down --to=$to");
})->desc('Rollback migrations; use "--to" option to set new migration base');
/**
* Show migrations history
*/
task('migrations:history', static function () {
runMinionTask('migrations:history');
})->desc('Show migrations list');
task('sitemap:generate', static function () {
runMinionTask('sitemap', true);
})->desc('Generate sitemap.xml');
task('cache:warmup', static function () {
runMinionTask('cache:warmup', true);
})->desc('Warm up cache by making internal HTTP request to every IFace');
task('assets:deploy', static function () {
runMinionTask('assets:deploy', true);
})->desc('Collect assets from all assets/static directories');
task('bk:deploy:dotenv:migrate', static function () {
$globalDotEnv = '{{deploy_path}}/.env';
$targetDotEnv = '{{release_path}}/{{app_path}}/.env';
$exampleDotEnv = '{{release_path}}/{{app_path}}/.env.example';
if (!test("[ -f $globalDotEnv ]")) {
if (test("[ -f $exampleDotEnv ]")) {
run("cp $exampleDotEnv $globalDotEnv");
} else {
throw new Exception('Can not find .env file');
}
}
run("cp $globalDotEnv $targetDotEnv");
})->desc('Copy .env file from deploy path');
task('bk:deploy:dotenv:revision', static function () {
runMinionTask(sprintf('storeAppRevision --path=%s --revision=%s', get('dotenv_path'), gitRevision()));
})->desc('Set APP_REVISION env variable from current git revision');
/**
* Maintenance mode
*/
task('maintenance:on', static function () {
runMinionTask('maintenance:on --for=deploy');
})->desc('Enable maintenance mode');
task('maintenance:prolong', static function () {
runMinionTask('maintenance:prolong --for=600');
})->desc('Prolong maintenance mode for 10 minutes');
task('maintenance:off', static function () {
runMinionTask('maintenance:off');
})->desc('Disable maintenance mode');
/**
* Daemons maintenance
*/
task('daemon:supervisor:kill', [
'daemon:supervisor:stop',
'daemon:supervisor:start',
])->desc('Kill and restart supervisor');
task('daemon:supervisor:start', static function () {
runMinionTask('daemon:start --name="supervisor"', true);
})->desc('Start supervisor');
task('daemon:supervisor:stop', static function () {
runMinionTask('daemon:stop --name="supervisor"', true);
})->desc('Stop supervisor');
task('daemon:supervisor:reload', static function () {
runMinionTask('daemon:supervisor:reload', true);
})->desc('Reload daemons in supervisor');
task('daemon:supervisor:restart', static function () {
runMinionTask('daemon:supervisor:restart', true);
})->desc('Restart daemons in supervisor');
/**
* Import data
*/
task('import:roles', static function () {
runMinionTask('import:roles');
})->desc('Import ACL roles data');
task('import:zones', static function () {
runMinionTask('import:zones');
})->desc('Import UrlElement zones');
task('import:i18n', static function () {
runMinionTask('import:i18n');
})->desc('Import localization data')->onStage(
\DEPLOYER_STAGE_STAGING,
\DEPLOYER_STAGE_PRODUCTION
);
task('import:notification', static function () {
runMinionTask('notification:importGroups');
})->desc('Import notifications data');
/**
* Started message
*/
task('deploy:started', static function () {
writeln('<info>Deploying to <comment>https://{{hostname}}</comment></info>');
})->shallow();
/**
* Success message
*/
task('deploy:done', static function () use ($tz) {
$dateTime = (new \DateTimeImmutable())->setTimezone($tz)->format('H:i:s T');
writeln('<info>Successfully deployed to <comment>https://{{hostname}}</comment> at '.$dateTime.'!</info>');
})->shallow();
/**
* Keep maintenance mode in case ofo failure
*/
//after('deploy:failed', 'maintenance:prolong');
task('migrate', [
// Migrate DB
'migrations:up',
// Import data
'import:zones',
'import:roles',
'import:notification', // Depends on roles
'import:i18n', // Depends on roles and notification
])->setPrivate();
task('bk:deploy:prepare', [
// Check app configuration
'check',
'deploy:started',
'deploy:lock',
// Prepare directories
'deploy:prepare',
// Check for new untracked files and commit them
//'git:check',
'deploy:release',
]);
task('bk:deploy:code', [
'deploy:app',
'deploy:betakiller',
]);
task('bk:deploy:shared', [
// app shared and writable dirs
'deploy:shared',
'deploy:writable',
// BetaKiller shared and writable dirs
'deploy:betakiller:shared',
'deploy:betakiller:writable',
]);
task('bk:deploy:dotenv', [
// Copy .env file from previous revision to the new one
'bk:deploy:dotenv:migrate',
// Store APP_REVISION hash (leads to cache reset)
'bk:deploy:dotenv:revision',
]);
task('bk:deploy:assets', [
// Deploy assets (locally or on remote host)
'assets:build',
'assets:deploy',
]);
task('bk:deploy:migrate', [
// Enable maintenance mode before any DB processing
'maintenance:on',
// Migrate and import data
'migrate',
// Prepare
'sitemap:generate',
'cache:warmup',
// Restart daemons and workers from new directory
'daemon:supervisor:kill',
// Switch to new version
'deploy:symlink',
// Disable maintenance mode
'maintenance:off',
]);
task('bk:deploy:finalize', [
'deploy:clear_paths',
'cleanup',
'deploy:unlock',
]);
/**
* Composite deployment scenario
*/
task('bk:deploy', [
'git:check-no-changes',
'bk:deploy:prepare',
'bk:deploy:code',
'bk:deploy:shared',
'bk:deploy:dotenv',
'bk:deploy:assets',
'bk:deploy:migrate',
'bk:deploy:finalize',
'deploy:done',
])->desc('Deploy app bundle')->onStage(
DEPLOYER_STAGE_STAGING,
DEPLOYER_STAGE_PRODUCTION,
DEPLOYER_STAGE_TESTING
);
task('update', [
'migrate',
])->desc('Update local workspace')->onStage(\DEPLOYER_STAGE_DEV);
task('load:errors', static function () {
download('{{release_path}}/{{core_path}}/application/logs/errors.sqlite', __DIR__.'/application/logs/');
})->desc('Load errors database from remote host');
/**
* Run minion-task and echo result to console
*
* @param string $name
* @param bool|null $asHttpUser
* @param bool|null $silent
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function runMinionTask(string $name, bool $asHttpUser = null, bool $silent = null): string
{
$currentPath = getcwd();
$path = getRepoPath();
$corePath = get('core_path');
if (!str_contains($currentPath, $corePath)) {
$path .= '/public';
}
$stage = stage();
$cmd = "{{bin/php}} $path/index.php --task=$name --stage=$stage";
if (isVeryVerbose() || isDebug()) {
$cmd .= ' --debug';
}
if ($stage !== DEPLOYER_STAGE_DEV && $asHttpUser && has('minion_user')) {
$cmd = 'sudo -u {{minion_user}} '.$cmd;
}
$text = run($cmd, [
// 'tty' => (bool)$tty,
]);
if ($text && !$silent) {
write($text);
}
return $text;
}
/**
* Run git command and echo result to console
*
* @param string $gitCmd
* @param string|null $path
* @param bool|null $silent
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function runGitCommand(string $gitCmd, string $path = null, ?bool $silent = null, ?bool $cd = null): string
{
$path = $path ?: getRepoPath();
$silent = $silent ?? false;
$cd = $cd ?? true;
$gitCmd = "{{bin/git}} $gitCmd";
if ($cd) {
$gitCmd = "cd $path && $gitCmd";
}
$exec = sprintf(get('git_exec'), $gitCmd);
$result = run($exec);
if (!$silent) {
write($result);
}
return $result;
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitStatus(?string $path = null)
{
return runGitCommand('status', $path);
}
function gitHasLocalChanges(): bool
{
$result = runLocally('git status --porcelain --untracked-files=no');
return (bool)$result;
}
/**
* @param null|string $basePath
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitAdd(?string $basePath = null)
{
$addPath = ask('Path to add files:', '.');
return runGitCommand('add '.$addPath, $basePath);
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitCommit(?string $path = null)
{
$message = ask('Enter commit message:', 'Commit from production');
return runGitCommand('commit -m "'.$message.'"', $path);
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitCommitAll(?string $path = null)
{
return gitAdd($path).gitCommit($path);
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitCheckout(?string $path = null)
{
$branch = input()->getOption('branch') ?: 'master';
return runGitCommand('checkout '.$branch, $path);
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitPush(?string $path = null)
{
return runGitCommand('push', $path);
}
/**
* @param null|string $path
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitPull(?string $path = null)
{
return runGitCommand('pull', $path);
}
/**
* @param string $key
* @param string $value
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitConfig(string $key, string $value)
{
return runGitCommand("config --global $key \"$value\"");
}
/**
* @param string $repo
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function gitRevision(): string
{
return trim(runGitCommand('rev-parse --short HEAD', getRepoPath('app'), true));
}
/**
* Get current stage
*
* @return string
*/
function stage()
{
$context = Context::get();
if ($context) {
return $context->getHost()->get('stage');
}
return input()->getArgument('stage') ?: get('default_stage');
}
function getLatestReleasePath()
{
/** @var string[] $list */
$list = get('releases_list');
$release = $list[0] ?? null;
if (!$release) {
return get('release_path') ?: get('current');
}
if (isVerbose()) {
writeln(PHP_EOL.'Releases are:'.PHP_EOL);
foreach ($list as $key => $item) {
writeln($key.' => '.$item);
}
writeln('');
}
return parse('{{deploy_path}}/releases/'.$release);
}
/**
* @param null|string $repo
* @param null|string $basePath
*
* @return string
* @throws \Deployer\Exception\Exception
*/
function getRepoPath(?string $repo = null): string
{
$repoPaths = [
'core' => 'core_path',
'app' => 'app_path',
];
if (!$repo) {
$repo = (string)input()->getOption('repo') ?: 'app'; // TODO Maybe revert to 'core'
}
if (!\in_array($repo, array_keys($repoPaths), true)) {
throw new Exception('Unknown repo '.$repo);
}
$basePath = stage() === DEPLOYER_STAGE_DEV
? getcwd()
: get('repo_base_path');
return $basePath.'/'.get($repoPaths[$repo]);
}