This commit is contained in:
Andras Bacsai 2024-05-29 15:15:03 +02:00
parent 022762c0c9
commit 1d5932e63f

View File

@ -17,6 +17,7 @@
use App\Notifications\Application\DeploymentFailed; use App\Notifications\Application\DeploymentFailed;
use App\Notifications\Application\DeploymentSuccess; use App\Notifications\Application\DeploymentSuccess;
use App\Traits\ExecuteRemoteCommand; use App\Traits\ExecuteRemoteCommand;
use Exception;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -26,13 +27,12 @@
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Sleep; use Illuminate\Support\Sleep;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
use Visus\Cuid2\Cuid2;
use Spatie\Url\Url;
use Yosymfony\Toml\Toml;
use Exception;
use RuntimeException; use RuntimeException;
use Spatie\Url\Url;
use Symfony\Component\Yaml\Yaml;
use Throwable; use Throwable;
use Visus\Cuid2\Cuid2;
use Yosymfony\Toml\Toml;
class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
{ {
@ -44,7 +44,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private int $application_deployment_queue_id; private int $application_deployment_queue_id;
private bool $is_new_version_healthy = false; private bool $newVersionIsHealthy = false;
private ApplicationDeploymentQueue $application_deployment_queue; private ApplicationDeploymentQueue $application_deployment_queue;
private Application $application; private Application $application;
private string $deployment_uuid; private string $deployment_uuid;
@ -54,8 +54,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private bool $force_rebuild; private bool $force_rebuild;
private bool $restart_only; private bool $restart_only;
private ?string $docker_image = null; private ?string $dockerImage = null;
private ?string $docker_image_tag = null; private ?string $dockerImageTag = null;
private GithubApp|GitlabApp|string $source = 'other'; private GithubApp|GitlabApp|string $source = 'other';
private StandaloneDocker|SwarmDocker $destination; private StandaloneDocker|SwarmDocker $destination;
@ -66,7 +66,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private bool $use_build_server = false; private bool $use_build_server = false;
// Save original server between phases // Save original server between phases
private Server $original_server; private Server $original_server;
private Server $main_server; private Server $mainServer;
private ?ApplicationPreview $preview = null; private ?ApplicationPreview $preview = null;
private ?string $git_type = null; private ?string $git_type = null;
private bool $only_this_server = false; private bool $only_this_server = false;
@ -92,19 +92,19 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private string $docker_compose_location = '/docker-compose.yml'; private string $docker_compose_location = '/docker-compose.yml';
private ?string $docker_compose_custom_start_command = null; private ?string $docker_compose_custom_start_command = null;
private ?string $docker_compose_custom_build_command = null; private ?string $docker_compose_custom_build_command = null;
private ?string $add_hosts = null; private ?string $addHosts = null;
private ?string $build_target = null; private ?string $buildTarget = null;
private Collection $saved_outputs; private Collection $saved_outputs;
private ?string $full_healthcheck_url = null; private ?string $full_healthcheck_url = null;
private string $server_user = 'root'; private string $serverUser = 'root';
private string $server_user_home_dir = '/root'; private string $serverUserHomeDir = '/root';
private string $is_docker_config_file_exists = 'NOK'; private string $dockerConfigFileExists = 'NOK';
private int $custom_port = 22; private int $customPort = 22;
private ?string $custom_repository = null; private ?string $customRepository = null;
private ?string $full_repository_url = null; private ?string $fullRepoUrl = null;
private ?string $branch = null; private ?string $branch = null;
private ?string $coolify_variables = null; private ?string $coolify_variables = null;
@ -134,8 +134,8 @@ public function __construct(int $application_deployment_queue_id)
$this->server = Server::find($this->application_deployment_queue->server_id); $this->server = Server::find($this->application_deployment_queue->server_id);
$this->timeout = $this->server->settings->dynamic_timeout; $this->timeout = $this->server->settings->dynamic_timeout;
$this->destination = $this->server->destinations()->where('id', $this->application_deployment_queue->destination_id)->first(); $this->destination = $this->server->destinations()->where('id', $this->application_deployment_queue->destination_id)->first();
$this->server = $this->main_server = $this->destination->server; $this->server = $this->mainServer = $this->destination->server;
$this->server_user = $this->server->user; $this->serverUser = $this->server->user;
$this->basedir = $this->application->generateBaseDir($this->deployment_uuid); $this->basedir = $this->application->generateBaseDir($this->deployment_uuid);
$this->workdir = "{$this->basedir}" . rtrim($this->application->base_directory, '/'); $this->workdir = "{$this->basedir}" . rtrim($this->application->base_directory, '/');
$this->configuration_dir = application_configuration_dir() . "/{$this->application->uuid}"; $this->configuration_dir = application_configuration_dir() . "/{$this->application->uuid}";
@ -217,17 +217,17 @@ public function handle(): void
} }
} }
} }
$this->add_hosts = $ips->map(function ($ip, $name) { $this->addHosts = $ips->map(function ($ip, $name) {
return "--add-host $name:$ip"; return "--add-host $name:$ip";
})->implode(' '); })->implode(' ');
} }
if ($this->application->dockerfile_target_build) { if ($this->application->dockerfile_target_build) {
$this->build_target = " --target {$this->application->dockerfile_target_build} "; $this->buildTarget = " --target {$this->application->dockerfile_target_build} ";
} }
// Check custom port // Check custom port
['repository' => $this->custom_repository, 'port' => $this->custom_port] = $this->application->custom_repository(); ['repository' => $this->customRepository, 'port' => $this->customPort] = $this->application->customRepository();
if (data_get($this->application, 'settings.is_build_server_enabled')) { if (data_get($this->application, 'settings.is_build_server_enabled')) {
$teamId = data_get($this->application, 'environment.project.team.id'); $teamId = data_get($this->application, 'environment.project.team.id');
@ -355,14 +355,14 @@ private function deploy_simple_dockerfile()
} }
private function deploy_dockerimage_buildpack() private function deploy_dockerimage_buildpack()
{ {
$this->docker_image = $this->application->docker_registry_image_name; $this->dockerImage = $this->application->docker_registry_image_name;
if (str($this->application->docker_registry_image_tag)->isEmpty()) { if (str($this->application->docker_registry_image_tag)->isEmpty()) {
$this->docker_image_tag = 'latest'; $this->dockerImageTag = 'latest';
} else { } else {
$this->docker_image_tag = $this->application->docker_registry_image_tag; $this->dockerImageTag = $this->application->docker_registry_image_tag;
} }
ray("echo 'Starting deployment of {$this->docker_image}:{$this->docker_image_tag} to {$this->server->name}.'"); ray("echo 'Starting deployment of {$this->dockerImage}:{$this->dockerImageTag} to {$this->server->name}.'");
$this->application_deployment_queue->addLogEntry("Starting deployment of {$this->docker_image}:{$this->docker_image_tag} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->dockerImage}:{$this->dockerImageTag} to {$this->server->name}.");
$this->generate_image_names(); $this->generate_image_names();
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->generate_compose_file(); $this->generate_compose_file();
@ -382,7 +382,7 @@ private function deploy_docker_compose_buildpack()
if ($this->pull_request_id === 0) { if ($this->pull_request_id === 0) {
$this->application_deployment_queue->addLogEntry("Starting deployment of {$this->application->name} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->application->name} to {$this->server->name}.");
} else { } else {
$this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}.");
} }
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->check_git_if_build_needed(); $this->check_git_if_build_needed();
@ -464,7 +464,7 @@ private function deploy_docker_compose_buildpack()
} }
private function deploy_dockerfile_buildpack() private function deploy_dockerfile_buildpack()
{ {
$this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}.");
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->build_server; $this->server = $this->build_server;
} }
@ -499,7 +499,7 @@ private function deploy_nixpacks_buildpack()
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->build_server; $this->server = $this->build_server;
} }
$this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}.");
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->check_git_if_build_needed(); $this->check_git_if_build_needed();
$this->set_base_dir(); $this->set_base_dir();
@ -532,7 +532,7 @@ private function deploy_static_buildpack()
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->build_server; $this->server = $this->build_server;
} }
$this->application_deployment_queue->addLogEntry("Starting deployment of {$this->custom_repository}:{$this->application->git_branch} to {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Starting deployment of {$this->customRepository}:{$this->application->git_branch} to {$this->server->name}.");
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->check_git_if_build_needed(); $this->check_git_if_build_needed();
$this->set_base_dir(); $this->set_base_dir();
@ -657,7 +657,7 @@ private function generate_image_names()
$this->production_image_name = "{$this->application->uuid}:latest"; $this->production_image_name = "{$this->application->uuid}:latest";
} }
} else if ($this->application->build_pack === 'dockerimage') { } else if ($this->application->build_pack === 'dockerimage') {
$this->production_image_name = "{$this->docker_image}:{$this->docker_image_tag}"; $this->production_image_name = "{$this->dockerImage}:{$this->dockerImageTag}";
} else if ($this->pull_request_id !== 0) { } else if ($this->pull_request_id !== 0) {
if ($this->application->docker_registry_image_name) { if ($this->application->docker_registry_image_name) {
$this->build_image_name = "{$this->application->docker_registry_image_name}:pr-{$this->pull_request_id}-build"; $this->build_image_name = "{$this->application->docker_registry_image_name}:pr-{$this->pull_request_id}-build";
@ -667,22 +667,22 @@ private function generate_image_names()
$this->production_image_name = "{$this->application->uuid}:pr-{$this->pull_request_id}"; $this->production_image_name = "{$this->application->uuid}:pr-{$this->pull_request_id}";
} }
} else { } else {
$this->docker_image_tag = str($this->commit)->substr(0, 128); $this->dockerImageTag = str($this->commit)->substr(0, 128);
if ($this->application->docker_registry_image_tag) { if ($this->application->docker_registry_image_tag) {
$this->docker_image_tag = $this->application->docker_registry_image_tag; $this->dockerImageTag = $this->application->docker_registry_image_tag;
} }
if ($this->application->docker_registry_image_name) { if ($this->application->docker_registry_image_name) {
$this->build_image_name = "{$this->application->docker_registry_image_name}:{$this->docker_image_tag}-build"; $this->build_image_name = "{$this->application->docker_registry_image_name}:{$this->dockerImageTag}-build";
$this->production_image_name = "{$this->application->docker_registry_image_name}:{$this->docker_image_tag}"; $this->production_image_name = "{$this->application->docker_registry_image_name}:{$this->dockerImageTag}";
} else { } else {
$this->build_image_name = "{$this->application->uuid}:{$this->docker_image_tag}-build"; $this->build_image_name = "{$this->application->uuid}:{$this->dockerImageTag}-build";
$this->production_image_name = "{$this->application->uuid}:{$this->docker_image_tag}"; $this->production_image_name = "{$this->application->uuid}:{$this->dockerImageTag}";
} }
} }
} }
private function just_restart() private function just_restart()
{ {
$this->application_deployment_queue->addLogEntry("Restarting {$this->custom_repository}:{$this->application->git_branch} on {$this->server->name}."); $this->application_deployment_queue->addLogEntry("Restarting {$this->customRepository}:{$this->application->git_branch} on {$this->server->name}.");
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->check_git_if_build_needed(); $this->check_git_if_build_needed();
$this->set_base_dir(); $this->set_base_dir();
@ -817,29 +817,16 @@ private function save_environment_variables()
$this->env_filename = null; $this->env_filename = null;
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->original_server; $this->server = $this->original_server;
$this->execute_remote_command( }
[ $this->execute_remote_command(
"command" => "rm -f $this->configuration_dir/{$this->env_filename}", [
"hidden" => true, "command" => "rm -f $this->configuration_dir/{$this->env_filename}",
"ignore_errors" => true "hidden" => true,
] "ignore_errors" => true
); ]
);
if ($this->use_build_server) {
$this->server = $this->build_server; $this->server = $this->build_server;
$this->execute_remote_command(
[
"command" => "rm -f $this->configuration_dir/{$this->env_filename}",
"hidden" => true,
"ignore_errors" => true
]
);
} else {
$this->execute_remote_command(
[
"command" => "rm -f $this->configuration_dir/{$this->env_filename}",
"hidden" => true,
"ignore_errors" => true
]
);
} }
} else { } else {
$envs_base64 = base64_encode($envs->implode("\n")); $envs_base64 = base64_encode($envs->implode("\n"));
@ -851,21 +838,38 @@ private function save_environment_variables()
); );
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->original_server; $this->server = $this->original_server;
$this->execute_remote_command(
[
"echo '$envs_base64' | base64 -d | tee $this->configuration_dir/{$this->env_filename} > /dev/null"
]
);
$this->server = $this->build_server;
} else {
$this->execute_remote_command(
[
"echo '$envs_base64' | base64 -d | tee $this->configuration_dir/{$this->env_filename} > /dev/null"
]
);
} }
$this->execute_remote_command(
[
"echo '$envs_base64' | base64 -d | tee $this->configuration_dir/{$this->env_filename} > /dev/null"
]
);
if ($this->use_build_server) {
$this->server = $this->build_server;
}
} }
// $this->execute_remote_command([
// executeInDocker($this->deployment_uuid, "cat $this->workdir/.env 2>/dev/null || true"),
// "hidden" => true,
// "save" => "dotenv"
// ]);
// if (str($this->saved_outputs->get('dotenv'))->isNotEmpty()) {
// $base64_dotenv = base64_encode($this->saved_outputs->get('dotenv')->value());
// $this->execute_remote_command(
// [
// "echo '{$base64_dotenv}' | base64 -d | tee $this->configuration_dir/.env > /dev/null"
// ]
// );
// } else {
// $this->execute_remote_command(
// [
// "command" => "rm -f $this->configuration_dir/.env",
// "hidden" => true,
// "ignore_errors" => true
// ]
// );
// }
} }
@ -879,7 +883,7 @@ private function framework_based_notification()
$nixpacks_php_fallback_path = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_FALLBACK_PATH')->first(); $nixpacks_php_fallback_path = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_FALLBACK_PATH')->first();
$nixpacks_php_root_dir = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_ROOT_DIR')->first(); $nixpacks_php_root_dir = $this->application->environment_variables_preview->where('key', 'NIXPACKS_PHP_ROOT_DIR')->first();
} }
if ($nixpacks_php_fallback_path?->value === '/index.php' && $nixpacks_php_root_dir?->value === '/app/public' && $this->is_new_version_healthy === false) { if ($nixpacks_php_fallback_path?->value === '/index.php' && $nixpacks_php_root_dir?->value === '/app/public' && $this->newVersionIsHealthy === false) {
$this->application_deployment_queue->addLogEntry("There was a change in how Laravel is deployed. Please update your environment variables to match the new deployment method. More details here: https://coolify.io/docs/resources/laravel", 'stderr'); $this->application_deployment_queue->addLogEntry("There was a change in how Laravel is deployed. Please update your environment variables to match the new deployment method. More details here: https://coolify.io/docs/resources/laravel", 'stderr');
} }
} }
@ -935,7 +939,7 @@ private function health_check()
// Implement healthcheck for swarm // Implement healthcheck for swarm
} else { } else {
if ($this->application->isHealthcheckDisabled() && $this->application->custom_healthcheck_found === false) { if ($this->application->isHealthcheckDisabled() && $this->application->custom_healthcheck_found === false) {
$this->is_new_version_healthy = true; $this->newVersionIsHealthy = true;
return; return;
} }
if ($this->application->custom_healthcheck_found) { if ($this->application->custom_healthcheck_found) {
@ -980,13 +984,13 @@ private function health_check()
} }
if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'healthy') { if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'healthy') {
$this->is_new_version_healthy = true; $this->newVersionIsHealthy = true;
$this->application->update(['status' => 'running']); $this->application->update(['status' => 'running']);
$this->application_deployment_queue->addLogEntry("New container is healthy."); $this->application_deployment_queue->addLogEntry("New container is healthy.");
break; break;
} }
if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'unhealthy') { if (Str::of($this->saved_outputs->get('health_check'))->replace('"', '')->value() === 'unhealthy') {
$this->is_new_version_healthy = false; $this->newVersionIsHealthy = false;
$this->query_logs(); $this->query_logs();
break; break;
} }
@ -1025,9 +1029,9 @@ private function deploy_pull_request()
if ($this->use_build_server) { if ($this->use_build_server) {
$this->server = $this->build_server; $this->server = $this->build_server;
} }
$this->is_new_version_healthy = true; $this->newVersionIsHealthy = true;
$this->generate_image_names(); $this->generate_image_names();
$this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->custom_repository}:{$this->application->git_branch}."); $this->application_deployment_queue->addLogEntry("Starting pull request (#{$this->pull_request_id}) deployment of {$this->customRepository}:{$this->application->git_branch}.");
$this->prepare_builder_image(); $this->prepare_builder_image();
$this->check_git_if_build_needed(); $this->check_git_if_build_needed();
$this->clone_repository(); $this->clone_repository();
@ -1048,47 +1052,29 @@ private function deploy_pull_request()
} }
private function create_workdir() private function create_workdir()
{ {
if ($this->use_build_server) { $this->execute_remote_command(
$this->server = $this->original_server; [
$this->execute_remote_command( "command" => executeInDocker($this->deployment_uuid, "mkdir -p {$this->workdir}")
[ ],
"command" => "mkdir -p {$this->configuration_dir}" [
], "command" => "mkdir -p {$this->configuration_dir}"
); ],
$this->server = $this->build_server; );
$this->execute_remote_command(
[
"command" => executeInDocker($this->deployment_uuid, "mkdir -p {$this->workdir}")
],
[
"command" => "mkdir -p {$this->configuration_dir}"
],
);
} else {
$this->execute_remote_command(
[
"command" => executeInDocker($this->deployment_uuid, "mkdir -p {$this->workdir}")
],
[
"command" => "mkdir -p {$this->configuration_dir}"
],
);
}
} }
private function prepare_builder_image() private function prepare_builder_image()
{ {
$helperImage = config('coolify.helper_image'); $helperImage = config('coolify.helper_image');
// Get user home directory // Get user home directory
$this->server_user_home_dir = instant_remote_process(["echo \$HOME"], $this->server); $this->serverUserHomeDir = instant_remote_process(["echo \$HOME"], $this->server);
$this->is_docker_config_file_exists = instant_remote_process(["test -f {$this->server_user_home_dir}/.docker/config.json && echo 'OK' || echo 'NOK'"], $this->server); $this->dockerConfigFileExists = instant_remote_process(["test -f {$this->serverUserHomeDir}/.docker/config.json && echo 'OK' || echo 'NOK'"], $this->server);
if ($this->use_build_server) { if ($this->use_build_server) {
if ($this->is_docker_config_file_exists === 'NOK') { if ($this->dockerConfigFileExists === 'NOK') {
throw new RuntimeException('Docker config file (~/.docker/config.json) not found on the build server. Please run "docker login" to login to the docker registry on the server.'); throw new RuntimeException('Docker config file (~/.docker/config.json) not found on the build server. Please run "docker login" to login to the docker registry on the server.');
} }
$runCommand = "docker run -d --name {$this->deployment_uuid} --rm -v {$this->server_user_home_dir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; $runCommand = "docker run -d --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}";
} else { } else {
if ($this->is_docker_config_file_exists === 'OK') { if ($this->dockerConfigFileExists === 'OK') {
$runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v {$this->server_user_home_dir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; $runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}";
} else { } else {
$runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; $runCommand = "docker run -d --network {$this->destination->network} --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}";
} }
@ -1132,7 +1118,7 @@ private function deploy_to_additional_destinations()
foreach ($destination_ids as $destination_id) { foreach ($destination_ids as $destination_id) {
$destination = StandaloneDocker::find($destination_id); $destination = StandaloneDocker::find($destination_id);
$server = $destination->server; $server = $destination->server;
if ($server->team_id !== $this->main_server->team_id) { if ($server->team_id !== $this->mainServer->team_id) {
$this->application_deployment_queue->addLogEntry("Skipping deployment to {$server->name}. Not in the same team?!"); $this->application_deployment_queue->addLogEntry("Skipping deployment to {$server->name}. Not in the same team?!");
continue; continue;
} }
@ -1195,7 +1181,7 @@ private function check_git_if_build_needed()
executeInDocker($this->deployment_uuid, "chmod 600 /root/.ssh/id_rsa") executeInDocker($this->deployment_uuid, "chmod 600 /root/.ssh/id_rsa")
], ],
[ [
executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->custom_port} -o Port={$this->custom_port} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git ls-remote {$this->full_repository_url} {$local_branch}"), executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git ls-remote {$this->fullRepoUrl} {$local_branch}"),
"hidden" => true, "hidden" => true,
"save" => "git_commit_sha" "save" => "git_commit_sha"
], ],
@ -1203,12 +1189,13 @@ private function check_git_if_build_needed()
} else { } else {
$this->execute_remote_command( $this->execute_remote_command(
[ [
executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->custom_port} -o Port={$this->custom_port} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git ls-remote {$this->full_repository_url} {$local_branch}"), executeInDocker($this->deployment_uuid, "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git ls-remote {$this->fullRepoUrl} {$local_branch}"),
"hidden" => true, "hidden" => true,
"save" => "git_commit_sha" "save" => "git_commit_sha"
], ],
); );
} }
ray("GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git ls-remote {$this->fullRepoUrl} {$local_branch}");
if ($this->saved_outputs->get('git_commit_sha') && !$this->rollback) { if ($this->saved_outputs->get('git_commit_sha') && !$this->rollback) {
$this->commit = $this->saved_outputs->get('git_commit_sha')->before("\t"); $this->commit = $this->saved_outputs->get('git_commit_sha')->before("\t");
$this->application_deployment_queue->commit = $this->commit; $this->application_deployment_queue->commit = $this->commit;
@ -1220,10 +1207,11 @@ private function clone_repository()
{ {
$importCommands = $this->generate_git_import_commands(); $importCommands = $this->generate_git_import_commands();
$this->application_deployment_queue->addLogEntry("\n----------------------------------------"); $this->application_deployment_queue->addLogEntry("\n----------------------------------------");
$this->application_deployment_queue->addLogEntry("Importing {$this->custom_repository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}."); $this->application_deployment_queue->addLogEntry("Importing {$this->customRepository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}.");
if ($this->pull_request_id !== 0) { if ($this->pull_request_id !== 0) {
$this->application_deployment_queue->addLogEntry("Checking out tag pull/{$this->pull_request_id}/head."); $this->application_deployment_queue->addLogEntry("Checking out tag pull/{$this->pull_request_id}/head.");
} }
ray($importCommands);
$this->execute_remote_command( $this->execute_remote_command(
[ [
$importCommands, "hidden" => true $importCommands, "hidden" => true
@ -1237,6 +1225,8 @@ private function clone_repository()
"save" => "commit_message" "save" => "commit_message"
] ]
); );
ray($this->saved_outputs->get('commit_message'));
raY($this->commit);
if ($this->saved_outputs->get('commit_message')) { if ($this->saved_outputs->get('commit_message')) {
$commit_message = str($this->saved_outputs->get('commit_message'))->limit(47); $commit_message = str($this->saved_outputs->get('commit_message'))->limit(47);
$this->application_deployment_queue->commit_message = $commit_message->value(); $this->application_deployment_queue->commit_message = $commit_message->value();
@ -1248,7 +1238,7 @@ private function clone_repository()
private function generate_git_import_commands() private function generate_git_import_commands()
{ {
['commands' => $commands, 'branch' => $this->branch, 'full_repository_url' => $this->full_repository_url] = $this->application->generateGitImportCommands( ['commands' => $commands, 'branch' => $this->branch, 'fullRepoUrl' => $this->fullRepoUrl] = $this->application->generateGitImportCommands(
deployment_uuid: $this->deployment_uuid, deployment_uuid: $this->deployment_uuid,
pull_request_id: $this->pull_request_id, pull_request_id: $this->pull_request_id,
git_type: $this->git_type, git_type: $this->git_type,
@ -1755,10 +1745,10 @@ private function build_image()
$this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]); $this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]);
} else { } else {
if ($this->force_rebuild) { if ($this->force_rebuild) {
$build_command = "docker build --no-cache {$this->build_target} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; $build_command = "docker build --no-cache {$this->buildTarget} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
} else { } else {
$build_command = "docker build {$this->build_target} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"; $build_command = "docker build {$this->buildTarget} --network {$this->destination->network} -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
} }
$this->execute_remote_command( $this->execute_remote_command(
@ -1794,7 +1784,7 @@ private function build_image()
} }
}"); }");
} }
$build_command = "docker build {$this->add_hosts} --network host -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $build_command = "docker build {$this->addHosts} --network host -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
$this->execute_remote_command( $this->execute_remote_command(
[ [
@ -1813,7 +1803,7 @@ private function build_image()
} else { } else {
// Pure Dockerfile based deployment // Pure Dockerfile based deployment
if ($this->application->dockerfile) { if ($this->application->dockerfile) {
$build_command = "docker build --pull {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $build_command = "docker build --pull {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
$this->execute_remote_command( $this->execute_remote_command(
[ [
@ -1839,10 +1829,10 @@ private function build_image()
$this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]); $this->execute_remote_command([executeInDocker($this->deployment_uuid, "rm /artifacts/thegameplan.json"), "hidden" => true]);
} else { } else {
if ($this->force_rebuild) { if ($this->force_rebuild) {
$build_command = "docker build --no-cache {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $build_command = "docker build --no-cache {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
} else { } else {
$build_command = "docker build {$this->build_target} {$this->add_hosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}"; $build_command = "docker build {$this->buildTarget} {$this->addHosts} --network host -f {$this->workdir}{$this->dockerfile_location} {$this->build_args} --progress plain -t {$this->production_image_name} {$this->workdir}";
$base64_build_command = base64_encode($build_command); $base64_build_command = base64_encode($build_command);
} }
$this->execute_remote_command( $this->execute_remote_command(
@ -1862,7 +1852,7 @@ private function build_image()
private function stop_running_container(bool $force = false) private function stop_running_container(bool $force = false)
{ {
$this->application_deployment_queue->addLogEntry("Removing old containers."); $this->application_deployment_queue->addLogEntry("Removing old containers.");
if ($this->is_new_version_healthy || $force) { if ($this->newVersionIsHealthy || $force) {
$containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id);
if ($this->pull_request_id === 0) { if ($this->pull_request_id === 0) {
$containers = $containers->filter(function ($container) { $containers = $containers->filter(function ($container) {