diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index c9f0f1eef..ccffbc873 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -2520,7 +2520,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue return escapeDollarSign($value); }); } - $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->application->project()->name, $this->application->name, $this->application->environment->name, $this->pull_request_id))->toArray(); + $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->application->project()->name, $this->application->name, $this->application->environment->name, $this->pull_request_id, tags: $this->application->tags))->toArray(); // Check for custom HEALTHCHECK if ($this->application->build_pack === 'dockerfile' || $this->application->dockerfile) { diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 7b74392cf..117ab2570 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -210,6 +210,23 @@ function get_port_from_dockerfile($dockerfile): ?int return null; } +function formatTagsForDockerLabel($tags, int $limit = 50): string +{ + if (! $tags || $tags->isEmpty()) { + return ''; + } + + return $tags->take($limit) + ->pluck('name') + ->map(function ($tag) { + // Sanitize to only allow characters safe for Docker labels + // Allows: alphanumeric, hyphens, underscores, periods, colons, forward slashes + return preg_replace('/[^a-zA-Z0-9\-_.:\/]/', '', $tag); + }) + ->filter() // Remove empty strings after sanitization + ->implode(' '); +} + function defaultDatabaseLabels($database) { $labels = collect([]); @@ -222,10 +239,15 @@ function defaultDatabaseLabels($database) $labels->push('coolify.environmentName='.Str::slug($database->environment->name)); $labels->push('coolify.database.subType='.$database->type()); + $tagsLabel = formatTagsForDockerLabel($database->tags ?? collect()); + if ($tagsLabel) { + $labels->push('coolify.tags='.$tagsLabel); + } + return $labels; } -function defaultLabels($id, $name, string $projectName, string $resourceName, string $environment, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null, $subName = null) +function defaultLabels($id, $name, string $projectName, string $resourceName, string $environment, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null, $subName = null, $tags = null) { $labels = collect([]); $labels->push('coolify.managed=true'); @@ -245,6 +267,13 @@ function defaultLabels($id, $name, string $projectName, string $resourceName, st $subName && $labels->push('coolify.service.subName='.Str::slug($subName)); } + if ($tags) { + $tagsLabel = formatTagsForDockerLabel($tags); + if ($tagsLabel) { + $labels->push('coolify.tags='.$tagsLabel); + } + } + return $labels; } diff --git a/bootstrap/helpers/parsers.php b/bootstrap/helpers/parsers.php index fa40857ac..2bd5ca332 100644 --- a/bootstrap/helpers/parsers.php +++ b/bootstrap/helpers/parsers.php @@ -1254,6 +1254,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int pull_request_id: $pullRequestId, type: 'application', environment: $resource->environment->name, + tags: $resource->tags, ); $isDatabase = isDatabaseImage($image, $service); @@ -2499,6 +2500,7 @@ function serviceParser(Service $resource): Collection subId: $savedService->id, subName: $savedService->human_name ?? $savedService->name, environment: $resource->environment->name, + tags: $resource->tags, ); // Add COOLIFY_FQDN & COOLIFY_URL to environment diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 3e993dbf3..9eb59ae67 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -2283,6 +2283,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal subId: $savedService->id, subName: $savedService->name, environment: $resource->environment->name, + tags: $resource->tags, ); $serviceLabels = $serviceLabels->merge($defaultLabels); if (! $isDatabase && $fqdns->count() > 0) { @@ -3130,7 +3131,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal resourceName: $resource->name, environment: $resource->environment->name, pull_request_id: $pull_request_id, - type: 'application' + type: 'application', + tags: $resource->tags, ); $serviceLabels = $serviceLabels->merge($defaultLabels);