} */ 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 `:` 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 */ 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; } }