|
8 | 8 | use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
|
9 | 9 | use JTMcC\AtomicDeployments\Services\Deployment;
|
10 | 10 | use JTMcC\AtomicDeployments\Services\Output;
|
| 11 | +use Throwable; |
11 | 12 |
|
12 | 13 | class DeployCommand extends BaseCommand
|
13 | 14 | {
|
14 |
| - protected $signature = 'atomic-deployments:deploy |
15 |
| - {--hash= : Specify a previous deployments commit hash/deploy-dir to deploy } |
16 |
| - {--directory= : Define your deploy folder name. Defaults to current HEAD hash } |
17 |
| - {--dry-run : Test and log deployment steps }'; |
| 15 | + protected $signature = 'atomic-deployments:deploy |
| 16 | + {--hash= : Specify a previous deployments commit hash/deploy-dir to deploy } |
| 17 | + {--directory= : Define your deploy folder name. Defaults to current HEAD hash } |
| 18 | + {--dry-run : Test and log deployment steps }'; |
18 | 19 |
|
19 | 20 | protected $description = 'Deploy a clone of your latest build and attach symlink';
|
20 | 21 |
|
21 |
| - public function handle() |
| 22 | + public function handle(): void |
22 | 23 | {
|
23 | 24 | Output::alert('Running Atomic Deployment');
|
24 | 25 |
|
25 | 26 | $migrate = config('atomic-deployments.migrate', []);
|
26 | 27 | $dryRun = $this->option('dry-run');
|
27 | 28 |
|
28 | 29 | if ($hash = $this->option('hash')) {
|
29 |
| - Output::info("Updating symlink to previous build: {$hash}"); |
30 |
| - |
31 |
| - $deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->first(); |
32 |
| - |
33 |
| - if (!$deploymentModel || !$deploymentModel->hasDeployment) { |
34 |
| - Output::warn("Build not found for hash: {$hash}"); |
35 |
| - } else { |
36 |
| - $atomicDeployment = AtomicDeploymentService::create( |
37 |
| - new Deployment($deploymentModel), |
38 |
| - $migrate, |
39 |
| - $dryRun |
40 |
| - ); |
41 |
| - |
42 |
| - try { |
43 |
| - $atomicDeployment->getDeployment()->link(); |
44 |
| - $atomicDeployment->confirmSymbolicLink(); |
45 |
| - DeploymentSuccessful::dispatch($atomicDeployment, $deploymentModel); |
46 |
| - } catch (\Throwable $e) { |
47 |
| - $atomicDeployment->fail(); |
48 |
| - Output::throwable($e); |
49 |
| - } |
50 |
| - } |
| 30 | + $this->deployPreviousBuild($hash, $migrate, $dryRun); |
51 | 31 | } else {
|
52 |
| - $atomicDeployment = AtomicDeploymentService::create($migrate, $dryRun); |
53 |
| - |
54 |
| - Output::info('Running Deployment...'); |
55 |
| - |
56 |
| - try { |
57 |
| - if ($deployDir = trim($this->option('directory'))) { |
58 |
| - Output::info("Deployment directory option set - Deployment will use directory: {$deployDir} "); |
59 |
| - $atomicDeployment->getDeployment()->setDirectory($deployDir); |
60 |
| - } |
61 |
| - $atomicDeployment->deploy(fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit'))); |
62 |
| - } catch (\Throwable $e) { |
63 |
| - $atomicDeployment->fail(); |
64 |
| - Output::throwable($e); |
65 |
| - } |
| 32 | + $this->deployCurrentBuild($migrate, $dryRun); |
66 | 33 | }
|
67 | 34 |
|
68 | 35 | Output::info('Finished');
|
69 | 36 | ConsoleOutput::line('');
|
70 | 37 | }
|
| 38 | + |
| 39 | + private function deployPreviousBuild(string $hash, array $migrate, bool $dryRun): void |
| 40 | + { |
| 41 | + Output::info("Updating symlink to previous build: {$hash}"); |
| 42 | + |
| 43 | + /** @var null|AtomicDeployment $deploymentModel */ |
| 44 | + $deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->first(); |
| 45 | + |
| 46 | + if (! $deploymentModel?->has_deployment) { |
| 47 | + Output::warn("Build not found for hash: {$hash}"); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + $atomicDeployment = AtomicDeploymentService::create( |
| 53 | + new Deployment($deploymentModel), |
| 54 | + $migrate, |
| 55 | + $dryRun |
| 56 | + ); |
| 57 | + |
| 58 | + try { |
| 59 | + $atomicDeployment->getDeployment()->link(); |
| 60 | + $atomicDeployment->confirmSymbolicLink(); |
| 61 | + DeploymentSuccessful::dispatch($atomicDeployment, $deploymentModel); |
| 62 | + } catch (Throwable $e) { |
| 63 | + $atomicDeployment->fail(); |
| 64 | + Output::throwable($e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private function deployCurrentBuild(array $migrate, bool $dryRun): void |
| 69 | + { |
| 70 | + $atomicDeployment = AtomicDeploymentService::create($migrate, $dryRun); |
| 71 | + |
| 72 | + Output::info('Running Deployment...'); |
| 73 | + |
| 74 | + try { |
| 75 | + if ($deployDir = trim($this->option('directory'))) { |
| 76 | + Output::info("Deployment directory option set - Deployment will use directory: {$deployDir}"); |
| 77 | + $atomicDeployment->getDeployment()->setDirectory($deployDir); |
| 78 | + } |
| 79 | + |
| 80 | + $atomicDeployment |
| 81 | + ->deploy( |
| 82 | + fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit')) |
| 83 | + ); |
| 84 | + } catch (Throwable $e) { |
| 85 | + $atomicDeployment->fail(); |
| 86 | + Output::throwable($e); |
| 87 | + } |
| 88 | + } |
71 | 89 | }
|
0 commit comments