59 lines
1.6 KiB
PHP
59 lines
1.6 KiB
PHP
<?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,
|
|
]);
|
|
}
|
|
}
|
|
} |