From 6fe791c1f1f2012427fa3c862c1737ad1ad98b58 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 1 Mar 2024 11:43:42 +0100 Subject: [PATCH] fix: pull request deployments + build servers --- app/Jobs/ApplicationDeploymentJob.php | 123 ++++++++---------- .../project/application/advanced.blade.php | 70 +++++----- .../project/application/general.blade.php | 13 +- 3 files changed, 102 insertions(+), 104 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index bc4b655b3..becf6387a 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -218,12 +218,12 @@ public function handle(): void $teamId = data_get($this->application, 'environment.project.team.id'); $buildServers = Server::buildServers($teamId)->get(); if ($buildServers->count() === 0) { - $this->application_deployment_queue->addLogEntry("Build server feature activated, but no suitable build server found. Using the deployment server."); + $this->application_deployment_queue->addLogEntry("No suitable build server found. Using the deployment server."); $this->build_server = $this->server; $this->original_server = $this->server; } else { - $this->application_deployment_queue->addLogEntry("Build server feature activated and found a suitable build server. Using it to build your application - if needed."); $this->build_server = $buildServers->random(); + $this->application_deployment_queue->addLogEntry("Found a suitable build server ({$this->build_server->name})."); $this->original_server = $this->server; $this->use_build_server = true; } @@ -427,13 +427,13 @@ private function deploy_docker_compose_buildpack() } private function deploy_dockerfile_buildpack() { + $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); if ($this->use_build_server) { $this->server = $this->build_server; } if (data_get($this->application, 'dockerfile_location')) { $this->dockerfile_location = $this->application->dockerfile_location; } - $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}."); $this->prepare_builder_image(); $this->check_git_if_build_needed(); $this->set_base_dir(); @@ -528,9 +528,11 @@ private function write_deployment_configurations() $this->server = $this->original_server; } $readme = generate_readme_file($this->application->name, $this->application_deployment_queue->updated_at); - $composeFileName = "$this->configuration_dir/docker-compose.yml"; - if ($this->pull_request_id !== 0) { + if ($this->pull_request_id === 0) { + $composeFileName = "$this->configuration_dir/docker-compose.yml"; + } else { $composeFileName = "$this->configuration_dir/docker-compose-pr-{$this->pull_request_id}.yml"; + $this->docker_compose_location = "/docker-compose-pr-{$this->pull_request_id}.yml"; } $this->execute_remote_command( [ @@ -725,7 +727,7 @@ private function rolling_update() $this->write_deployment_configurations(); $this->server = $this->original_server; } - if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled) { + if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled || $this->application->pull_request_id !== 0) { $this->application_deployment_queue->addLogEntry("----------------------------------------"); if (count($this->application->ports_mappings_array) > 0) { $this->application_deployment_queue->addLogEntry("Application has ports mapped to the host system, rolling update is not supported."); @@ -733,6 +735,10 @@ private function rolling_update() if ((bool) $this->application->settings->is_consistent_container_name_enabled) { $this->application_deployment_queue->addLogEntry("Consistent container name feature enabled, rolling update is not supported."); } + if ($this->application->pull_request_id !== 0) { + $this->application->settings->is_consistent_container_name_enabled = true; + $this->application_deployment_queue->addLogEntry("Pull request deployment, rolling update is not supported."); + } $this->stop_running_container(force: true); $this->start_by_compose_file(); } else { @@ -810,26 +816,9 @@ private function deploy_pull_request() $this->add_build_env_variables_to_dockerfile(); } $this->build_image(); - $this->stop_running_container(); - if ($this->application->destination->server->isSwarm()) { - $this->push_to_docker_registry(); - $this->execute_remote_command( - [ - executeInDocker($this->deployment_uuid, "docker stack deploy --with-registry-auth -c {$this->workdir}{$this->docker_compose_location} {$this->application->uuid}-{$this->pull_request_id}") - ], - ); - } else { - $this->application_deployment_queue->addLogEntry("Starting preview deployment."); - if ($this->use_build_server) { - $this->execute_remote_command( - ["SOURCE_COMMIT={$this->commit} docker compose --project-directory {$this->configuration_dir} -f {$this->configuration_dir}{$this->docker_compose_location} up --build -d", "hidden" => true], - ); - } else { - $this->execute_remote_command( - [executeInDocker($this->deployment_uuid, "SOURCE_COMMIT={$this->commit} docker compose --project-directory {$this->workdir} -f {$this->workdir}{$this->docker_compose_location} up --build -d"), "hidden" => true], - ); - } - } + $this->push_to_docker_registry(); + // $this->stop_running_container(); + $this->rolling_update(); } private function create_workdir() { @@ -1226,43 +1215,45 @@ private function generate_compose_file() // ]; // } - if ((bool)$this->application->settings->is_consistent_container_name_enabled) { - $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); - if (count($custom_compose) > 0) { - $ipv4 = data_get($custom_compose, 'ip.0'); - $ipv6 = data_get($custom_compose, 'ip6.0'); - data_forget($custom_compose, 'ip'); - data_forget($custom_compose, 'ip6'); - if ($ipv4 || $ipv6) { - data_forget($docker_compose['services'][$this->application->uuid], 'networks'); + if ($this->application->pull_request_id === 0) { + if ((bool)$this->application->settings->is_consistent_container_name_enabled) { + $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); + if (count($custom_compose) > 0) { + $ipv4 = data_get($custom_compose, 'ip.0'); + $ipv6 = data_get($custom_compose, 'ip6.0'); + data_forget($custom_compose, 'ip'); + data_forget($custom_compose, 'ip6'); + if ($ipv4 || $ipv6) { + data_forget($docker_compose['services'][$this->container_name], 'networks'); + } + if ($ipv4) { + $docker_compose['services'][$this->container_name]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; + } + if ($ipv6) { + $docker_compose['services'][$this->container_name]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; + } + $docker_compose['services'][$this->container_name] = array_merge_recursive($docker_compose['services'][$this->container_name], $custom_compose); } - if ($ipv4) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; + } else { + $docker_compose['services'][$this->application->uuid] = $docker_compose['services'][$this->container_name]; + data_forget($docker_compose, 'services.' . $this->container_name); + $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); + if (count($custom_compose) > 0) { + $ipv4 = data_get($custom_compose, 'ip.0'); + $ipv6 = data_get($custom_compose, 'ip6.0'); + data_forget($custom_compose, 'ip'); + data_forget($custom_compose, 'ip6'); + if ($ipv4 || $ipv6) { + data_forget($docker_compose['services'][$this->application->uuid], 'networks'); + } + if ($ipv4) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; + } + if ($ipv6) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; + } + $docker_compose['services'][$this->application->uuid] = array_merge_recursive($docker_compose['services'][$this->application->uuid], $custom_compose); } - if ($ipv6) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; - } - $docker_compose['services'][$this->container_name] = array_merge_recursive($docker_compose['services'][$this->container_name], $custom_compose); - } - } else { - $docker_compose['services'][$this->application->uuid] = $docker_compose['services'][$this->container_name]; - data_forget($docker_compose, 'services.' . $this->container_name); - $custom_compose = convert_docker_run_to_compose($this->application->custom_docker_run_options); - if (count($custom_compose) > 0) { - $ipv4 = data_get($custom_compose, 'ip.0'); - $ipv6 = data_get($custom_compose, 'ip6.0'); - data_forget($custom_compose, 'ip'); - data_forget($custom_compose, 'ip6'); - if ($ipv4 || $ipv6) { - data_forget($docker_compose['services'][$this->application->uuid], 'networks'); - } - if ($ipv4) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; - } - if ($ipv6) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; - } - $docker_compose['services'][$this->application->uuid] = array_merge_recursive($docker_compose['services'][$this->application->uuid], $custom_compose); } } @@ -1539,18 +1530,18 @@ private function stop_running_container(bool $force = false) $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); if ($this->pull_request_id === 0) { $containers = $containers->filter(function ($container) { - return data_get($container, 'Names') !== $this->container_name; + return data_get($container, 'Names') !== $this->container_name && data_get($container, 'Names') !== $this->container_name . '-pr-' . $this->pull_request_id; }); } $containers->each(function ($container) { $containerName = data_get($container, 'Names'); $this->execute_remote_command( - [executeInDocker($this->deployment_uuid, "docker rm -f $containerName >/dev/null 2>&1"), "hidden" => true, "ignore_errors" => true], + ["docker rm -f $containerName >/dev/null 2>&1", "hidden" => true, "ignore_errors" => true], ); }); if ($this->application->settings->is_consistent_container_name_enabled) { $this->execute_remote_command( - [executeInDocker($this->deployment_uuid, "docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true, "ignore_errors" => true], + ["docker rm -f $this->container_name >/dev/null 2>&1", "hidden" => true, "ignore_errors" => true], ); } } else { @@ -1559,7 +1550,7 @@ private function stop_running_container(bool $force = false) 'status' => ApplicationDeploymentStatus::FAILED->value, ]); $this->execute_remote_command( - [executeInDocker($this->deployment_uuid, "docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true, "ignore_errors" => true], + ["docker rm -f $this->container_name >/dev/null 2>&1", "hidden" => true, "ignore_errors" => true], ); } } @@ -1680,7 +1671,7 @@ public function failed(Throwable $exception): void // 69420 means failed to push the image to the registry, so we don't need to remove the new version as it is the currently running one $this->application_deployment_queue->addLogEntry("Deployment failed. Removing the new version of your application.", 'stderr'); $this->execute_remote_command( - [executeInDocker($this->deployment_uuid, "docker rm -f $this->container_name >/dev/null 2>&1"), "hidden" => true, "ignore_errors" => true] + ["docker rm -f $this->container_name >/dev/null 2>&1", "hidden" => true, "ignore_errors" => true] ); } } diff --git a/resources/views/livewire/project/application/advanced.blade.php b/resources/views/livewire/project/application/advanced.blade.php index 53d984698..e88fb8a27 100644 --- a/resources/views/livewire/project/application/advanced.blade.php +++ b/resources/views/livewire/project/application/advanced.blade.php @@ -4,8 +4,8 @@

Advanced

Advanced configuration for your application.
-
-

General

+
+

General

@if ($application->git_based()) @@ -20,50 +20,52 @@ helper="The deployed container will have the same name ({{ $application->uuid }}). You will lose the rolling update feature!" instantSave id="application.settings.is_consistent_container_name_enabled" label="Consistent Container Names" /> -

Logs

+

Logs

@if (!$application->settings->is_raw_compose_deployment_enabled) @endif @if ($application->git_based()) -

Git

- Git + - @endif -

GPU

-
- @if ($application->build_pack !== 'dockercompose') -
- - @if ($application->settings->is_gpu_enabled) - Save - @endif -
- @endif - @if ($application->settings->is_gpu_enabled) -
- - - - -
-
- - -
- @endif -
{{-- --}}
+

GPU

+
+ @if ($application->build_pack !== 'dockercompose') +
+ + @if ($application->settings->is_gpu_enabled) +
GPU Settings
+ + Save + @endif +
+ @endif + @if ($application->settings->is_gpu_enabled) +
+ + + + +
+
+ + +
+ @endif +
diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index fb8ff2c23..aa104267b 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -8,7 +8,8 @@ @if ($isConfigurationChanged && !is_null($application->config_hash) && !$application->isExited())
- +
@endif @@ -100,11 +101,15 @@ class="underline" href="https://coolify.io/docs/docker/registry" @endif @else - @if ($application->destination->server->isSwarm() || $application->additional_servers->count() > 0) - + @if ( + $application->destination->server->isSwarm() || + $application->additional_servers->count() > 0 || + $application->settings->is_build_server_enabled) + + placeholder="Empty means latest will be used." label="Docker Image Tag" /> @else