Files
coolify/app/Actions/Application/GetRollbackImages.php
T
Andras Bacsai 3d8cea9e3c feat(rollback): list Docker registry images of recent deployments
Rollback now also lists the commit images of the last 10 successful
deployments when the application pushes to a Docker registry. The
deployment pulls such an image when the server no longer has it.
The page and the API share GetRollbackImages, and the API returns a
source field (local or registry).
2026-10-19 10:10:48 +02:00

124 lines
4.5 KiB
PHP

<?php
namespace App\Actions\Application;
use App\Enums\ApplicationDeploymentStatus;
use App\Models\Application;
use Lorisleiva\Actions\Concerns\AsAction;
/**
* Lists the images an application can roll back to: the images on its server, and the images of
* recent successful deployments that were pushed to the Docker registry. The deployment pulls a
* registry image again when the server no longer has it.
*/
class GetRollbackImages
{
use AsAction;
public const REGISTRY_DEPLOYMENTS_LIMIT = 10;
/**
* @return array{current: ?string, images: list<array{tag: string, created_at: ?string, is_current: bool, source: 'local'|'registry'}>}
*/
public function handle(Application $application): array
{
$current = null;
$images = collect();
$server = $application->destination?->server;
if ($server?->isFunctional()) {
$output = instant_remote_process([
"docker inspect --format='{{.Config.Image}}' {$application->uuid}",
], $server, throwError: false);
$current = self::imageTag(str($output)->trim()->toString());
$repository = $application->docker_registry_image_name ?: $application->uuid;
$output = instant_remote_process([
"docker images --format '{{.Repository}}#{{.Tag}}#{{.CreatedAt}}'",
], $server);
$images = str($output)->trim()->explode("\n")
->map(fn (string $line) => explode('#', $line))
// Exact repository match only, so other images with a similar name are not listed.
->filter(fn (array $parts) => $parts[0] === $repository && filled($parts[1] ?? null))
->map(fn (array $parts) => [
'tag' => $parts[1],
'created_at' => $parts[2] ?? null,
'is_current' => $parts[1] === $current,
'source' => 'local',
])
->keyBy('tag');
}
foreach ($this->registryImages($application, $current) as $image) {
if (! $images->has($image['tag'])) {
$images->put($image['tag'], $image);
}
}
return [
'current' => $current,
'images' => $images->values()->all(),
];
}
/**
* Each successful deployment pushes `<image name>:<commit>` to the registry, so the commits of
* recent deployments are rollback targets. Docker image applications do not push, and
* Dockerfile-only applications always push the `latest` tag.
*
* @return list<array{tag: string, created_at: ?string, is_current: bool, source: 'registry'}>
*/
private function registryImages(Application $application, ?string $current): array
{
if (blank($application->docker_registry_image_name)
|| in_array($application->build_pack, ['dockerimage', 'dockercompose'], true)
|| filled($application->dockerfile)) {
return [];
}
return $application->deployment_queue()
->where('status', ApplicationDeploymentStatus::FINISHED->value)
->where('pull_request_id', 0)
->whereNotNull('commit')
->where('commit', '!=', 'HEAD')
->select('commit')
->selectRaw('max(updated_at) as deployed_at')
->groupBy('commit')
->orderByDesc('deployed_at')
->limit(self::REGISTRY_DEPLOYMENTS_LIMIT)
->get()
->map(function ($deployment) use ($current) {
// The same tag as the deployment job builds in generate_image_names().
$tag = substr($deployment->commit, 0, 128);
return [
'tag' => $tag,
'created_at' => $deployment->deployed_at,
'is_current' => $tag === $current,
'source' => 'registry',
];
})
->values()
->all();
}
/**
* The tag of an image reference such as `registry.example.com:5000/team/app:tag`, or null for a digest.
*/
public static function imageTag(string $imageReference): ?string
{
if (str_contains($imageReference, '@')) {
return null;
}
$lastColon = strrpos($imageReference, ':');
$lastSlash = strrpos($imageReference, '/');
if ($lastColon === false || ($lastSlash !== false && $lastColon < $lastSlash)) {
return null;
}
return substr($imageReference, $lastColon + 1) ?: null;
}
}