Add pre-deployment and post-deployment commands

This commit is contained in:
Andras Bacsai 2024-03-13 14:41:31 +01:00
parent 62905f084f
commit a53d888747
2 changed files with 26 additions and 24 deletions

View File

@ -240,6 +240,7 @@ public function handle(): void
}
$this->next(ApplicationDeploymentStatus::FINISHED->value);
$this->application->isConfigurationChanged(false);
$this->run_post_deployment_command();
return;
} else if ($this->pull_request_id !== 0) {
$this->deploy_pull_request();
@ -1700,11 +1701,11 @@ private function run_pre_deployment_command()
if ($containers->count() == 0) {
return;
}
$this->application_deployment_queue->addLogEntry("Executing pre deployment command: {$this->application->post_deployment_command}");
$this->application_deployment_queue->addLogEntry("Executing pre-deployment command (see debug log for output): {$this->application->pre_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)) {
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(
@ -1715,7 +1716,7 @@ private function run_pre_deployment_command()
return;
}
}
throw new RuntimeException('Pre deployment command: Could not find a valid container. Is the container name correct?');
throw new RuntimeException('Pre-deployment command: Could not find a valid container. Is the container name correct?');
}
private function run_post_deployment_command()
@ -1723,12 +1724,12 @@ 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}");
$this->application_deployment_queue->addLogEntry("Executing post-deployment command (see debug log for output): {$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)) {
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(
@ -1739,7 +1740,7 @@ private function run_post_deployment_command()
return;
}
}
throw new RuntimeException('Post deployment command: Could not find a valid container. Is the container name correct?');
throw new RuntimeException('Post-deployment command: Could not find a valid container. Is the container name correct?');
}
private function next(string $status)

View File

@ -251,17 +251,18 @@ class="underline" href="https://coolify.io/docs/docker/registry"
<x-forms.button wire:click="resetDefaultLabels">Reset to Coolify Generated Labels</x-forms.button>
@endif
<h3 class="pt-8">Deployment scripts</h3>
<h3 class="pt-8">Pre/Post Deployment Commands</h3>
<div class="flex flex-col gap-2 xl:flex-row">
<x-forms.input placeholder="" id="application.pre_deployment_command" label="Pre deployment command"
<x-forms.input id="application.pre_deployment_command" label="Pre-deployment Command"
helper="An optional script or command to execute in the existing container before the deployment begins." />
<x-forms.input placeholder="" id="application.pre_deployment_command_container" label="Container name"
<x-forms.input id="application.pre_deployment_command_container" label="Container Name"
helper="The name of the container to execute within. You can leave it blank if your application only has one container." />
</div>
<div class="flex flex-col gap-2 xl:flex-row">
<x-forms.input placeholder="php artisan migrate" id="application.post_deployment_command" label="Post deployment command"
<x-forms.input placeholder="php artisan migrate" id="application.post_deployment_command"
label="Post-deployment Command"
helper="An optional script or command to execute in the newly built container after the deployment completes." />
<x-forms.input placeholder="php" id="application.post_deployment_command_container" label="Container name"
<x-forms.input id="application.post_deployment_command_container" label="Container Name"
helper="The name of the container to execute within. You can leave it blank if your application only has one container." />
</div>
</div>