Skip to content
This repository was archived by the owner on Jan 6, 2026. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions app/Http/Controllers/ProjectDeploymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Jobs\DeployJob;
use App\Models\Deployment;
use App\Models\DeploymentTask;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -90,14 +91,37 @@ public function index(Project $project)
->latest()
->paginate();

return inertia('Projects/Deployments/Index', compact('project', 'deployments'));
return Inertia::render('Projects/Deployments/Index', [
'project' => $project,
'deployments' => $deployments,
]);
}

public function show(Project $project, Deployment $deployment)
{
abort_if($deployment->project_id != $project->id, 404);
$deployment->makeVisible(['raw_output']);

return inertia('Projects/Deployments/Show', compact('project', 'deployment'));
$tasks = $deployment
->tasks()
->with('server')
->get()
->groupBy('name');

return Inertia::render('Projects/Deployments/Show', [
'project' => $project,
'deployment' => $deployment,
'tasks' => $tasks,
]);
}

public function showTaskOutput(Project $project, Deployment $deployment, DeploymentTask $task)
{
abort_if($deployment->project_id != $project->id, 404);
abort_if($task->deployment_id != $deployment->id, 404);

return Inertia::modal('Projects/Deployments/TaskOutput', [
'output' => $task->output,
])
->baseRoute('projects.deployments.show', [$project, $deployment]);
}
}
117 changes: 33 additions & 84 deletions app/Jobs/DeployJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,36 @@
namespace App\Jobs;

use App\Models\Deployment;
use App\Notifications\DeploymentFailed;
use App\Notifications\DeploymentStarted;
use App\Notifications\DeploymentSuccessful;
use App\Models\DeploymentTask;
use App\Utils\ShellScriptRenderer;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Spatie\Ssh\Ssh as SSH;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Illuminate\Support\Facades\Bus;
use Throwable;

class DeployJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $ssh;
protected Deployment $deployment;

protected $tasks;
protected $github_deployment_id;

public function __construct(Deployment $deployment)
{
$this->deployment = $deployment;
public function __construct(
protected Deployment $deployment
) {
}

public function handle()
public function handle():void
{
$this
->before()
->computeTasks()
->runTasks()
->after();
->runTasks();
}

protected function before()
Expand All @@ -46,27 +41,6 @@ protected function before()
$this->deployment->started_at = now();
$this->deployment->save();

$this->ssh = SSH::create(
$this->deployment->server->ssh_user,
$this->deployment->server->ssh_host,
$this->deployment->server->ssh_port,
)
->usePrivateKey(Storage::path('keys/' . $this->deployment->server->id))
->disableStrictHostKeyChecking();

$this->github_deployment_id = rescue(function () {
$gh_client = $this->deployment->project->user->github()->deployments();
[$user, $repo] = explode('/', $this->deployment->project->repository);

return $gh_client->create($user, $repo, [
'ref' => $this->deployment->commit['from_ref'] ? 'refs/' . $this->deployment->commit['from_ref'] : $this->deployment->commit['sha'],
'environment' => $this->deployment->project->environment,
'auto_merge' => false,
])['id'];
}, null, false);

$this->deployment->project->notify(new DeploymentStarted($this->deployment, $this->github_deployment_id));

return $this;
}

Expand Down Expand Up @@ -119,59 +93,34 @@ protected function defineTask($name, $commands)

protected function runTasks()
{
$jobs = [];

foreach ($this->tasks as $name => $task) {
$this->runTask($name);
$model = DeploymentTask::create([
'deployment_id' => $this->deployment->id,
'server_id' => $this->deployment->server_id,
'name' => $name,
'commands' => $task,
]);

$jobs[] = (new ProcessDeploymentTaskJob($model));
}

return $this;
}

