From 0be8ffbdc9431a2b683925b1316826d40ee82312 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 10 Oct 2023 14:02:43 +0200 Subject: [PATCH] feat: add dockerfile location --- .../Livewire/Project/Application/General.php | 2 + app/Jobs/ApplicationDeploymentJob.php | 43 ++++++++++++++++++- app/Models/Application.php | 20 +++++++-- ...dockerfile_location_applications_table.php | 28 ++++++++++++ .../project/application/general.blade.php | 5 +++ 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2023_10_10_113144_add_dockerfile_location_applications_table.php diff --git a/app/Http/Livewire/Project/Application/General.php b/app/Http/Livewire/Project/Application/General.php index 93fcd250b..afe1b62dd 100644 --- a/app/Http/Livewire/Project/Application/General.php +++ b/app/Http/Livewire/Project/Application/General.php @@ -51,6 +51,7 @@ class General extends Component 'application.dockerfile' => 'nullable', 'application.docker_registry_image_name' => 'nullable', 'application.docker_registry_image_tag' => 'nullable', + 'application.dockerfile_location' => 'nullable', ]; protected $validationAttributes = [ 'application.name' => 'name', @@ -71,6 +72,7 @@ class General extends Component 'application.dockerfile' => 'Dockerfile', 'application.docker_registry_image_name' => 'Docker registry image name', 'application.docker_registry_image_tag' => 'Docker registry image tag', + 'application.dockerfile_location' => 'Dockerfile location', ]; diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index a1dff9c93..f4b839ec3 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -57,6 +57,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private string|null $currently_running_container_name = null; private string $basedir; private string $workdir; + private ?string $build_pack = null; private string $configuration_dir; private string $build_image_name; private string $production_image_name; @@ -65,6 +66,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private $env_args; private $docker_compose; private $docker_compose_base64; + private string $dockerfile_location = '/Dockerfile'; private $log_model; private Collection $saved_outputs; @@ -76,6 +78,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->application_deployment_queue = ApplicationDeploymentQueue::find($application_deployment_queue_id); $this->log_model = $this->application_deployment_queue; $this->application = Application::find($this->application_deployment_queue->application_id); + $this->build_pack = data_get($this->application, 'build_pack'); $this->application_deployment_queue_id = $application_deployment_queue_id; $this->deployment_uuid = $this->application_deployment_queue->deployment_uuid; @@ -140,6 +143,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->deploy_simple_dockerfile(); } else if ($this->application->build_pack === 'dockerimage') { $this->deploy_dockerimage(); + } else if ($this->application->build_pack === 'dockerfile') { + $this->deploy_dockerfile(); } else { if ($this->pull_request_id !== 0) { $this->deploy_pull_request(); @@ -178,6 +183,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted ); } } + private function deploy_docker_compose() { $dockercompose_base64 = base64_encode($this->application->dockercompose); @@ -265,6 +271,35 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->generate_compose_file(); $this->rolling_update(); } + + private function deploy_dockerfile() + { + if (data_get($this->application, 'dockerfile_location')) { + $this->dockerfile_location = $this->application->dockerfile_location; + } + $this->execute_remote_command( + [ + "echo 'Starting deployment of {$this->application->git_repository}:{$this->application->git_branch}.'" + ], + ); + $this->prepare_builder_image(); + $this->clone_repository(); + $this->set_base_dir(); + $tag = Str::of("{$this->commit}-{$this->application->id}-{$this->pull_request_id}"); + if (strlen($tag) > 128) { + $tag = $tag->substr(0, 128); + } + + $this->build_image_name = Str::lower("{$this->application->git_repository}:{$tag}-build"); + $this->production_image_name = Str::lower("{$this->application->uuid}:{$tag}"); + // ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green(); + $this->cleanup_git(); + $this->generate_compose_file(); + $this->generate_build_env_variables(); + $this->add_build_env_variables_to_dockerfile(); + // $this->build_image(); + $this->rolling_update(); + } private function deploy() { $this->execute_remote_command( @@ -629,6 +664,12 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted if (count($volume_names) > 0) { $docker_compose['volumes'] = $volume_names; } + if ($this->build_pack === 'dockerfile') { + $docker_compose['services'][$this->container_name]['build'] = [ + 'context' => $this->workdir, + 'dockerfile' => $this->workdir . $this->dockerfile_location, + ]; + } $this->docker_compose = Yaml::dump($docker_compose, 10); $this->docker_compose_base64 = base64_encode($this->docker_compose); $this->execute_remote_command([executeInDocker($this->deployment_uuid, "echo '{$this->docker_compose_base64}' | base64 -d > {$this->workdir}/docker-compose.yml"), "hidden" => true]); @@ -785,7 +826,7 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); { $this->execute_remote_command( ["echo -n 'Starting application (could take a while).'"], - [executeInDocker($this->deployment_uuid, "docker compose --project-directory {$this->workdir} up -d >/dev/null"), "hidden" => true], + [executeInDocker($this->deployment_uuid, "docker compose --project-directory {$this->workdir} up --build -d >/dev/null"), "hidden" => true], ); } diff --git a/app/Models/Application.php b/app/Models/Application.php index cd3b43e05..634569c0f 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -101,7 +101,21 @@ class Application extends BaseModel } ); } - + public function dockerfileLocation(): Attribute + { + return Attribute::make( + set: function ($value) { + if (is_null($value) || $value === '') { + return '/Dockerfile'; + } else { + if ($value !== '/') { + return Str::start(Str::replaceEnd('/', '', $value), '/'); + } + return Str::start($value, '/'); + } + } + ); + } public function baseDirectory(): Attribute { return Attribute::make( @@ -259,14 +273,14 @@ class Application extends BaseModel if ($this->dockerfile) { return false; } - if ($this->build_pack === 'dockerimage'){ + if ($this->build_pack === 'dockerimage') { return false; } return true; } public function isHealthcheckDisabled(): bool { - if (data_get($this, 'dockerfile') || data_get($this, 'build_pack') === 'dockerfile' || data_get($this, 'health_check_enabled') === false) { + if (data_get($this, 'health_check_enabled') === false) { return true; } return false; diff --git a/database/migrations/2023_10_10_113144_add_dockerfile_location_applications_table.php b/database/migrations/2023_10_10_113144_add_dockerfile_location_applications_table.php new file mode 100644 index 000000000..39956a9ed --- /dev/null +++ b/database/migrations/2023_10_10_113144_add_dockerfile_location_applications_table.php @@ -0,0 +1,28 @@ +string('dockerfile_location')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('applications', function (Blueprint $table) { + $table->dropColumn('dockerfile_location'); + }); + } +}; diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php index 89c748aed..4492c8192 100644 --- a/resources/views/livewire/project/application/general.blade.php +++ b/resources/views/livewire/project/application/general.blade.php @@ -56,6 +56,11 @@
+ @if ($application->build_pack === 'dockerfile') + + @endif @if ($application->could_set_build_commands()) @if ($application->settings->is_static)