Skip to content

Commit 694e42d

Browse files
committed
Refactoring
Added deployment service Making use of service container for DI Added deployment class as config option to allow custom deployment implementations Updated tests Apply fixes from StyleCI README.md
1 parent 0b084da commit 694e42d

16 files changed

+514
-412
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Atomic Deployment Package for Laravel Framework
2-
![run-tests](https://github.com/J-T-McC/laravel-atomic-deployments/workflows/run-tests/badge.svg) [![StyleCI](https://github.styleci.io/repos/330310979/shield?branch=main)](https://github.styleci.io/repos/330310979?branch=main)
2+
![run-tests](https://github.com/J-T-McC/laravel-atomic-deployments/workflows/run-tests/badge.svg)
3+
[![StyleCI](https://github.styleci.io/repos/330310979/shield?branch=main)](https://github.styleci.io/repos/330310979?branch=main)
4+
[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
35

46
The purpose of this package is to introduce local zero-downtime deployments into a laravel application.
57

@@ -43,36 +45,45 @@ return [
4345

4446
/**
4547
* Symbolic link to the current deployed build
46-
* This path should be used for schedules and setting your web root
48+
* This path should be used for schedules and setting your web root.
4749
*/
4850
'deployment-link' => env('ATM_DEPLOYMENT_LINK'),
4951

5052
/**
5153
* The primary build folder
52-
* This folder is where all deployments ran and ultimately copied to a deployment directory
54+
* This folder is where all deployments ran and ultimately copied to a deployment directory.
5355
*/
5456
'build-path' => env('ATM_BUILD'),
5557

5658
/**
5759
* Production build directory
5860
* Builds are copied here and linked for deployment
59-
* Ensure this directory has the required permissions to allow php and your webserver to run your application here
61+
* Ensure this directory has the required permissions to allow php and your webserver to run your application here.
6062
*/
6163
'deployments-path' => env('ATM_DEPLOYMENTS'),
6264

6365
/**
6466
* Max number of build directories allowed
65-
* Once limit is hit, old deployments will be removed automatically after a successful build
67+
* Once limit is hit, old deployments will be removed automatically after a successful build.
6668
*/
6769
'build-limit' => 10,
6870

6971
/**
70-
* Migrate files|folders from the outgoing production build to your new release using a relative path and pattern
72+
* Migrate files|folders from the outgoing production build to your new release using a relative path and pattern.
73+
*
7174
* @see https://www.php.net/manual/en/function.glob.php
7275
*/
7376
'migrate' => [
74-
// 'storage/framework/sessions/*',
75-
]
77+
// 'storage/framework/sessions/*',
78+
],
79+
80+
/**
81+
* Deployment class used.
82+
*
83+
* Add custom deployments by implementing @see \JTMcC\AtomicDeployments\Interfaces\DeploymentInterface
84+
* and adding your class to this config property
85+
*/
86+
'deployment-class' => \JTMcC\AtomicDeployments\Services\Deployment::class,
7687

7788
];
7889
```

config/atomic-deployments.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@
3636
// 'storage/framework/sessions/*',
3737
],
3838

39+
/**
40+
* Deployment class used.
41+
*
42+
* Add custom deployments by implementing @see \JTMcC\AtomicDeployments\Interfaces\DeploymentInterface
43+
* and adding your class to this config property
44+
*/
45+
'deployment-class' => \JTMcC\AtomicDeployments\Services\Deployment::class,
46+
3947
];

src/AtomicDeploymentsServiceProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\ServiceProvider;
66
use JTMcC\AtomicDeployments\Commands\DeployCommand;
77
use JTMcC\AtomicDeployments\Commands\ListCommand;
8+
use JTMcC\AtomicDeployments\Interfaces\DeploymentInterface;
9+
use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
810

911
class AtomicDeploymentsServiceProvider extends ServiceProvider
1012
{
@@ -15,9 +17,19 @@ public function boot()
1517

1618
public function register()
1719
{
18-
$this->registerPublishables();
1920
$this->mergeConfigFrom(__DIR__.'/../config/atomic-deployments.php', 'atomic-deployments');
21+
$this->registerPublishables();
2022
$this->registerCommands();
23+
24+
$this->app->bind(DeploymentInterface::class, config('atomic-deployments.deployment-class'));
25+
26+
$this->app->bind(AtomicDeploymentService::class, function ($app, $params) {
27+
if (empty($params) || (count($params) && !is_a($params[0], DeploymentInterface::class))) {
28+
array_unshift($params, $app->make(DeploymentInterface::class));
29+
}
30+
31+
return new AtomicDeploymentService(...$params);
32+
});
2133
}
2234

2335
protected function registerPublishables(): void

src/Commands/DeployCommand.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use JTMcC\AtomicDeployments\Events\DeploymentSuccessful;
66
use JTMcC\AtomicDeployments\Helpers\ConsoleOutput;
77
use JTMcC\AtomicDeployments\Models\AtomicDeployment;
8-
use JTMcC\AtomicDeployments\Services\AtomicDeployments;
8+
use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
9+
use JTMcC\AtomicDeployments\Services\Deployment;
910
use JTMcC\AtomicDeployments\Services\Output;
1011

1112
class DeployCommand extends BaseCommand
@@ -21,48 +22,47 @@ public function handle()
2122
{
2223
Output::alert('Running Atomic Deployment');
2324

24-
$buildPath = config('atomic-deployments.build-path');
25-
$deploymentLink = config('atomic-deployments.deployment-link');
26-
$deploymentsPath = config('atomic-deployments.deployments-path');
2725
$migrate = config('atomic-deployments.migrate', []);
28-
29-
$atomicDeployment = (new AtomicDeployments(
30-
$deploymentLink,
31-
$deploymentsPath,
32-
$buildPath,
33-
$migrate,
34-
$this->option('dry-run')
35-
));
26+
$dryRun = $this->option('dry-run');
3627

3728
if ($hash = $this->option('hash')) {
3829
Output::info("Updating symlink to previous build: {$hash}");
3930

40-
$deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->orderBy('id', 'desc')->first();
31+
$deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->first();
4132

4233
if (!$deploymentModel || !$deploymentModel->hasDeployment) {
4334
Output::warn("Build not found for hash: {$hash}");
4435
} else {
36+
$atomicDeployment = AtomicDeploymentService::create(
37+
new Deployment($deploymentModel),
38+
$migrate,
39+
$dryRun
40+
);
41+
4542
try {
46-
$atomicDeployment->linkDeployment(
47-
$deploymentModel->deployment_link,
48-
$deploymentModel->deployment_path
49-
);
50-
$atomicDeployment->confirmSymbolicLink($deploymentModel->deployment_path);
43+
$atomicDeployment->getDeployment()->linkDeployment();
44+
$atomicDeployment->confirmSymbolicLink();
5145
DeploymentSuccessful::dispatch($atomicDeployment, $deploymentModel);
5246
} catch (\Throwable $e) {
47+
$atomicDeployment->failed();
5348
Output::throwable($e);
54-
$atomicDeployment->rollback();
5549
}
5650
}
5751
} else {
52+
$atomicDeployment = AtomicDeploymentService::create($migrate, $dryRun);
53+
5854
Output::info('Running Deployment...');
5955

60-
if ($deployDir = trim($this->option('directory'))) {
61-
Output::info("Deployment directory option set. Deployment will use {$deployDir}");
62-
$atomicDeployment->setDeploymentDirectory($deployDir);
56+
try {
57+
if ($deployDir = trim($this->option('directory'))) {
58+
Output::info("Deployment directory option set - Deployment will use directory: {$deployDir} ");
59+
$atomicDeployment->getDeployment()->setDeploymentDirectory($deployDir);
60+
}
61+
$atomicDeployment->deploy(fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit')));
62+
} catch (\Throwable $e) {
63+
$atomicDeployment->failed();
64+
Output::throwable($e);
6365
}
64-
65-
$atomicDeployment->deploy(fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit')));
6666
}
6767

6868
Output::info('Finished');

src/Events/DeploymentFailed.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
use Illuminate\Foundation\Events\Dispatchable;
77
use Illuminate\Queue\SerializesModels;
88
use JTMcC\AtomicDeployments\Models\AtomicDeployment;
9-
use JTMcC\AtomicDeployments\Services\AtomicDeployments;
9+
use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
1010

1111
class DeploymentFailed implements ShouldQueue
1212
{
1313
use Dispatchable;
1414
use SerializesModels;
1515

16-
public AtomicDeployments $deploymentService;
16+
public AtomicDeploymentService $deploymentService;
1717
public ?AtomicDeployment $deployment = null;
1818

1919
/**
2020
* DeploymentSuccessful constructor.
2121
*
22-
* @param AtomicDeployments $deploymentService
23-
* @param mixed $deployment
22+
* @param AtomicDeploymentService $deploymentService
23+
* @param AtomicDeployment|null $deployment
2424
*/
25-
public function __construct(AtomicDeployments $deploymentService, ?AtomicDeployment $deployment = null)
25+
public function __construct(AtomicDeploymentService $deploymentService, AtomicDeployment $deployment = null)
2626
{
2727
$this->deploymentService = $deploymentService;
2828
$this->deployment = $deployment;

src/Events/DeploymentSuccessful.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
use Illuminate\Foundation\Events\Dispatchable;
77
use Illuminate\Queue\SerializesModels;
88
use JTMcC\AtomicDeployments\Models\AtomicDeployment;
9-
use JTMcC\AtomicDeployments\Services\AtomicDeployments;
9+
use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
1010

1111
class DeploymentSuccessful implements ShouldQueue
1212
{
1313
use Dispatchable;
1414
use SerializesModels;
1515

16-
public AtomicDeployments $deploymentService;
16+
public AtomicDeploymentService $deploymentService;
1717
public ?AtomicDeployment $deployment = null;
1818

1919
/**
2020
* DeploymentSuccessful constructor.
2121
*
22-
* @param AtomicDeployments $deploymentService
23-
* @param mixed $deployment
22+
* @param AtomicDeploymentService $deploymentService
23+
* @param AtomicDeployment|null $deployment
2424
*/
25-
public function __construct(AtomicDeployments $deploymentService, ?AtomicDeployment $deployment = null)
25+
public function __construct(AtomicDeploymentService $deploymentService, AtomicDeployment $deployment = null)
2626
{
2727
$this->deploymentService = $deploymentService;
2828
$this->deployment = $deployment;

src/Helpers/FileHelper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,4 @@ public static function confirmPathsExist(string ...$paths): bool
2424

2525
return true;
2626
}
27-
28-
/**
29-
* @param $path
30-
* @param int $mode
31-
* @param false $recursive
32-
*/
33-
public static function createDirectory($path, $mode = 0755, $recursive = true): void
34-
{
35-
File::ensureDirectoryExists($path, $mode, $recursive);
36-
}
3727
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace JTMcC\AtomicDeployments\Interfaces;
4+
5+
interface DeploymentInterface
6+
{
7+
public function getBuildPath();
8+
9+
public function setDeploymentDirectory(string $name = '');
10+
11+
public function setDeploymentPath();
12+
13+
public function getDeploymentPath();
14+
15+
public function getCurrentDeploymentPath();
16+
17+
public function copyContents();
18+
19+
public function linkDeployment();
20+
21+
public function getDeploymentLink();
22+
23+
public function getModel();
24+
25+
public function updateDeploymentStatus(int $status);
26+
}

0 commit comments

Comments
 (0)