protected function runTask($name)
{
$this->ssh->onOutput(fn ($type, $line) => $this->appendToOutput($type, $line, $name));

$process = $this->ssh->execute($this->tasks[$name]);

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$deployment = $this->deployment;

$batch = Bus::batch([
[...$jobs],
])->then(function (Batch $batch) use ($deployment) {
$deployment->status = 'success';
$deployment->save();
})->catch(function (Batch $batch, Throwable $e) use ($deployment) {
$deployment->status = 'error';
$deployment->save();
})->finally(function (Batch $batch) use ($deployment) {
$deployment->ended_at = now();
$deployment->save();
})->dispatch();

return $this;
}

protected function after()
{
$this->deployment->status = 'success';
$this->deployment->ended_at = now();
$this->deployment->save();

$this->deployment->project->notify(new DeploymentSuccessful($this->deployment, $this->github_deployment_id));

// dispatch(new PingJob($this->deployment));

return $this;
}

public function failed()
{
$this->deployment->status = 'error';
$this->deployment->ended_at = now();
$this->deployment->save();

$this->deployment->project->notify(new DeploymentFailed($this->deployment, $this->github_deployment_id));
}

protected function appendToOutput($type, $line, $name)
{
$raw_output = $this->deployment->raw_output;

if (! ($raw_output[$name] ?? null)) {
$raw_output[$name] = '';
}

$raw_output[$name] .= $line;
$this->deployment->raw_output = $raw_output;

$this->deployment->save();
}
}
102 changes: 102 additions & 0 deletions app/Jobs/ProcessDeploymentTaskJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Jobs;

use App\Models\Deployment;
use App\Models\DeploymentTask;
use App\Models\Server;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Spatie\Ssh\Ssh as SSH;
use Symfony\Component\Process\Exception\ProcessFailedException;

class ProcessDeploymentTaskJob implements ShouldQueue
{
use Dispatchable, Batchable, InteractsWithQueue, Queueable, SerializesModels;

protected ?SSH $ssh;
protected ?Deployment $deployment;
protected ?Server $server;

public function __construct(
protected DeploymentTask $task,
) {
}

public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}

$this
->before()
->runTask()
->after();
}

protected function before()
{
$this->deployment = $this->task->deployment;
$this->server = $this->deployment->server;

$this->ssh = SSH::create(
$this->server->ssh_user,
$this->server->ssh_host,
$this->server->ssh_port,
)
->usePrivateKey(Storage::path('keys/' . $this->server->id))
->disableStrictHostKeyChecking();

$this->ssh->onOutput(fn ($type, $line) => $this->appendToOutput($type, $line));

$this->task->status = 'in_progress';
$this->task->started_at = now();
$this->task->save();

return $this;
}

protected function runTask()
{
if (! $this->task->commands) {
return $this;
}

$process = $this->ssh->execute($this->task->commands);

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

return $this;
}

protected function after()
{
$this->task->status = 'success';
$this->task->ended_at = now();
$this->task->save();

return $this;
}

public function failed()
{
$this->task->status = 'error';
$this->task->ended_at = now();
$this->task->save();
}

protected function appendToOutput($type, $line)
{
$this->task->output .= $line;

$this->task->save();
}
}
7 changes: 7 additions & 0 deletions app/Models/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public function server()
return $this->belongsTo(Server::class);
}

public function tasks()
{
return $this
->hasMany(DeploymentTask::class)
->orderBy('created_at');
}

public function getDurationAttribute()
{
return rescue(fn () => $this->ended_at->diff($this->started_at)->format('%i minutes %s seconds'), 'N/A', false);
Expand Down
33 changes: 33 additions & 0 deletions app/Models/DeploymentTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DeploymentTask extends Model
{
use HasFactory, HasUlids;

protected $guarded = [];

protected $casts = [
'commands' => 'json',
];

protected $hidden = [
'commands',
'output',
];

public function deployment()
{
return $this->belongsTo(Deployment::class);
}

public function server()
{
return $this->belongsTo(Server::class);
}
}
11 changes: 0 additions & 11 deletions database/migrations/2023_03_20_074952_create_jobs_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('jobs', function (Blueprint $table) {
Expand All @@ -20,12 +17,4 @@ public function up(): void
$table->unsignedInteger('created_at');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobs');
}
};
Loading