From fb2598f2e464b273be508e38aa4b6713e2142739 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 10 Nov 2023 11:33:15 +0100 Subject: [PATCH] Update UI elements and add new build pack option (static) --- app/Jobs/ApplicationDeploymentJob.php | 83 ++++++++++++++++--- resources/js/components/MagicBar.vue | 2 +- .../components/navbar-subscription.blade.php | 2 +- resources/views/components/navbar.blade.php | 2 +- .../project/application/general.blade.php | 5 +- 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 74d8f2af2..469566060 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -192,6 +192,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->deploy_dockerimage_buildpack(); } else if ($this->application->build_pack === 'dockerfile') { $this->deploy_dockerfile_buildpack(); + } else if ($this->application->build_pack === 'static') { + $this->deploy_static_buildpack(); } else { if ($this->pull_request_id !== 0) { $this->deploy_pull_request(); @@ -227,6 +229,14 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted [ "docker rm -f {$this->deployment_uuid} >/dev/null 2>&1", "hidden" => true, + "ignore_errors" => true, + ] + ); + $this->execute_remote_command( + [ + "docker image prune -f >/dev/null 2>&1", + "hidden" => true, + "ignore_errors" => true, ] ); } @@ -421,6 +431,23 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->build_image(); $this->rolling_update(); } + private function deploy_static_buildpack() + { + $this->execute_remote_command( + [ + "echo 'Starting deployment of {$this->customRepository}:{$this->application->git_branch}.'" + ], + ); + $this->prepare_builder_image(); + $this->check_git_if_build_needed(); + $this->set_base_dir(); + $this->generate_image_names(); + $this->clone_repository(); + $this->cleanup_git(); + $this->build_image(); + $this->generate_compose_file(); + $this->rolling_update(); + } private function rolling_update() { @@ -910,22 +937,26 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function build_image() { - $this->execute_remote_command([ - "echo -n 'Building docker image for your application. To check the current progress, click on Show Debug Logs.'", - ]); - - if ($this->application->settings->is_static) { + if ($this->application->build_pack === 'static') { $this->execute_remote_command([ - executeInDocker($this->deployment_uuid, "docker build $this->buildTarget $this->addHosts --network host -f {$this->workdir}/{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"), "hidden" => true + "echo -n 'Static deployment. Copying static assets to the image.'", ]); + } else { + $this->execute_remote_command([ + "echo -n 'Building docker image for your application. To check the current progress, click on Show Debug Logs.'", + ]); + } - $dockerfile = base64_encode("FROM {$this->application->static_image} + if ($this->application->settings->is_static || $this->application->build_pack === 'static') { + if ($this->application->build_pack === 'static') { + $dockerfile = base64_encode("FROM {$this->application->static_image} WORKDIR /usr/share/nginx/html/ LABEL coolify.deploymentId={$this->deployment_uuid} -COPY --from=$this->build_image_name /app/{$this->application->publish_directory} . +COPY . . +RUN rm -f /usr/share/nginx/html/nginx.conf +RUN rm -f /usr/share/nginx/html/Dockerfile COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); - - $nginx_config = base64_encode("server { + $nginx_config = base64_encode("server { listen 80; listen [::]:80; server_name localhost; @@ -941,15 +972,43 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); root /usr/share/nginx/html; } }"); + } else { + $this->execute_remote_command([ + executeInDocker($this->deployment_uuid, "docker build $this->buildTarget $this->addHosts --network host -f {$this->workdir}/{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"), "hidden" => true + ]); + + $dockerfile = base64_encode("FROM {$this->application->static_image} +WORKDIR /usr/share/nginx/html/ +LABEL coolify.deploymentId={$this->deployment_uuid} +COPY --from=$this->build_image_name /app/{$this->application->publish_directory} . +COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); + + $nginx_config = base64_encode("server { + listen 80; + listen [::]:80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html; + try_files \$uri \$uri.html \$uri/index.html \$uri/ /index.html =404; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + }"); + } $this->execute_remote_command( [ - executeInDocker($this->deployment_uuid, "echo '{$dockerfile}' | base64 -d > {$this->workdir}/Dockerfile-prod") + executeInDocker($this->deployment_uuid, "echo '{$dockerfile}' | base64 -d > {$this->workdir}/Dockerfile") ], [ executeInDocker($this->deployment_uuid, "echo '{$nginx_config}' | base64 -d > {$this->workdir}/nginx.conf") ], [ - executeInDocker($this->deployment_uuid, "docker build $this->addHosts --network host -f {$this->workdir}/Dockerfile-prod {$this->build_args} --progress plain -t $this->production_image_name {$this->workdir}"), "hidden" => true + executeInDocker($this->deployment_uuid, "docker build $this->addHosts --network host -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t $this->production_image_name {$this->workdir}"), "hidden" => true ] ); } else { diff --git a/resources/js/components/MagicBar.vue b/resources/js/components/MagicBar.vue index 9797e14b6..4fc1fc5df 100644 --- a/resources/js/components/MagicBar.vue +++ b/resources/js/components/MagicBar.vue @@ -1,7 +1,7 @@