Refactor to remove slices and environments, replace with instances.
This commit is contained in:
59
app/Jobs/Applications/DeployApplication.php
Normal file
59
app/Jobs/Applications/DeployApplication.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Applications;
|
||||
|
||||
use App\Enums\DeploymentStatus;
|
||||
use App\Models\Application;
|
||||
use App\Models\Deployment;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
|
||||
class DeployApplication implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
protected Deployment $deployment;
|
||||
|
||||
public function __construct(
|
||||
public Application $application,
|
||||
) {
|
||||
//
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->deployment = $this->application->deployments()->create([
|
||||
'status' => DeploymentStatus::PENDING,
|
||||
]);
|
||||
|
||||
foreach ($this->application->instances as $instance) {
|
||||
$step = $this->deployment->steps()->create([
|
||||
'name' => "Deploy to {$instance->server->name}",
|
||||
'order' => $instance->id,
|
||||
'status' => DeploymentStatus::PENDING,
|
||||
'script' => $this->getDeploymentScript($instance),
|
||||
'secrets' => [],
|
||||
]);
|
||||
|
||||
$step->dispatchJob();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getDeploymentScript($instance): string
|
||||
{
|
||||
return "#!/bin/bash\n" .
|
||||
"cd /opt/apps/{$this->application->name}-{$instance->id}\n" .
|
||||
"git fetch origin\n" .
|
||||
"git checkout {$instance->branch}\n" .
|
||||
"git pull origin {$instance->branch}\n";
|
||||
}
|
||||
|
||||
public function failed(\Throwable $exception): void
|
||||
{
|
||||
if (isset($this->deployment)) {
|
||||
$this->deployment->update([
|
||||
'status' => DeploymentStatus::FAILED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user