Merge remote-tracking branch 'origin/main' into next

This commit is contained in:
github-actions[bot]
2026-10-26 13:58:06 +00:00
6 changed files with 433 additions and 8 deletions
+36 -7
View File
@@ -2922,14 +2922,14 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
return;
}
if ($this->application->custom_healthcheck_found) {
$this->application_deployment_queue->addLogEntry('Custom healthcheck found in Dockerfile.');
if ($this->application->isHealthcheckDisabled()) {
$this->application_deployment_queue->addLogEntry('Using the custom healthcheck from the Dockerfile.');
}
if ($this->container_name) {
$escapedContainerName = escapeshellarg($this->container_name);
$counter = 1;
$this->application_deployment_queue->addLogEntry('Waiting for healthcheck to pass on the new container.');
if ($this->full_healthcheck_url && ! $this->application->custom_healthcheck_found) {
if ($this->full_healthcheck_url) {
$healthcheckLabel = $this->application->health_check_type === 'cmd' ? 'Healthcheck command' : 'Healthcheck URL';
$this->application_deployment_queue->addLogEntry("{$healthcheckLabel} (inside the container): {$this->full_healthcheck_url}");
}
@@ -3344,6 +3344,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
}
}
$this->resolve_bitbucket_pull_request_commit();
$this->generate_git_import_commands();
$local_branch = $this->branch;
if ($this->pull_request_id !== 0) {
@@ -3426,6 +3427,35 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
}
/**
* Bitbucket Cloud has no pull request refs over git, so a preview deployment without a webhook
* commit (a manual redeploy) builds the commit of the latest earlier deployment of the pull request.
* Without such a commit the clone checks out the base branch, so the deployment fails instead.
*/
private function resolve_bitbucket_pull_request_commit(): void
{
if ($this->pull_request_id === 0 || $this->git_type !== 'bitbucket' || ! $this->shouldResolveBranchHeadCommit()) {
return;
}
$commit = ApplicationDeploymentQueue::query()
->where('application_id', $this->application->id)
->where('pull_request_id', $this->pull_request_id)
->whereKeyNot($this->application_deployment_queue->id)
->whereNotIn('commit', ['HEAD', ''])
->latest('id')
->value('commit');
if (blank($commit)) {
throw new DeploymentException("Coolify cannot find the commit of Bitbucket pull request #{$this->pull_request_id}, because Bitbucket Cloud has no pull request refs over git. Push a commit to the pull request to deploy it with the webhook, or deploy the preview with a commit SHA through the API.");
}
$this->commit = $commit;
$this->application_deployment_queue->commit = $commit;
$this->application_deployment_queue->save();
$this->application_deployment_queue->addLogEntry("Using commit {$commit} of the latest deployment of Bitbucket pull request #{$this->pull_request_id}.");
}
/**
* The commit message is read from the cloned repository. Deployments that skip the build or fail
* before the clone reuse the message stored by an earlier deployment of the same commit.
@@ -4418,10 +4448,9 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
$docker_compose['services'][$this->container_name]['stop_grace_period'] = $this->application->settings->stopGracePeriodSeconds().'s';
}
// Only add Coolify healthcheck if no custom HEALTHCHECK found in Dockerfile
// If custom_healthcheck_found is true, the Dockerfile's HEALTHCHECK will be used
// If healthcheck is disabled, no healthcheck will be added
if (! $this->application->custom_healthcheck_found && ! $this->application->isHealthcheckDisabled()) {
// An enabled Coolify healthcheck replaces any HEALTHCHECK from the image or Dockerfile.
// When it is disabled, the container keeps the HEALTHCHECK of the image (if any).
if (! $this->application->isHealthcheckDisabled()) {
$healthcheck_command = $this->generate_healthcheck_commands();
if ($healthcheck_command !== null) {
$docker_compose['services'][$this->container_name]['healthcheck'] = [
+2
View File
@@ -298,6 +298,8 @@ class Application extends BaseModel
'container_present' => 'boolean',
'last_restart_at' => 'datetime',
'is_maintenance_enabled' => 'boolean',
'health_check_enabled' => 'boolean',
'custom_healthcheck_found' => 'boolean',
];
}
+16 -1
View File
@@ -211,7 +211,8 @@ class LocalPersistentVolume extends BaseModel
$resourceUuid = $resource instanceof Application ? $resource->uuid : data_get($resource, 'service.uuid');
$generatedName = $source ? $resourceUuid.'_'.Str::slug($source, '-') : null;
if ($generatedName === $this->name && $target && str($target)->start('/')->value() === $this->mount_path) {
if ($generatedName && $target && str($target)->start('/')->value() === $this->mount_path
&& ($generatedName === $this->name || $this->isVolumeOfOpenPreview($resource, $generatedName))) {
return true;
}
}
@@ -223,6 +224,20 @@ class LocalPersistentVolume extends BaseModel
}
}
/**
* Whether this storage entry is the preview copy ("{volume}-pr-{id}") of the given volume for a
* preview that still exists. The preview cleanup removes the entry when the preview is closed.
*/
private function isVolumeOfOpenPreview(mixed $resource, string $generatedName): bool
{
if (! $resource instanceof Application
|| preg_match('/^'.preg_quote($generatedName, '/').'-pr-(\d+)$/', $this->name, $matches) !== 1) {
return false;
}
return $resource->previews()->where('pull_request_id', (int) $matches[1])->exists();
}
/**
* The external Compose volume that this storage entry still replaces, or null. Before Coolify
* used external volumes as written, the parsers gave them a generated name, for example
@@ -0,0 +1,101 @@
<?php
use App\Exceptions\DeploymentException;
use App\Jobs\ApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$team = Team::factory()->create();
$this->server = Server::factory()->create(['team_id' => $team->id]);
$destination = StandaloneDocker::where('server_id', $this->server->id)->firstOrFail();
$project = Project::factory()->create(['team_id' => $team->id]);
$environment = $project->environments()->first() ?? Environment::factory()->create(['project_id' => $project->id]);
$this->application = Application::factory()->create([
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
});
function createPreviewDeployment(Application $application, Server $server, int $pullRequestId, string $commit): ApplicationDeploymentQueue
{
return ApplicationDeploymentQueue::create([
'application_id' => $application->id,
'deployment_uuid' => new_public_id(),
'server_id' => $server->id,
'pull_request_id' => $pullRequestId,
'git_type' => 'bitbucket',
'commit' => $commit,
]);
}
function resolveBitbucketPullRequestCommit(ApplicationDeploymentQueue $deployment, Application $application, string $gitType = 'bitbucket'): ApplicationDeploymentJob
{
$reflection = new ReflectionClass(ApplicationDeploymentJob::class);
$job = $reflection->newInstanceWithoutConstructor();
$reflection->getProperty('application')->setValue($job, $application);
$reflection->getProperty('application_deployment_queue')->setValue($job, $deployment);
$reflection->getProperty('pull_request_id')->setValue($job, $deployment->pull_request_id);
$reflection->getProperty('git_type')->setValue($job, $gitType);
$reflection->getProperty('commit')->setValue($job, $deployment->commit);
$reflection->getMethod('resolve_bitbucket_pull_request_commit')->invoke($job);
return $job;
}
function deploymentJobCommit(ApplicationDeploymentJob $job): string
{
return (new ReflectionProperty(ApplicationDeploymentJob::class, 'commit'))->getValue($job);
}
test('a manual bitbucket preview redeploy builds the commit of the latest deployment of the pull request', function () {
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
createPreviewDeployment($this->application, $this->server, 7, str_repeat('b', 40));
createPreviewDeployment($this->application, $this->server, 8, str_repeat('c', 40));
createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
$job = resolveBitbucketPullRequestCommit($deployment, $this->application);
expect(deploymentJobCommit($job))->toBe(str_repeat('b', 40))
->and($deployment->fresh()->commit)->toBe(str_repeat('b', 40));
});
test('a manual bitbucket preview redeploy fails when no commit of the pull request is known', function () {
createPreviewDeployment($this->application, $this->server, 8, str_repeat('c', 40));
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
expect(fn () => resolveBitbucketPullRequestCommit($deployment, $this->application))
->toThrow(DeploymentException::class, 'Bitbucket pull request #7');
expect($deployment->fresh()->commit)->toBe('HEAD');
});
test('a bitbucket preview deployment keeps the commit from the webhook', function () {
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
$deployment = createPreviewDeployment($this->application, $this->server, 7, str_repeat('d', 40));
$job = resolveBitbucketPullRequestCommit($deployment, $this->application);
expect(deploymentJobCommit($job))->toBe(str_repeat('d', 40));
});
test('other git providers resolve the pull request ref over git', function () {
createPreviewDeployment($this->application, $this->server, 7, str_repeat('a', 40));
$deployment = createPreviewDeployment($this->application, $this->server, 7, 'HEAD');
$job = resolveBitbucketPullRequestCommit($deployment, $this->application, 'github');
expect(deploymentJobCommit($job))->toBe('HEAD');
});
+162
View File
@@ -0,0 +1,162 @@
<?php
use App\Livewire\Project\Shared\Storages\All;
use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Models\Environment;
use App\Models\InstanceSettings;
use App\Models\LocalPersistentVolume;
use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
uses(RefreshDatabase::class);
const STALE_VOLUME_THREE_VOLUMES_COMPOSE = <<<'YAML'
services:
web:
image: nginx:alpine
volumes:
- 'first:/first'
- 'second:/second'
- 'third:/third'
YAML;
const STALE_VOLUME_TWO_VOLUMES_COMPOSE = <<<'YAML'
services:
web:
image: nginx:alpine
volumes:
- 'first:/first'
- 'second:/second'
YAML;
beforeEach(function () {
Server::flushIdentityMap();
InstanceSettings::unguarded(fn () => InstanceSettings::firstOrCreate(['id' => 0]));
config(['constants.ssh.mux_enabled' => false]);
Bus::fake();
Queue::fake();
Process::fake(['*' => Process::result(output: '')]);
$this->withoutVite();
$team = Team::factory()->create();
$privateKey = PrivateKey::factory()->create(['team_id' => $team->id]);
$server = Server::factory()->create([
'team_id' => $team->id,
'private_key_id' => $privateKey->id,
]);
$server->settings()->update(['is_reachable' => true, 'is_usable' => true]);
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
$project = Project::factory()->create(['team_id' => $team->id]);
$environment = $project->environments()->first()
?? Environment::factory()->create(['project_id' => $project->id]);
$user = User::factory()->create();
$team->members()->attach($user->id, ['role' => 'owner']);
$this->actingAs($user);
session(['currentTeam' => $team]);
$this->application = Application::factory()->create([
'build_pack' => 'dockercompose',
'docker_compose_raw' => STALE_VOLUME_THREE_VOLUMES_COMPOSE,
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
])->fresh();
applicationParser($this->application);
$this->application->update(['docker_compose_raw' => STALE_VOLUME_TWO_VOLUMES_COMPOSE]);
applicationParser($this->application->fresh());
});
function staleVolume(string $volume): LocalPersistentVolume
{
return LocalPersistentVolume::where('name', test()->application->uuid.'_'.$volume)->sole();
}
it('offers to delete only the volume that the Compose file no longer declares', function () {
$forms = Livewire::test(All::class, ['resource' => $this->application->fresh()])->get('forms');
expect($forms[staleVolume('third')->id]['canDeleteStale'])->toBeTrue()
->and($forms[staleVolume('first')->id]['canDeleteStale'])->toBeFalse()
->and($forms[staleVolume('second')->id]['canDeleteStale'])->toBeFalse();
});
it('deletes the entry of a volume that the Compose file no longer declares', function (bool $deleteDockerVolume) {
$name = staleVolume('third')->name;
$commands = [];
Process::fake(function ($process) use (&$commands) {
$commands[] = is_array($process->command) ? implode(' ', $process->command) : $process->command;
return Process::result(output: '');
});
Livewire::test(All::class, ['resource' => $this->application->fresh()])
->call('delete', staleVolume('third')->id, 'password', $deleteDockerVolume ? ['deleteDockerVolume'] : [])
->assertReturned(true);
expect(LocalPersistentVolume::where('name', $name)->exists())->toBeFalse()
->and(implode("\n", $commands))->{$deleteDockerVolume ? 'toContain' : 'not'}("docker volume rm -f '{$name}'");
applicationParser($this->application->fresh());
expect(LocalPersistentVolume::where('name', $name)->exists())->toBeFalse();
})->with([
'keep the Docker volume' => [false],
'delete the Docker volume' => [true],
]);
it('does not delete the entry of a volume that the Compose file declares', function () {
$volume = staleVolume('first');
Livewire::test(All::class, ['resource' => $this->application->fresh()])
->call('delete', $volume->id, 'password')
->assertReturned(false);
expect(LocalPersistentVolume::find($volume->id))->not->toBeNull();
});
it('does not offer to delete the preview volume of a volume that the Compose file declares', function () {
ApplicationPreview::create([
'uuid' => 'stale-volume-preview',
'application_id' => $this->application->id,
'pull_request_id' => 42,
'pull_request_html_url' => 'https://github.com/example/repository/pull/42',
]);
applicationParser($this->application->fresh(), 42);
$previewVolume = staleVolume('first-pr-42');
Livewire::test(All::class, ['resource' => $this->application->fresh()])
->assertSet("forms.{$previewVolume->id}.canDeleteStale", false)
->call('delete', $previewVolume->id, 'password')
->assertReturned(false);
expect(LocalPersistentVolume::find($previewVolume->id))->not->toBeNull();
});
it('offers to delete the left-over volume of a closed preview', function () {
$preview = ApplicationPreview::create([
'uuid' => 'stale-volume-closed-preview',
'application_id' => $this->application->id,
'pull_request_id' => 7,
'pull_request_html_url' => 'https://github.com/example/repository/pull/7',
]);
applicationParser($this->application->fresh(), 7);
$previewVolume = staleVolume('first-pr-7');
$preview->deleteQuietly();
Livewire::test(All::class, ['resource' => $this->application->fresh()])
->assertSet("forms.{$previewVolume->id}.canDeleteStale", true)
->call('delete', $previewVolume->id, 'password')
->assertReturned(true);
expect(LocalPersistentVolume::find($previewVolume->id))->toBeNull();
});
@@ -0,0 +1,116 @@
<?php
use App\Jobs\ApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
use Symfony\Component\Yaml\Yaml;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
class TestableDockerfileHealthcheckDeploymentJob extends ApplicationDeploymentJob
{
public function __construct() {}
public function execute_remote_command(...$commands): void {}
}
function generateComposeServiceWithDockerfileHealthcheck(array $applicationAttributes): array
{
$team = Team::create([
'name' => 'Dockerfile Healthcheck Team',
'personal_team' => false,
'show_boarding' => false,
]);
$project = Project::create([
'name' => 'Dockerfile Healthcheck Project',
'team_id' => $team->id,
]);
$environment = Environment::where('project_id', $project->id)->firstOrFail();
$server = Server::factory()->create(['team_id' => $team->id]);
$destination = $server->standaloneDockers()->firstOrFail();
$application = Application::factory()->create([
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => StandaloneDocker::class,
'build_pack' => 'dockerfile',
'ports_exposes' => '4181',
'custom_healthcheck_found' => true,
...$applicationAttributes,
]);
$queue = Mockery::mock(ApplicationDeploymentQueue::class)->makePartial();
$queue->status = 'queued';
$queue->shouldReceive('refresh')->once()->andReturnSelf();
$queue->shouldReceive('addLogEntry');
$job = new TestableDockerfileHealthcheckDeploymentJob;
$reflection = new ReflectionClass(ApplicationDeploymentJob::class);
foreach ([
'application' => $application->fresh(),
'application_deployment_queue' => $queue,
'destination' => $destination,
'server' => $server,
'mainServer' => $server,
'pull_request_id' => 0,
'container_name' => 'dockerfile-healthcheck-app',
'production_image_name' => 'example/app:latest',
'deployment_uuid' => 'deployment-uuid',
'workdir' => '/artifacts/dockerfile-healthcheck-app',
'configuration_dir' => '/data/coolify/applications/test',
'dockerfile_location' => '/Dockerfile',
'saved_outputs' => new Collection([
'dockerfile_from_repo' => "FROM scratch\nHEALTHCHECK --interval=30s --timeout=3s CMD [\"/app\", \"healthcheck\"]\n",
]),
] as $property => $value) {
$reflection->getProperty($property)->setValue($job, $value);
}
$reflection->getMethod('generate_compose_file')->invoke($job);
$compose = Yaml::parse($reflection->getProperty('docker_compose')->getValue($job));
return $compose['services']['dockerfile-healthcheck-app'];
}
it('replaces the Dockerfile HEALTHCHECK with the enabled HTTP healthcheck', function () {
$service = generateComposeServiceWithDockerfileHealthcheck([
'health_check_enabled' => true,
'health_check_type' => 'http',
'health_check_port' => '4181',
'health_check_path' => '/health',
'health_check_interval' => 300,
'health_check_timeout' => 3,
]);
expect($service['healthcheck']['interval'])->toBe('300s')
->and($service['healthcheck']['timeout'])->toBe('3s')
->and($service['healthcheck']['test'][1])->toContain("'http://localhost:4181/health'");
});
it('replaces the Dockerfile HEALTHCHECK with the enabled command healthcheck', function () {
$service = generateComposeServiceWithDockerfileHealthcheck([
'health_check_enabled' => true,
'health_check_type' => 'cmd',
'health_check_command' => '/app healthcheck',
'health_check_interval' => 300,
]);
expect($service['healthcheck']['test'])->toBe(['CMD-SHELL', '/app healthcheck'])
->and($service['healthcheck']['interval'])->toBe('300s');
});
it('keeps the Dockerfile HEALTHCHECK when the Coolify healthcheck is disabled', function () {
$service = generateComposeServiceWithDockerfileHealthcheck([
'health_check_enabled' => false,
]);
expect($service)->not->toHaveKey('healthcheck');
});