From 77a01798226a2636ebe3264b2c42a71d69e8ca58 Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Thu, 8 Feb 2024 19:27:43 +1000 Subject: [PATCH 1/2] Added basic support for post-deployment commands. --- app/Jobs/ApplicationDeploymentJob.php | 25 ++++++++++++++++ app/Livewire/Project/Application/General.php | 2 ++ ...23_add_post_deployment_to_applications.php | 30 +++++++++++++++++++ .../project/application/general.blade.php | 8 +++++ 4 files changed, 65 insertions(+) create mode 100644 database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 9d0e069ce..ca19a3b23 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -257,6 +257,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted ApplicationPullRequestUpdateJob::dispatch(application: $this->application, preview: $this->preview, deployment_uuid: $this->deployment_uuid, status: ProcessStatus::FINISHED); } } + $this->run_post_deployment_command(); $this->application->isConfigurationChanged(true); } catch (Exception $e) { if ($this->pull_request_id !== 0 && $this->application->is_github_based()) { @@ -1515,6 +1516,30 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); ]); } + private function run_post_deployment_command() + { + if (empty($this->application->post_deployment_command)) { + return; + } + $this->application_deployment_queue->addLogEntry("Executing post deployment command: {$this->application->post_deployment_command}"); + + $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); + foreach ($containers as $container) { + $containerName = data_get($container, 'Names'); + if ($containers->count() == 1 || str_starts_with($containerName, $this->application->post_deployment_command_container. '-' . $this->application->uuid)) { + $cmd = 'sh -c "' . str_replace('"', '\"', $this->application->post_deployment_command) . '"'; + $exec = "docker exec {$containerName} {$cmd}"; + $this->execute_remote_command( + [ + executeInDocker($this->deployment_uuid, $exec), 'hidden' => true + ], + ); + return; + } + } + throw new RuntimeException('Post deployment command: Could not find a valid container. Is the container name correct?'); + } + private function next(string $status) { queue_next_deployment($this->application); diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 0f6d61957..277ec23d3 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -66,6 +66,8 @@ class General extends Component 'application.docker_compose_custom_build_command' => 'nullable', 'application.custom_labels' => 'nullable', 'application.custom_docker_run_options' => 'nullable', + 'application.post_deployment_command' => 'nullable', + 'application.post_deployment_command_container' => 'nullable', 'application.settings.is_static' => 'boolean|required', 'application.settings.is_raw_compose_deployment_enabled' => 'boolean|required', 'application.settings.is_build_server_enabled' => 'boolean|required', diff --git a/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php b/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php new file mode 100644 index 000000000..ccc86684a --- /dev/null +++ b/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php @@ -0,0 +1,30 @@ +string('post_deployment_command')->nullable(); + $table->string('post_deployment_command_container')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('applications', function (Blueprint $table) { + $table->dropColumn('post_deployment_command'); + $table->dropColumn('post_deployment_command_container'); + }); + } +}; diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index 3e5e4f137..189265834 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -236,6 +236,14 @@ Reset to Coolify Generated Labels @endif + +

Deployment scripts

+
+ + +
From 0538c2f4789b4615919c12ae1e705f750e978604 Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Thu, 8 Feb 2024 20:02:30 +1000 Subject: [PATCH 2/2] Added pre-deployment support. --- app/Jobs/ApplicationDeploymentJob.php | 29 ++++++++++++++++++- app/Livewire/Project/Application/General.php | 2 ++ ...23_add_post_deployment_to_applications.php | 4 +++ .../project/application/general.blade.php | 8 ++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index ca19a3b23..3532f5c84 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -783,8 +783,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted [ "command" => executeInDocker($this->deployment_uuid, "mkdir -p {$this->basedir}") ], - ); + $this->run_pre_deployment_command(); } private function deploy_to_additional_destinations() { @@ -1516,6 +1516,33 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); ]); } + private function run_pre_deployment_command() + { + if (empty($this->application->pre_deployment_command)) { + return; + } + $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); + if ($containers->count() == 0) { + return; + } + $this->application_deployment_queue->addLogEntry("Executing pre deployment command: {$this->application->post_deployment_command}"); + + foreach ($containers as $container) { + $containerName = data_get($container, 'Names'); + if ($containers->count() == 1 || str_starts_with($containerName, $this->application->pre_deployment_command_container. '-' . $this->application->uuid)) { + $cmd = 'sh -c "' . str_replace('"', '\"', $this->application->pre_deployment_command) . '"'; + $exec = "docker exec {$containerName} {$cmd}"; + $this->execute_remote_command( + [ + executeInDocker($this->deployment_uuid, $exec), 'hidden' => true + ], + ); + return; + } + } + throw new RuntimeException('Pre deployment command: Could not find a valid container. Is the container name correct?'); + } + private function run_post_deployment_command() { if (empty($this->application->post_deployment_command)) { diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 277ec23d3..f018e3b9d 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -66,6 +66,8 @@ class General extends Component 'application.docker_compose_custom_build_command' => 'nullable', 'application.custom_labels' => 'nullable', 'application.custom_docker_run_options' => 'nullable', + 'application.pre_deployment_command' => 'nullable', + 'application.pre_deployment_command_container' => 'nullable', 'application.post_deployment_command' => 'nullable', 'application.post_deployment_command_container' => 'nullable', 'application.settings.is_static' => 'boolean|required', diff --git a/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php b/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php index ccc86684a..6d3a74896 100644 --- a/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php +++ b/database/migrations/2024_02_08_075523_add_post_deployment_to_applications.php @@ -14,6 +14,8 @@ return new class extends Migration Schema::table('applications', function (Blueprint $table) { $table->string('post_deployment_command')->nullable(); $table->string('post_deployment_command_container')->nullable(); + $table->string('pre_deployment_command')->nullable(); + $table->string('pre_deployment_command_container')->nullable(); }); } @@ -25,6 +27,8 @@ return new class extends Migration Schema::table('applications', function (Blueprint $table) { $table->dropColumn('post_deployment_command'); $table->dropColumn('post_deployment_command_container'); + $table->dropColumn('pre_deployment_command'); + $table->dropColumn('pre_deployment_command_container'); }); } }; diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index 189265834..f17f80af7 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -238,9 +238,15 @@ @endif

Deployment scripts

+
+ + +
+ helper="An optional script or command to execute in the newly built container after the deployment completes." />