56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreEnvironmentDeploymentRequest;
|
|
use App\Jobs\Environments\DeployEnvironment;
|
|
use App\Models\Application;
|
|
use App\Models\Environment;
|
|
use App\Models\Organisation;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class EnvironmentDeploymentController extends Controller
|
|
{
|
|
public function store(StoreEnvironmentDeploymentRequest $request, Organisation $organisation, Application $application, Environment $environment): RedirectResponse
|
|
{
|
|
abort_unless(
|
|
(int) $application->organisation_id === (int) $organisation->id
|
|
&& (int) $environment->application_id === (int) $application->id,
|
|
404,
|
|
);
|
|
|
|
$environment->loadMissing('services.replicas');
|
|
|
|
if ($organisation->registries()->doesntExist() && $this->serverIdsFor($environment)->count() > 1) {
|
|
return back()->with('error', 'Configure a registry before deploying this environment to multiple servers.');
|
|
}
|
|
|
|
dispatch(new DeployEnvironment(
|
|
environment: $environment,
|
|
targetCommit: $request->validated('target_commit') ?: null,
|
|
));
|
|
|
|
return redirect()->route('environments.show', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Support\Collection<int, int>
|
|
*/
|
|
private function serverIdsFor(Environment $environment): Collection
|
|
{
|
|
return $environment->services
|
|
->flatMap(fn ($service) => [
|
|
$service->server_id,
|
|
...$service->replicas->pluck('server_id')->all(),
|
|
])
|
|
->filter()
|
|
->unique()
|
|
->values();
|
|
}
|
|
}
|