2026-03-29 03:03:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\Service;
|
|
|
|
|
|
|
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
use Spatie\Activitylog\Contracts\Activity;
|
|
|
|
|
|
|
|
|
|
class DeployServiceApplication
|
|
|
|
|
{
|
|
|
|
|
use AsAction;
|
|
|
|
|
|
|
|
|
|
public string $jobQueue = 'high';
|
|
|
|
|
|
|
|
|
|
public function handle(ServiceApplication $serviceApplication, bool $pullLatestImages = false, bool $forceRebuild = false): Activity
|
|
|
|
|
{
|
|
|
|
|
$service = $serviceApplication->service;
|
|
|
|
|
$composeServiceName = $serviceApplication->name;
|
|
|
|
|
|
|
|
|
|
$service->parse();
|
|
|
|
|
$service->saveComposeConfigs();
|
|
|
|
|
$service->isConfigurationChanged(save: true);
|
|
|
|
|
|
|
|
|
|
$workdir = $service->workdir();
|
2026-07-07 13:10:24 +00:00
|
|
|
$composeFile = "{$workdir}/docker-compose.yml";
|
|
|
|
|
$safeWorkdir = escapeshellarg($workdir);
|
|
|
|
|
$safeComposeFile = escapeshellarg($composeFile);
|
|
|
|
|
$safeProjectName = escapeshellarg($service->uuid);
|
|
|
|
|
$safeComposeServiceName = escapeshellarg($composeServiceName);
|
|
|
|
|
|
2026-03-29 03:03:47 +00:00
|
|
|
$commands = collect([
|
2026-07-07 13:10:24 +00:00
|
|
|
'echo '.escapeshellarg("Saved configuration files to {$workdir}."),
|
|
|
|
|
'touch '.escapeshellarg("{$workdir}/.env"),
|
2026-03-29 03:03:47 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($pullLatestImages) {
|
|
|
|
|
$commands->push('echo Pulling image for service.');
|
2026-07-07 13:10:24 +00:00
|
|
|
$commands->push("docker compose --project-directory {$safeWorkdir} -f {$safeComposeFile} --project-name {$safeProjectName} pull {$safeComposeServiceName}");
|
2026-03-29 03:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($service->networks()->count() > 0) {
|
|
|
|
|
$commands->push('echo Creating Docker network.');
|
2026-07-07 13:10:24 +00:00
|
|
|
$commands->push("docker network inspect {$safeProjectName} >/dev/null 2>&1 || docker network create --attachable {$safeProjectName}");
|
2026-03-29 03:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-07 13:10:24 +00:00
|
|
|
$upCommand = "docker compose --project-directory {$safeWorkdir} -f {$safeComposeFile} --project-name {$safeProjectName} up -d --no-deps";
|
2026-03-29 03:03:47 +00:00
|
|
|
if ($forceRebuild) {
|
|
|
|
|
$upCommand .= ' --build';
|
|
|
|
|
}
|
2026-07-07 13:10:24 +00:00
|
|
|
$upCommand .= " {$safeComposeServiceName}";
|
2026-03-29 03:03:47 +00:00
|
|
|
$commands->push('echo Starting service container.');
|
|
|
|
|
$commands->push($upCommand);
|
|
|
|
|
|
2026-07-07 13:10:24 +00:00
|
|
|
$commands->push("docker network connect {$safeProjectName} coolify-proxy >/dev/null 2>&1 || true");
|
2026-03-29 03:03:47 +00:00
|
|
|
|
|
|
|
|
if (data_get($service, 'connect_to_docker_network')) {
|
2026-07-07 13:10:24 +00:00
|
|
|
$network = escapeshellarg($service->destination->network);
|
|
|
|
|
$containerName = escapeshellarg("{$composeServiceName}-{$service->uuid}");
|
|
|
|
|
$networkAlias = escapeshellarg("{$composeServiceName}-{$service->uuid}");
|
|
|
|
|
$commands->push("docker network connect --alias {$networkAlias} {$network} {$containerName} >/dev/null 2>&1 || true");
|
2026-03-29 03:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return remote_process($commands->toArray(), $service->server, type_uuid: $service->uuid, callEventOnFinish: 'ServiceStatusChanged');
|
|
|
|
|
}
|
|
|
|
|
}
|