diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 580fa45602..f769930e72 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -196,18 +196,11 @@ return function (Container $context): void { $publisher, new Queue(System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME), 'utopia-queue', Event::FUNCTIONS_QUEUE_TTL) ), ['publisher']); - $context->set('deployments', function (BuildPublisher $publisherForBuilds, Jobs $jobs, Database $dbForProject, Document $project, Executor $executor, array $platform, Request $request) { - // The jobs-service orchestrator backend only understands functions - // (its payload builder reads function-shaped attributes like - // `runtime`); sites always build on the executor. Transitional: - // once sites build on the jobs-service too, this URI check and the - // nullable Backend params in the VCS trait can both go. - $isSite = \str_starts_with($request->getURI(), '/v1/sites'); - - return !$isSite && System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator' + $context->set('deployments', function (BuildPublisher $publisherForBuilds, Jobs $jobs, Database $dbForProject, Document $project, Executor $executor, array $platform) { + return System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator' ? new Orchestrator($jobs, $dbForProject, $project, $platform) : new ExecutorBackend($publisherForBuilds, $dbForProject, $project, $executor, $platform); - }, ['publisherForBuilds', 'jobs', 'dbForProject', 'project', 'executor', 'platform', 'request']); + }, ['publisherForBuilds', 'jobs', 'dbForProject', 'project', 'executor', 'platform']); $context->set('eventProcessor', fn () => new EventProcessor(), []); $context->set('databaseFactory', fn (Group $pools, Cache $cache, Authorization $authorization) => new DatabaseFactory( $pools, diff --git a/app/init/worker/message.php b/app/init/worker/message.php index bf2d2f6cb1..d581820549 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -188,10 +188,8 @@ return function (Container $container): void { return new TelemetryDevice($telemetry, getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId())); }, ['project', 'telemetry']); - // Workers only ever build functions on the jobs-service backend (sites - // always build on the executor via the Builds worker directly), so - // unlike the HTTP-side 'deployments' resource this doesn't need to pick - // a backend per-request. + // Only the Builds worker uses this, handing template-into-repo pushes to + // the jobs-service when _APP_BUILDS_BACKEND=orchestrator — no backend switch. $container->set('deployments', function (Jobs $jobs, Database $dbForProject, Document $project, array $platform) { return new Orchestrator($jobs, $dbForProject, $project, $platform); }, ['jobs', 'dbForProject', 'project', 'platform']); diff --git a/src/Appwrite/Deployment/Backend.php b/src/Appwrite/Deployment/Backend.php index b68d8f0276..a75294480b 100644 --- a/src/Appwrite/Deployment/Backend.php +++ b/src/Appwrite/Deployment/Backend.php @@ -2,6 +2,7 @@ namespace Appwrite\Deployment; +use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\Permission; @@ -122,6 +123,26 @@ abstract readonly class Backend } } + /** + * The build command for a deployment: its buildCommands, wrapped for + * sites with the framework's env and bundle commands. + */ + public static function command(Document $resource, Document $deployment): string + { + $command = $deployment->getAttribute('buildCommands', ''); + if ($resource->getCollection() !== 'sites') { + return $command; + } + + $framework = Config::getParam('frameworks', [])[$resource->getAttribute('framework', '')] ?? []; + + return \implode(' && ', \array_filter([ + $framework['envCommand'] ?? '', + $command, + $framework['bundleCommand'] ?? '', + ], fn ($command) => !empty($command))); + } + /** * @return array */ diff --git a/src/Appwrite/Deployment/Backend/Orchestrator.php b/src/Appwrite/Deployment/Backend/Orchestrator.php index 9d029d3fab..da51c51ff9 100644 --- a/src/Appwrite/Deployment/Backend/Orchestrator.php +++ b/src/Appwrite/Deployment/Backend/Orchestrator.php @@ -8,6 +8,7 @@ use Appwrite\Deployment\Token; use OpenRuntimes\Orchestrator\Enum\CallbackEvent; use OpenRuntimes\Orchestrator\Jobs; use OpenRuntimes\Orchestrator\Model\Artifact\DownloadArtifact; +use OpenRuntimes\Orchestrator\Model\Artifact\ReadArtifact; use OpenRuntimes\Orchestrator\Model\Artifact\StatArtifact; use OpenRuntimes\Orchestrator\Model\Artifact\UnarchiveArtifact; use OpenRuntimes\Orchestrator\Model\Callback; @@ -31,9 +32,10 @@ use Utopia\System\System; * upload/download artifacts instead of a shared volume) override storage() * — everything else about the payload stays the same. * - * Covers function deployments whose source is a tarball: manual upload, - * duplicate/rebuild, and templates (public GitHub tarball resolved from a - * git reference). + * Covers function and site deployments whose source is a tarball: manual + * upload, duplicate/rebuild, and templates (public GitHub tarball resolved + * from a git reference). Site builds also emit a JSON build manifest, read + * back post-job as an artifact callback for adapter detection. */ readonly class Orchestrator extends Backend { @@ -107,20 +109,26 @@ readonly class Orchestrator extends Backend */ protected static function payload( Document $project, - Document $function, + Document $resource, Document $deployment, array $platform, ?array $source = null, ): array { $projectId = $project->getId(); $deploymentId = $deployment->getId(); + $isSite = $resource->getCollection() === 'sites'; $timeout = (int) System::getEnv('_APP_COMPUTE_BUILD_TIMEOUT', 900); - $version = $function->getAttribute('version', 'v2'); - $runtime = self::runtime($function, $version); - $spec = Config::getParam('specifications')[$function->getAttribute('buildSpecification', APP_COMPUTE_SPECIFICATION_DEFAULT)]; + $runtime = self::runtime($resource, self::version($resource)); + $spec = Config::getParam('specifications')[$resource->getAttribute('buildSpecification', APP_COMPUTE_SPECIFICATION_DEFAULT)]; $cpus = (float) ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT); - $memory = \max((int) ($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT), 1024); + + // Some runtimes/frameworks can't compile with less memory than this. + $minMemory = $isSite ? 2048 : 1024; + if (\in_array($resource->getAttribute('framework', ''), ['analog', 'tanstack-start'], true)) { + $minMemory = 4096; + } + $memory = \max((int) ($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT), $minMemory); // The jobs-service (and the containers it spawns) reach Appwrite over // the internal Docker network, so the presigned + callback URLs use an @@ -136,7 +144,13 @@ readonly class Orchestrator extends Backend // - otherwise: the deployment's uploaded tarball, fetched from Appwrite // over a presigned GET (manual upload / duplicate). if ($source !== null) { + // Normalize a "./"-style root directory — the unarchive matches + // subdir against literal tarball paths, so "./x" would match nothing. $subdir = \trim($source['subdir'] ?? '', '/'); + if (\str_starts_with($subdir, './')) { + $subdir = \substr($subdir, 2); + } + $subdir = $subdir === '.' ? '' : $subdir; $sourceArtifacts = [ new DownloadArtifact(id: 'source', in: $source['url'], out: 'source.tar.gz'), new UnarchiveArtifact(id: 'extract', in: 'source.tar.gz', out: 'source', subdir: $subdir !== '' ? $subdir : null), @@ -151,7 +165,7 @@ readonly class Orchestrator extends Backend // the sidecar. Bound to this deployment + direction; valid for the whole // build window plus transfer slack. $ttl = $timeout + 300; - $base = "{$endpoint}/v1/functions/{$function->getId()}/deployments/{$deploymentId}"; + $base = "{$endpoint}/v1/{$resource->getCollection()}/{$resource->getId()}/deployments/{$deploymentId}"; $sourceUrl = "{$base}/download?" . \http_build_query([ 'type' => Token::TYPE_SOURCE, 'project' => $projectId, @@ -166,13 +180,26 @@ readonly class Orchestrator extends Backend // Where output + cache land is a swappable strategy (see storage()) — // the default mounts the shared builds volume; nothing else here cares // which strategy is active. - $output = static::storage($project, $function, $deployment); + $output = static::storage($project, $resource, $deployment); - $command = $deployment->getAttribute('buildCommands', ''); - $env = self::variables($project, $function, $deployment, $runtime, $cpus, $memory, $endpoint, $timeout) + [ + // Site builds write a JSON build manifest into the workspace, read + // back post-job so the Jobs worker can run adapter detection. + $manifestArtifacts = $isSite ? [new ReadArtifact(id: 'manifest', in: 'manifest.json', depends: 'job')] : []; + + $command = self::command($resource, $deployment); + $env = self::variables($project, $resource, $deployment, $runtime, $cpus, $memory, $endpoint, $timeout) + [ 'OPEN_RUNTIMES_BUILD_INPUT_DIR' => '/mnt/code/source', 'OPEN_RUNTIMES_BUILD_COMPRESSION' => static::compression(), - ] + $output['environment']; + ] + ($isSite ? ['OPEN_RUNTIMES_BUILD_MANIFEST' => '/mnt/code/manifest.json'] : []) + $output['environment']; + + // Two terminal callbacks: exit carries the code (fires before + // post-job artifacts), complete confirms artifact delivery — the + // worker joins them, so readiness holds on any storage strategy. + // Artifact callbacks carry the source-size stat and the site manifest. + $events = [CallbackEvent::Log, CallbackEvent::Exit, CallbackEvent::Complete]; + if ($source !== null || $isSite) { + $events[] = CallbackEvent::Artifact; + } return [ 'id' => static::id($projectId, $deploymentId), @@ -185,24 +212,16 @@ readonly class Orchestrator extends Backend 'meta' => [ 'projectId' => $projectId, 'deploymentId' => $deploymentId, - 'resourceId' => $function->getId(), - 'resourceType' => 'functions', + 'resourceId' => $resource->getId(), + 'resourceType' => $resource->getCollection(), ], // The orchestrator expects environment as a string->string map. 'environment' => \array_map('strval', $env), - 'artifacts' => [...$sourceArtifacts, ...$output['artifacts']], + 'artifacts' => [...$sourceArtifacts, ...$manifestArtifacts, ...$output['artifacts']], 'volumes' => $output['volumes'], 'callback' => new Callback( url: "{$endpoint}/v1/jobs/event?" . \http_build_query(['project' => $projectId]), - // Two terminal callbacks: exit carries the code (fires on - // command exit, before post-job artifacts), complete confirms - // artifact delivery (carries only jobId + meta) — the worker - // joins them, so readiness holds on any storage strategy. - // Artifact callbacks only carry the source-size stat, which - // exists only for remote-source builds (templates / VCS). - events: $source !== null - ? [CallbackEvent::Log, CallbackEvent::Artifact, CallbackEvent::Exit, CallbackEvent::Complete] - : [CallbackEvent::Log, CallbackEvent::Exit, CallbackEvent::Complete], + events: $events, key: System::getEnv('_APP_JOBS_SECRET', ''), ), ]; @@ -251,12 +270,12 @@ readonly class Orchestrator extends Backend } /** - * Deterministic build-cache key, shared across a function's deployments so + * Deterministic build-cache key, shared across a resource's deployments so * package-manager caches (npm/yarn/pnpm) survive between builds. */ - public static function cacheKey(string $projectId, string $functionId, string $image): string + public static function cacheKey(string $projectId, string $resourceId, string $image): string { - return \substr(\hash('sha256', "{$projectId}:{$functionId}:{$image}"), 0, 48); + return \substr(\hash('sha256', "{$projectId}:{$resourceId}:{$image}"), 0, 48); } public static function cachePath(string $projectId, string $cacheKey): string @@ -284,7 +303,7 @@ readonly class Orchestrator extends Backend { $projectId = $project->getId(); $deploymentId = $deployment->getId(); - $runtime = self::runtime($resource, $resource->getAttribute('version', 'v2')); + $runtime = self::runtime($resource, self::version($resource)); $cacheKey = static::cacheKey($projectId, $resource->getId(), $runtime['image'] ?? ''); return [ @@ -302,12 +321,17 @@ readonly class Orchestrator extends Backend ]; } - protected static function runtime(Document $function, string $version): array + protected static function version(Document $resource): string { - $runtimes = Config::getParam($version === 'v2' ? 'runtimes-v2' : 'runtimes', []); - $runtime = $runtimes[$function->getAttribute('runtime')] ?? null; + return $resource->getCollection() === 'sites' ? 'v5' : $resource->getAttribute('version', 'v2'); + } + + protected static function runtime(Document $resource, string $version): array + { + $key = $resource->getAttribute($resource->getCollection() === 'sites' ? 'buildRuntime' : 'runtime'); + $runtime = Config::getParam($version === 'v2' ? 'runtimes-v2' : 'runtimes', [])[$key] ?? null; if ($runtime === null) { - throw new \Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported'); + throw new \Exception('Runtime "' . $key . '" is not supported'); } return $runtime; @@ -315,7 +339,7 @@ readonly class Orchestrator extends Backend private static function variables( Document $project, - Document $function, + Document $resource, Document $deployment, array $runtime, float $cpus, @@ -325,35 +349,37 @@ readonly class Orchestrator extends Backend ): array { $vars = []; - foreach ($function->getAttribute('varsProject', []) as $var) { + foreach ($resource->getAttribute('varsProject', []) as $var) { $vars[$var->getAttribute('key')] = $var->getAttribute('value', ''); } - foreach ($function->getAttribute('vars', []) as $var) { + foreach ($resource->getAttribute('vars', []) as $var) { $vars[$var->getAttribute('key')] = $var->getAttribute('value', ''); } $apiKey = (new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $timeout, 0))->encode([ 'projectId' => $project->getId(), - 'scopes' => $function->getAttribute('scopes', []), + 'scopes' => $resource->getAttribute('scopes', []), ]); + $prefix = $resource->getCollection() === 'sites' ? 'SITE' : 'FUNCTION'; + return \array_merge($vars, [ // Consumed by the open-runtimes build helper (build.sh). 'OPEN_RUNTIMES_ENTRYPOINT' => $deployment->getAttribute('entrypoint', ''), - 'OPEN_RUNTIMES_OUTPUT_DIRECTORY' => $deployment->getAttribute('buildOutput', '') ?: $function->getAttribute('outputDirectory', ''), + 'OPEN_RUNTIMES_OUTPUT_DIRECTORY' => $deployment->getAttribute('buildOutput', '') ?: $resource->getAttribute('outputDirectory', ''), 'APPWRITE_VERSION' => APP_VERSION_STABLE, 'APPWRITE_REGION' => $project->getAttribute('region'), 'APPWRITE_DEPLOYMENT_TYPE' => $deployment->getAttribute('type', ''), - 'APPWRITE_FUNCTION_API_ENDPOINT' => "{$endpoint}/v1", - 'APPWRITE_FUNCTION_API_KEY' => API_KEY_EPHEMERAL . '_' . $apiKey, - 'APPWRITE_FUNCTION_ID' => $function->getId(), - 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name'), - 'APPWRITE_FUNCTION_DEPLOYMENT' => $deployment->getId(), - 'APPWRITE_FUNCTION_PROJECT_ID' => $project->getId(), - 'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'] ?? '', - 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'] ?? '', - 'APPWRITE_FUNCTION_CPUS' => $cpus, - 'APPWRITE_FUNCTION_MEMORY' => $memory, + "APPWRITE_{$prefix}_API_ENDPOINT" => "{$endpoint}/v1", + "APPWRITE_{$prefix}_API_KEY" => API_KEY_EPHEMERAL . '_' . $apiKey, + "APPWRITE_{$prefix}_ID" => $resource->getId(), + "APPWRITE_{$prefix}_NAME" => $resource->getAttribute('name'), + "APPWRITE_{$prefix}_DEPLOYMENT" => $deployment->getId(), + "APPWRITE_{$prefix}_PROJECT_ID" => $project->getId(), + "APPWRITE_{$prefix}_RUNTIME_NAME" => $runtime['name'] ?? '', + "APPWRITE_{$prefix}_RUNTIME_VERSION" => $runtime['version'] ?? '', + "APPWRITE_{$prefix}_CPUS" => $cpus, + "APPWRITE_{$prefix}_MEMORY" => $memory, 'OPEN_RUNTIMES_NFT' => System::getEnv('_APP_OPEN_RUNTIMES_NFT', 'enabled'), ]); } diff --git a/src/Appwrite/Deployment/Detection.php b/src/Appwrite/Deployment/Detection.php new file mode 100644 index 0000000000..b34f30fa53 --- /dev/null +++ b/src/Appwrite/Deployment/Detection.php @@ -0,0 +1,34 @@ + $files + */ + public static function rendering(string $framework, array $files): object + { + $files = \array_map(\trim(...), $files); + $files = \array_filter($files); + $files = \array_map(fn ($file) => \str_starts_with($file, './') ? \substr($file, 2) : $file, $files); + + $detector = new Rendering($framework); + foreach ($files as $file) { + $detector->addInput($file); + } + + return $detector + ->addOption(new SSR()) + ->addOption(new XStatic()) + ->detect(); + } +} diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 60591096ce..cbe2de433f 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -68,7 +68,7 @@ class Base extends Action return $allowedSpecifications[0]; } - public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, array $platform = [], string $referenceType = 'branch', string $reference = '', ?Backend $deployments = null): Document + public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, Backend $deployments, array $platform = [], string $referenceType = 'branch', string $reference = ''): Document { $deploymentId = ID::unique(); $entrypoint = $function->getAttribute('entrypoint', ''); @@ -144,13 +144,12 @@ class Base extends Action 'activate' => $activate, ]); - // Build a plain (non-template) VCS function deployment through - // $deployments (executor or jobs-service, decided by - // _APP_BUILDS_BACKEND) when the caller opts in. Sites stay on the - // executor; template-into-repo pushes go through the Builds worker - // (which does the git write, then hands the build to the - // jobs-service itself when on orchestrator). - if ($deployments !== null && $function->getCollection() === 'functions' && $template->isEmpty()) { + // Build a plain (non-template) VCS deployment through $deployments + // (executor or jobs-service, decided by _APP_BUILDS_BACKEND). + // Template-into-repo pushes go through the Builds worker (which does + // the git write, then hands the build to the jobs-service itself + // when on orchestrator). + if ($template->isEmpty()) { $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); $deployment = $deployments->createFromUrl( $function, @@ -182,7 +181,7 @@ class Base extends Action return $deployment; } - public function redeployVcsSite(Request $request, Document $site, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, Authorization $authorization, array $platform, string $referenceType = 'branch', string $reference = ''): Document + public function redeployVcsSite(Request $request, Document $site, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, Authorization $authorization, Backend $deployments, array $platform, string $referenceType = 'branch', string $reference = ''): Document { $deploymentId = ID::unique(); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); @@ -369,18 +368,91 @@ class Base extends Action $this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform, $authorization); - $publisherForBuilds->enqueue(new BuildMessage( - project: $project, - resource: $site, - deployment: $deployment, - type: BUILD_TYPE_DEPLOYMENT, - template: $template, - platform: $platform, - )); + // Plain VCS deployments build through $deployments; template-into-repo + // pushes go through the Builds worker (same split as redeployVcsFunction). + if ($template->isEmpty()) { + $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + $deployment = $deployments->createFromUrl( + $site, + $deployment, + $vcs->getRepositoryPresignedUrl($owner, $repositoryName, $ref), + $site->getAttribute('providerRootDirectory', ''), + ); + } else { + $publisherForBuilds->enqueue(new BuildMessage( + project: $project, + resource: $site, + deployment: $deployment, + type: BUILD_TYPE_DEPLOYMENT, + template: $template, + platform: $platform, + )); + } return $deployment; } + /** + * Create or repoint the site's branch-preview rule — and any manual rules + * pinned to the branch — at a successfully built deployment. + */ + public static function activateBranchPreviewRule(Document $project, Document $site, Document $deployment, Database $dbForPlatform, string $sitesDomain): void + { + $branchName = $deployment->getAttribute('providerBranch', ''); + if (empty($branchName)) { + return; + } + + $domain = (new BranchDomainFilter())->apply([ + 'branch' => $branchName, + 'resourceId' => $site->getId(), + 'projectId' => $project->getId(), + 'sitesDomain' => $sitesDomain, + ]); + $ruleId = md5($domain); + + try { + $dbForPlatform->createDocument('rules', new Document([ + '$id' => $ruleId, + 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), + 'domain' => $domain, + 'type' => 'deployment', + 'trigger' => 'deployment', + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getSequence(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getSequence(), + 'deploymentVcsProviderBranch' => $branchName, + 'status' => 'verified', + 'certificateId' => '', + 'search' => implode(' ', [$ruleId, $domain]), + 'owner' => 'Appwrite', + 'region' => $project->getAttribute('region'), + ])); + } catch (Duplicate) { + $dbForPlatform->updateDocument('rules', $ruleId, new Document([ + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getSequence(), + ])); + } + + $dbForPlatform->forEach('rules', function (Document $rule) use ($dbForPlatform, $deployment) { + $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getSequence(), + ])); + }, [ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::equal('type', ['deployment']), + Query::equal('deploymentResourceInternalId', [$site->getSequence()]), + Query::equal('deploymentResourceType', ['site']), + Query::equal('deploymentVcsProviderBranch', [$branchName]), + Query::equal('trigger', ['manual']), + ]); + } + /** * Update empty manual rule for deployment. * In case of first deployment, deployment ID will be empty in the rules, so we need to update it here. diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php index c80249d8a9..5fd035cdc3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -134,6 +134,9 @@ class Create extends Action 'buildSize' => null, 'buildPath' => '', 'buildLogs' => '', + // Not inherited: a redeploy always goes live, and the source's own + // flag is unset by deactivateOthers() once anything newer builds. + 'activate' => true, ]); if ($hasSource) { diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index b44c8a7ef8..a6f7c3e047 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -329,7 +329,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform, deployments: $deployments); + $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $deployments, $platform); } // Inform scheduler if function is still active diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 3b2daf976b..55fa30728a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -4,6 +4,7 @@ namespace Appwrite\Platform\Modules\Functions\Workers; use Ahc\Jwt\JWT; use Appwrite\Deployment\Backend; +use Appwrite\Deployment\Detection; use Appwrite\Event\Event; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Publisher\Func as FunctionPublisher; @@ -12,7 +13,7 @@ use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Extend\Exception as AppwriteException; -use Appwrite\Filter\BranchDomain as BranchDomainFilter; +use Appwrite\Platform\Modules\Compute\Base; use Appwrite\Usage\Build as BuildUsage; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; @@ -29,14 +30,11 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Exception\Conflict; -use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; use Utopia\Detector\Detection\Rendering\SSR; -use Utopia\Detector\Detection\Rendering\XStatic; -use Utopia\Detector\Detector\Rendering; use Utopia\Logger\Log; use Utopia\Platform\Action; use Utopia\Queue\Message; @@ -529,8 +527,7 @@ class Builds extends Action // artifact system has no primitive for). With the commit pushed, // hand the build to the jobs-service like any other VCS commit, // via the same Deployments service the HTTP endpoints use. - if ($resource->getCollection() === 'functions' - && System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator') { + if (System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator') { $ref = $deployment->getAttribute('providerCommitHash') ?: $branchName; $deployments->createFromUrl( $resource, @@ -745,10 +742,7 @@ class Builds extends Action break; } - $command = $this->getCommand( - resource: $resource, - deployment: $deployment - ); + $command = Backend::command($resource, $deployment); $cacheKey = $this->getNodeModulesCacheKey($project, $resource, $runtime, $version, $command); @@ -960,7 +954,7 @@ class Builds extends Action $adapter = null; if ($resource->getCollection() === 'sites' && ! empty($detectionLogs)) { - $detection = $this->detectSiteRendering($resource->getAttribute('framework', ''), $detectionLogs); + $detection = Detection::rendering($resource->getAttribute('framework', ''), \explode("\n", $detectionLogs)); $adapter = $resource->getAttribute('adapter', ''); if (empty($adapter)) { @@ -1108,63 +1102,9 @@ class Builds extends Action ->setPayload($deployment->getArrayCopy()) ->trigger(); - if ($resource->getCollection() === 'sites') { - // VCS branch - $branchName = $deployment->getAttribute('providerBranch'); - if (! empty($branchName)) { - $domain = (new BranchDomainFilter())->apply([ - 'branch' => $branchName, - 'resourceId' => $resource->getId(), - 'projectId' => $project->getId(), - 'sitesDomain' => $platform['sitesDomain'], - ]); - $ruleId = md5($domain); - - try { - $dbForPlatform->createDocument('rules', new Document([ - '$id' => $ruleId, - 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), - 'domain' => $domain, - 'type' => 'deployment', - 'trigger' => 'deployment', - 'deploymentId' => $deployment->getId(), - 'deploymentInternalId' => $deployment->getSequence(), - 'deploymentResourceType' => 'site', - 'deploymentResourceId' => $resource->getId(), - 'deploymentResourceInternalId' => $resource->getSequence(), - 'deploymentVcsProviderBranch' => $branchName, - 'status' => 'verified', - 'certificateId' => '', - 'search' => implode(' ', [$ruleId, $domain]), - 'owner' => 'Appwrite', - 'region' => $project->getAttribute('region'), - ])); - } catch (Duplicate $err) { - $rule = $dbForPlatform->updateDocument('rules', $ruleId, new Document([ - 'deploymentId' => $deployment->getId(), - 'deploymentInternalId' => $deployment->getSequence(), - ])); - } - - $queries = [ - Query::equal('projectInternalId', [$project->getSequence()]), - Query::equal('type', ['deployment']), - Query::equal('deploymentResourceInternalId', [$resource->getSequence()]), - Query::equal('deploymentResourceType', ['site']), - Query::equal('deploymentVcsProviderBranch', [$branchName]), - Query::equal('trigger', ['manual']), - ]; - - $dbForPlatform->foreach('rules', function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ - 'deploymentId' => $deployment->getId(), - 'deploymentInternalId' => $deployment->getSequence(), - ])); - }, $queries); - - Span::add('build.preview_rule_created', true); - } + if ($resource->getCollection() === 'sites' && ! empty($deployment->getAttribute('providerBranch'))) { + Base::activateBranchPreviewRule($project, $resource, $deployment, $dbForPlatform, $platform['sitesDomain']); + Span::add('build.preview_rule_created', true); } $queueForRealtime @@ -1331,35 +1271,6 @@ class Builds extends Action }; } - protected function getCommand(Document $resource, Document $deployment): string - { - if ($resource->getCollection() === 'functions') { - return $deployment->getAttribute('buildCommands', ''); - } elseif ($resource->getCollection() === 'sites') { - $commands = []; - - $frameworks = Config::getParam('frameworks', []); - $framework = $frameworks[$resource->getAttribute('framework', '')] ?? null; - - $envCommand = ''; - $bundleCommand = ''; - if (! is_null($framework)) { - $envCommand = $framework['envCommand'] ?? ''; - $bundleCommand = $framework['bundleCommand'] ?? ''; - } - - $commands[] = $envCommand; - $commands[] = $deployment->getAttribute('buildCommands', ''); - $commands[] = $bundleCommand; - - $commands = array_filter($commands, fn ($command) => ! empty($command)); - - return implode(' && ', $commands); - } - - return ''; - } - protected function getNodeModulesCacheKey(Document $project, Document $resource, array $runtime, string $version, string $command): string { if ($version !== 'v5' || $command === '' || $command === '0') { @@ -1422,24 +1333,6 @@ class Builds extends Action ]; } - protected function detectSiteRendering(string $framework, string $detectionLogs): object - { - $files = \explode("\n", $detectionLogs); - $files = \array_filter($files); - $files = \array_map(\trim(...), $files); - $files = \array_map(fn ($file) => \str_starts_with($file, './') ? \substr($file, 2) : $file, $files); - - $detector = new Rendering($framework); - foreach ($files as $file) { - $detector->addInput($file); - } - - return $detector - ->addOption(new SSR()) - ->addOption(new XStatic()) - ->detect(); - } - /** * @throws Structure * @throws \Utopia\Database\Exception diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 29d1a4842a..0ec5cd5ce6 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -2,13 +2,16 @@ namespace Appwrite\Platform\Modules\Functions\Workers; +use Appwrite\Deployment\Detection; use Appwrite\Event\Event; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Jobs as JobsMessage; use Appwrite\Event\Publisher\Func as FunctionPublisher; +use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; +use Appwrite\Platform\Modules\Compute\Base; use Appwrite\Usage\Build as BuildUsage; use Appwrite\Usage\Context as UsageContext; use Appwrite\Utopia\Response\Model\Deployment; @@ -60,12 +63,14 @@ class Jobs extends Action ->inject('queueForEvents') ->inject('queueForWebhooks') ->inject('publisherForFunctions') + ->inject('publisherForScreenshots') ->inject('publisherForUsage') ->inject('usage') ->inject('deviceForBuilds') ->inject('vcsFactory') ->inject('cache') ->inject('locks') + ->inject('platform') ->callback($this->action(...)); } @@ -78,12 +83,14 @@ class Jobs extends Action Event $queueForEvents, Webhook $queueForWebhooks, FunctionPublisher $publisherForFunctions, + ScreenshotPublisher $publisherForScreenshots, UsagePublisher $publisherForUsage, UsageContext $usage, Device $deviceForBuilds, VcsFactory $vcsFactory, Cache $cache, callable $locks, + array $platform, ): void { $event = JobsMessage::fromArray($message->getPayload()); @@ -92,7 +99,7 @@ class Jobs extends Action return; } - $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForUsage, $usage, $deviceForBuilds, $vcsFactory, $cache, $deploymentId): void { + $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForScreenshots, $publisherForUsage, $usage, $deviceForBuilds, $vcsFactory, $cache, $platform, $deploymentId): void { if ($event->id !== '') { $key = 'jobs-event-' . $event->id; if ($cache->load($key, self::DEDUPE_TTL) !== false) { @@ -110,9 +117,9 @@ class Jobs extends Action $deployment = match ($event->event) { 'orchestrator.job.log' => $this->onLog($dbForProject, $deployment, $event->data), - 'orchestrator.job.artifact' => $this->onArtifact($dbForProject, $deployment, $event->data), - 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory, $cache), - 'orchestrator.job.complete' => $this->onComplete($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory, $cache), + 'orchestrator.job.artifact' => $this->onArtifact($dbForProject, $dbForPlatform, $project, $deployment, $event->data, $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform), + 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform), + 'orchestrator.job.complete' => $this->onComplete($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform), default => $deployment, }; @@ -120,8 +127,8 @@ class Jobs extends Action $queueForRealtime ->setSubscribers(['console']) ->setProject($project) - ->setEvent('functions.[functionId].deployments.[deploymentId].update') - ->setParam('functionId', $deployment->getAttribute('resourceId')) + ->setEvent(self::event($deployment)) + ->setParam(self::resourceParam($deployment), $deployment->getAttribute('resourceId')) ->setParam('deploymentId', $deploymentId) ->setPayload($deployment->getArrayCopy()) ->trigger(); @@ -161,15 +168,35 @@ class Jobs extends Action } /** - * Record a reported artifact size. Remote-source builds (templates / VCS) - * fetch the source in the sidecar, so Appwrite can't size it the way the - * uploaded-tarball path does — the job stats the downloaded archive and the - * orchestrator reports its size here, which becomes the deployment's - * sourceSize. This arrives before the exit callback, so finalize folds it - * into totalSize. + * Record a reported artifact: 'sourceSize' (remote-source builds) becomes + * the deployment's sourceSize; 'manifest' (site builds) is the output file + * listing for adapter detection, saved as a marker that joins readiness. */ - private function onArtifact(Database $dbForProject, Document $deployment, array $data): Document - { + private function onArtifact( + Database $dbForProject, + Database $dbForPlatform, + Document $project, + Document $deployment, + array $data, + UsageContext $usage, + UsagePublisher $publisherForUsage, + ScreenshotPublisher $publisherForScreenshots, + Device $deviceForBuilds, + VcsFactory $vcsFactory, + Cache $cache, + array $platform, + ): Document { + if (($data['artifactId'] ?? '') === 'manifest') { + // Content arrives JSON-decoded; a failed or unparseable manifest + // degrades to an empty listing (detection skipped), never a failed build. + $content = ($data['status'] ?? '') === 'success' ? ($data['content'] ?? null) : null; + $manifest = \is_array($content) ? $content : \json_decode((string) $content, true); + $files = \is_array($manifest) ? (array) ($manifest['files'] ?? []) : []; + $cache->save('jobs-manifest-' . $deployment->getId(), ['files' => \array_values($files)]); + + return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform); + } + if (($data['artifactId'] ?? '') !== 'sourceSize' || ($data['status'] ?? '') !== 'success') { return $deployment; } @@ -180,9 +207,6 @@ class Jobs extends Action return $deployment; } - // Recompute totalSize too: this callback can land either side of exit - // (they arrive out of order), and finalize likewise derives totalSize - // from the current sourceSize — so whichever runs last, the two agree. return $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'sourceSize' => $size, 'totalSize' => $size + (int) $deployment->getAttribute('buildSize', 0), @@ -190,13 +214,11 @@ class Jobs extends Action } /** - * A successful build needs both terminal callbacks before it can ready: - * exit carries the code but fires before post-job artifacts are - * delivered; complete confirms delivery but carries no code. Failures - * short-circuit at exit — no output is needed to fail. The success join - * is order-independent: each of exit(0)/complete leaves a marker, and - * whichever finds the other's marker finalizes (markers live in cache, - * applied under the per-deployment lock). + * Failures short-circuit here — no output is needed to fail. A success + * needs every terminal callback: exit carries the code but fires before + * post-job artifacts, complete confirms delivery, and site builds also + * need the manifest. Each leaves a cache marker and re-attempts the join + * via ready(), so whichever lands last finalizes. */ private function onExit( Database $dbForProject, @@ -206,26 +228,24 @@ class Jobs extends Action int $exitCode, UsageContext $usage, UsagePublisher $publisherForUsage, + ScreenshotPublisher $publisherForScreenshots, Device $deviceForBuilds, VcsFactory $vcsFactory, Cache $cache, + array $platform, ): Document { if ($exitCode !== 0) { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $vcsFactory); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $publisherForScreenshots, $vcsFactory, $platform); } $cache->save('jobs-exit-' . $deployment->getId(), true); - if ($cache->load('jobs-complete-' . $deployment->getId(), self::DEDUPE_TTL) !== false) { - return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory); - } - return $deployment; + return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform); } /** - * The delivery half of the success join — see onExit. Emitted by the - * jobs-service once the job's post-job artifacts have run, so on every - * storage strategy the output is already where Appwrite reads it. + * The delivery half of the success join — see onExit. Fires once post-job + * artifacts have run, so the output is already where Appwrite reads it. */ private function onComplete( Database $dbForProject, @@ -234,26 +254,20 @@ class Jobs extends Action Document $deployment, UsageContext $usage, UsagePublisher $publisherForUsage, + ScreenshotPublisher $publisherForScreenshots, Device $deviceForBuilds, VcsFactory $vcsFactory, Cache $cache, + array $platform, ): Document { - if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'], true)) { - return $deployment; // already finalized (a non-zero exit fails at the exit callback) - } - - if ($cache->load('jobs-exit-' . $deployment->getId(), self::DEDUPE_TTL) !== false) { - return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory); - } - $cache->save('jobs-complete-' . $deployment->getId(), true); - return $deployment; + return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $publisherForScreenshots, $deviceForBuilds, $vcsFactory, $cache, $platform); } /** - * Finalize a successful build: enforce the size limit against the stored - * output, then mark ready. + * Attempt the success join (see onExit); a no-op until every marker is in. + * Once joined: adapter detection for sites, size limit, then ready. */ private function ready( Database $dbForProject, @@ -262,12 +276,38 @@ class Jobs extends Action Document $deployment, UsageContext $usage, UsagePublisher $publisherForUsage, + ScreenshotPublisher $publisherForScreenshots, Device $deviceForBuilds, VcsFactory $vcsFactory, + Cache $cache, + array $platform, ): Document { + if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'], true)) { + return $deployment; // already finalized + } + + $deploymentId = $deployment->getId(); + $isSite = $deployment->getAttribute('resourceType') === 'sites'; + + if ($cache->load('jobs-exit-' . $deploymentId, self::DEDUPE_TTL) === false || $cache->load('jobs-complete-' . $deploymentId, self::DEDUPE_TTL) === false) { + return $deployment; + } + + $manifest = $isSite ? $cache->load('jobs-manifest-' . $deploymentId, self::DEDUPE_TTL) : null; + if ($isSite && $manifest === false) { + return $deployment; + } + + if ($isSite) { + [$deployment, $mismatch] = $this->detect($dbForProject, $deployment, (array) ($manifest['files'] ?? [])); + if ($mismatch !== null) { + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, $mismatch, $usage, $publisherForUsage, $publisherForScreenshots, $vcsFactory, $platform); + } + } + $path = (string) $deployment->getAttribute('buildPath', ''); if ($path === '' || ! $deviceForBuilds->exists($path)) { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build produced no output artifact.', $usage, $publisherForUsage, $vcsFactory); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build produced no output artifact.', $usage, $publisherForUsage, $publisherForScreenshots, $vcsFactory, $platform); } $size = $deviceForBuilds->getFileSize($path); @@ -276,10 +316,44 @@ class Jobs extends Action if ($limit !== 0 && $size > $limit) { $deviceForBuilds->delete($path); - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $vcsFactory); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $publisherForScreenshots, $vcsFactory, $platform); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $publisherForScreenshots, $vcsFactory, $platform, $size); + } + + /** + * Adapter detection over the site's build-manifest file listing: a first + * successful build pins adapter + fallbackFile on the site and deployment; + * a site pinned to 'ssr' that built static returns the failure message. + * + * @return array{Document, ?string} + */ + private function detect(Database $dbForProject, Document $deployment, array $files): array + { + $site = empty($files) ? new Document() : $dbForProject->getDocument('sites', $deployment->getAttribute('resourceId')); + if ($site->isEmpty()) { + return [$deployment, null]; + } + + $detection = Detection::rendering($site->getAttribute('framework', ''), $files); + + $adapter = $site->getAttribute('adapter', ''); + if (empty($adapter)) { + $update = [ + 'adapter' => $detection->getName(), + 'fallbackFile' => $detection->getFallbackFile() ?? '', + ]; + $dbForProject->updateDocument('sites', $site->getId(), new Document($update)); + + return [$dbForProject->updateDocument('deployments', $deployment->getId(), new Document($update)), null]; + } + + if ($adapter === 'ssr' && $detection->getName() === 'static') { + return [$deployment, 'Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter]; + } + + return [$deployment, null]; } /** @@ -296,10 +370,13 @@ class Jobs extends Action string $message, UsageContext $usage, UsagePublisher $publisherForUsage, + ScreenshotPublisher $publisherForScreenshots, VcsFactory $vcsFactory, + array $platform, int $buildSize = 0, ): Document { - $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); + $collection = $deployment->getAttribute('resourceType', 'functions'); + $resource = $dbForProject->getDocument($collection, $deployment->getAttribute('resourceId')); $logs = $deployment->getAttribute('buildLogs', ''); $trailer = $success @@ -325,30 +402,40 @@ class Jobs extends Action ]); $deployment = $dbForProject->getDocument('deployments', $deployment->getId()); - if ($applied > 0 && $success && $deployment->getAttribute('activate') === true && ! $function->isEmpty()) { - $this->activate($dbForProject, $dbForPlatform, $project, $function, $deployment); + if ($applied > 0 && $success && $deployment->getAttribute('activate') === true && ! $resource->isEmpty()) { + $this->activate($dbForProject, $dbForPlatform, $project, $resource, $deployment); + } + + if ($applied > 0 && $success && $collection === 'sites' && ! $resource->isEmpty()) { + // Every successful site build, activated or not, repoints the + // branch preview rule and refreshes the console screenshots. + Base::activateBranchPreviewRule($project, $resource, $deployment, $dbForPlatform, $platform['sitesDomain']); + $publisherForScreenshots->enqueue(new \Appwrite\Event\Message\Screenshot( + project: $project, + deploymentId: $deployment->getId(), + )); } // Count the build for usage/billing once it reached a terminal outcome // (mirrors the executor Builds worker); never for a concurrently-canceled // build (the guard above left it 'canceled'). - if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'], true) && ! $function->isEmpty()) { - BuildUsage::publish($usage, $function, $deployment, $project, $publisherForUsage); + if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'], true) && ! $resource->isEmpty()) { + BuildUsage::publish($usage, $resource, $deployment, $project, $publisherForUsage); } - // Keep the function's "latest deployment" pointer + status current, and - // (re)activate its schedule so the scheduler enqueues cron executions — - // both mirror the executor Builds worker on build completion. - if (! $function->isEmpty()) { - $this->updateLatestDeployment($dbForProject, $function); - $this->schedule($dbForProject, $dbForPlatform, $function); + // Keep the resource's "latest deployment" pointer + status current, and + // (re)activate its schedule so the scheduler enqueues cron executions + // (sites have no scheduleId, so schedule() no-ops for them). + if (! $resource->isEmpty()) { + $this->updateLatestDeployment($dbForProject, $resource); + $this->schedule($dbForProject, $dbForPlatform, $resource); } // Report the terminal outcome as a VCS commit status (jobs-built VCS // deployments only; best-effort). "pending" at build start is a follow-up. $status = $deployment->getAttribute('status'); - if (\in_array($status, ['ready', 'failed'], true) && ! $function->isEmpty()) { - $this->gitStatus($vcsFactory, $dbForPlatform, $project, $function, $deployment, $status === 'ready' ? 'success' : 'failure'); + if (\in_array($status, ['ready', 'failed'], true) && ! $resource->isEmpty()) { + $this->gitStatus($vcsFactory, $dbForPlatform, $project, $resource, $deployment, $status === 'ready' ? 'success' : 'failure'); } return $deployment; @@ -381,15 +468,15 @@ class Jobs extends Action * Post a VCS commit status for a VCS deployment (no-op for non-VCS builds * or silent mode). Best-effort — a failed status update never fails the build. */ - private function gitStatus(VcsFactory $vcsFactory, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void + private function gitStatus(VcsFactory $vcsFactory, Database $dbForPlatform, Document $project, Document $resource, Document $deployment, string $state): void { $commitHash = $deployment->getAttribute('providerCommitHash', ''); - if ($commitHash === '' || $function->getAttribute('providerSilentMode', false) === true) { + if ($commitHash === '' || $resource->getAttribute('providerSilentMode', false) === true) { return; } try { - $installation = $dbForPlatform->getDocument('installations', $function->getAttribute('installationId', '')); + $installation = $dbForPlatform->getDocument('installations', $resource->getAttribute('installationId', '')); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); if ($providerInstallationId === '') { return; @@ -399,9 +486,12 @@ class Jobs extends Action $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', '')); $region = $project->getAttribute('region', 'default'); - $targetUrl = "{$protocol}://{$hostname}/console/project-{$region}-{$project->getId()}/functions/function-{$function->getId()}"; + $segment = $resource->getCollection() === 'sites' + ? "sites/site-{$resource->getId()}" + : "functions/function-{$resource->getId()}"; + $targetUrl = "{$protocol}://{$hostname}/console/project-{$region}-{$project->getId()}/{$segment}"; $message = $state === 'success' ? 'Build succeeded.' : 'Build failed.'; - $name = $function->getAttribute('name', '') . ' (' . $project->getAttribute('name', '') . ')'; + $name = $resource->getAttribute('name', '') . ' (' . $project->getAttribute('name', '') . ')'; $vcs->updateCommitStatus( $deployment->getAttribute('providerRepositoryName', ''), @@ -418,12 +508,12 @@ class Jobs extends Action } /** - * Point the function at this deployment (auto-activate). Mirrors the - * essential function activation from the Builds worker. + * Point the resource at this deployment (auto-activate). Mirrors the + * essential activation from the Builds worker. */ - private function activate(Database $dbForProject, Database $dbForPlatform, Document $project, Document $function, Document $deployment): void + private function activate(Database $dbForProject, Database $dbForPlatform, Document $project, Document $resource, Document $deployment): void { - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document([ + $resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document([ 'live' => true, 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getSequence(), @@ -438,23 +528,23 @@ class Jobs extends Action }, [ Query::equal('projectInternalId', [$project->getSequence()]), Query::equal('type', ['deployment']), - Query::equal('deploymentResourceInternalId', [$function->getSequence()]), - Query::equal('deploymentResourceType', ['function']), + Query::equal('deploymentResourceInternalId', [$resource->getSequence()]), + Query::equal('deploymentResourceType', [$resource->getCollection() === 'sites' ? 'site' : 'function']), Query::equal('trigger', ['manual']), Query::equal('deploymentVcsProviderBranch', ['']), ]); } /** - * Refresh the function's latestDeployment* fields from its newest + * Refresh the resource's latestDeployment* fields from its newest * deployment. Mirrors the Builds worker so the console reflects the current * build status. */ - private function updateLatestDeployment(Database $dbForProject, Document $function): void + private function updateLatestDeployment(Database $dbForProject, Document $resource): void { $latest = $dbForProject->findOne('deployments', [ - Query::equal('resourceType', ['functions']), - Query::equal('resourceInternalId', [$function->getSequence()]), + Query::equal('resourceType', [$resource->getCollection()]), + Query::equal('resourceInternalId', [$resource->getSequence()]), Query::orderDesc('$createdAt'), ]); @@ -462,7 +552,7 @@ class Jobs extends Action return; } - $dbForProject->updateDocument('functions', $function->getId(), new Document([ + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document([ 'latestDeploymentId' => $latest->getId(), 'latestDeploymentInternalId' => $latest->getSequence(), 'latestDeploymentCreatedAt' => $latest->getCreatedAt(), @@ -471,20 +561,20 @@ class Jobs extends Action } /** - * (Re)activate the function's schedule document so the scheduler enqueues + * (Re)activate the resource's schedule document so the scheduler enqueues * cron executions. Mirrors the executor Builds worker: a schedule is active - * only when the function has both a cron expression and an active - * deployment. Re-reads the function so it sees a deploymentId just set by + * only when the resource has both a cron expression and an active + * deployment. Re-reads the resource so it sees a deploymentId just set by * activate(). */ - private function schedule(Database $dbForProject, Database $dbForPlatform, Document $function): void + private function schedule(Database $dbForProject, Database $dbForPlatform, Document $resource): void { - $scheduleId = $function->getAttribute('scheduleId', ''); + $scheduleId = $resource->getAttribute('scheduleId', ''); if ($scheduleId === '') { return; } - $function = $dbForProject->getDocument('functions', $function->getId()); + $resource = $dbForProject->getDocument($resource->getCollection(), $resource->getId()); $schedule = $dbForPlatform->getDocument('schedules', $scheduleId); if ($schedule->isEmpty()) { return; @@ -492,8 +582,8 @@ class Jobs extends Action $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ 'resourceUpdatedAt' => DateTime::now(), - 'schedule' => $function->getAttribute('schedule', ''), - 'active' => ! empty($function->getAttribute('schedule')) && ! empty($function->getAttribute('deploymentId')), + 'schedule' => $resource->getAttribute('schedule', ''), + 'active' => ! empty($resource->getAttribute('schedule')) && ! empty($resource->getAttribute('deploymentId')), ])); } @@ -512,8 +602,8 @@ class Jobs extends Action $model = new Deployment(); $update = $queueForEvents ->setProject($project) - ->setEvent('functions.[functionId].deployments.[deploymentId].update') - ->setParam('functionId', $deployment->getAttribute('resourceId')) + ->setEvent(self::event($deployment)) + ->setParam(self::resourceParam($deployment), $deployment->getAttribute('resourceId')) ->setParam('deploymentId', $deployment->getId()) ->setPayload($deployment->getArrayCopy(\array_keys($model->getRules()))); @@ -530,6 +620,18 @@ class Jobs extends Action )); } + private static function event(Document $deployment): string + { + $param = self::resourceParam($deployment); + + return "{$deployment->getAttribute('resourceType', 'functions')}.[{$param}].deployments.[deploymentId].update"; + } + + private static function resourceParam(Document $deployment): string + { + return $deployment->getAttribute('resourceType') === 'sites' ? 'siteId' : 'functionId'; + } + private function truncate(string $logs): string { $limit = APP_LOG_LENGTH_LIMIT; diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php index 40bdbe8e8e..73ef6e6328 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments\Download; +use Appwrite\Deployment\Token; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -9,13 +10,16 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\MethodType; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Http\Adapter\Swoole\Request; use Utopia\Platform\Enum; use Utopia\Platform\Scope\HTTP; use Utopia\Storage\Device; +use Utopia\Validator\Text; use Utopia\Validator\WhiteList; class Get extends Action @@ -34,7 +38,10 @@ class Get extends Action ->setHttpPath('/v1/sites/:siteId/deployments/:deploymentId/download') ->desc('Get deployment download') ->groups(['api', 'sites']) - ->label('scope', 'sites.read') + // Reachable by privileged callers (console/CLI via sites.read) + // and by guests holding a valid presigned token (public); the + // action enforces which one actually applies. + ->label('scope', ['public', 'sites.read']) ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( @@ -58,11 +65,14 @@ class Get extends Action ->param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject']) ->param('deploymentId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Deployment ID.', false, ['dbForProject']) ->param('type', 'source', new WhiteList(['source', 'output']), 'Deployment file to download. Can be: "source", "output".', true, enum: new Enum(name: 'DeploymentDownloadType')) + ->param('token', '', new Text(2048), 'Presigned source-download token for accessing this deployment without a session (jobs-service).', true) ->inject('response') ->inject('request') ->inject('dbForProject') ->inject('deviceForSites') ->inject('deviceForBuilds') + ->inject('user') + ->inject('authorization') ->callback($this->action(...)); } @@ -70,18 +80,32 @@ class Get extends Action string $siteId, string $deploymentId, string $type, + string $token, Response $response, Request $request, Database $dbForProject, Device $deviceForSites, - Device $deviceForBuilds + Device $deviceForBuilds, + User $user, + Authorization $authorization, ) { - $site = $dbForProject->getDocument('sites', $siteId); + // Access is granted either to a privileged caller (console/CLI via + // session or API key) or to a valid presigned token bound to this + // deployment + artifact type (used by the jobs-service). + $isPrivileged = $user->isPrivileged($authorization->getRoles()) || $user->isKey($authorization->getRoles()); + if (! $isPrivileged && ! Token::verify($token, $deploymentId, $type)) { + throw new Exception(Exception::USER_UNAUTHORIZED); + } + + // Access has been authorized at the endpoint level (privileged caller or + // presigned token), so read the artifact documents without re-applying + // document-level permissions (a token holder is not an authed user). + $site = $authorization->skip(fn () => $dbForProject->getDocument('sites', $siteId)); if ($site->isEmpty()) { throw new Exception(Exception::SITE_NOT_FOUND); } - $deployment = $dbForProject->getDocument('deployments', $deploymentId); + $deployment = $authorization->skip(fn () => $dbForProject->getDocument('deployments', $deploymentId)); if ($deployment->isEmpty()) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index b0eb8d343c..7b400e865c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -2,14 +2,14 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments\Duplicate; +use Appwrite\Deployment\Backend; use Appwrite\Event\Event; -use Appwrite\Event\Message\Build as BuildMessage; -use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -20,6 +20,7 @@ use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Storage\Device; use Utopia\System\System; +use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action { @@ -65,8 +66,9 @@ class Create extends Action ->inject('dbForProject') ->inject('dbForPlatform') ->inject('queueForEvents') - ->inject('publisherForBuilds') + ->inject('deployments') ->inject('deviceForSites') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -81,8 +83,9 @@ class Create extends Action Database $dbForProject, Database $dbForPlatform, Event $queueForEvents, - BuildPublisher $publisherForBuilds, + Backend $deployments, Device $deviceForSites, + VcsFactory $vcsFactory, Authorization $authorization, array $platform ) { @@ -97,15 +100,27 @@ class Create extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } + // Remote-source deployments (templates / VCS) on the jobs-service + // backend never store a source tarball — the build sidecar fetches + // it — so a duplicate re-fetches the same source from the + // coordinates persisted on the deployment. $path = $deployment->getAttribute('sourcePath'); - if (empty($path) || !$deviceForSites->exists($path)) { + $hasSource = !empty($path) && $deviceForSites->exists($path); + $installationId = $deployment->getAttribute('installationId', ''); + $owner = $deployment->getAttribute('providerRepositoryOwner', ''); + $repository = $deployment->getAttribute('providerRepositoryName', ''); + + if (!$hasSource && ($owner === '' || $repository === '')) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } $deploymentId = ID::unique(); - $destination = $deviceForSites->getPath($deploymentId . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); - $deviceForSites->transfer($path, $destination, $deviceForSites); + $destination = ''; + if ($hasSource) { + $destination = $deviceForSites->getPath($deploymentId . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); + $deviceForSites->transfer($path, $destination, $deviceForSites); + } $commands = []; @@ -116,12 +131,16 @@ class Create extends Action $commands[] = $site->getAttribute('buildCommand', ''); } + // Cloning the source deployment's attributes onto the new one, with + // its own $id and no $sequence, tells the service to create it fresh + // rather than update the deployment being duplicated. A re-fetched + // source starts unsized; its stat artifact reports the size. $deployment->removeAttribute('$sequence'); - - $deployment = $dbForProject->createDocument('deployments', $deployment->setAttributes([ + $deployment->setAttributes([ '$id' => $deploymentId, 'sourcePath' => $destination, - 'totalSize' => $deployment->getAttribute('sourceSize', 0), + 'sourceSize' => $hasSource ? $deployment->getAttribute('sourceSize', 0) : 0, + 'totalSize' => $hasSource ? $deployment->getAttribute('sourceSize', 0) : 0, 'buildCommands' => \implode(' && ', $commands), 'startCommand' => $site->getAttribute('startCommand', ''), 'buildOutput' => $site->getAttribute('outputDirectory', ''), @@ -133,11 +152,44 @@ class Create extends Action 'buildEndedAt' => null, 'buildDuration' => 0, 'buildSize' => 0, - 'status' => 'waiting', 'buildPath' => '', 'buildLogs' => '', - 'type' => $request->getHeaderLine('x-sdk-language') === 'cli' ? 'cli' : 'manual' - ])); + 'type' => $request->getHeaderLine('x-sdk-language') === 'cli' ? 'cli' : 'manual', + // Not inherited: a redeploy always goes live, and the source's own + // flag is unset by deactivateOthers() once anything newer builds. + 'activate' => true, + ]); + + if ($hasSource) { + $deployment = $deployments->createFromUpload($site, $deployment); + } elseif ($installationId !== '') { + $installation = $dbForPlatform->getDocument('installations', $installationId); + if ($installation->isEmpty()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + + $vcs = $vcsFactory->fromInstallation($installation); + + $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + $deployment = $deployments->createFromUrl( + $site, + $deployment, + $vcs->getRepositoryPresignedUrl($owner, $repository, $ref), + $deployment->getAttribute('providerRootDirectory', ''), + ); + } else { + // Public template repo: providerBranch holds the resolved ref + // (branch, tag, or commit — see Deployments/Template/Create). + $deployment = $deployments->createFromRef( + $site, + $deployment, + $owner, + $repository, + GitHub::CLONE_TYPE_BRANCH, + $deployment->getAttribute('providerBranch', ''), + $deployment->getAttribute('providerRootDirectory', ''), + ); + } // Preview deployments for sites $sitesDomain = $platform['sitesDomain']; @@ -155,8 +207,8 @@ class Create extends Action 'domain' => $domain, 'type' => 'deployment', 'trigger' => 'deployment', - 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), - 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getSequence(), + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getSequence(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $site->getId(), 'deploymentResourceInternalId' => $site->getSequence(), @@ -167,14 +219,6 @@ class Create extends Action ])) ); - $publisherForBuilds->enqueue(new BuildMessage( - project: $project, - resource: $site, - deployment: $deployment, - type: BUILD_TYPE_DEPLOYMENT, - platform: $platform, - )); - $queueForEvents ->setParam('siteId', $site->getId()) ->setParam('deploymentId', $deployment->getId()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php index c698a2d5f6..bbf7877791 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php @@ -2,13 +2,13 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments\Status; +use Appwrite\Deployment\Backend; use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Executor\Executor; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -56,9 +56,8 @@ class Update extends Action ->param('deploymentId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Deployment ID.', false, ['dbForProject']) ->inject('response') ->inject('dbForProject') - ->inject('project') ->inject('queueForEvents') - ->inject('executor') + ->inject('deployments') ->callback($this->action(...)); } @@ -67,9 +66,8 @@ class Update extends Action string $deploymentId, Response $response, Database $dbForProject, - Document $project, Event $queueForEvents, - Executor $executor + Backend $deployments ) { $site = $dbForProject->getDocument('sites', $siteId); @@ -92,11 +90,7 @@ class Update extends Action $duration = $endTime->getTimestamp() - $startTime->getTimestamp(); try { - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildEndedAt' => DateTime::now(), - 'buildDuration' => $duration, - 'status' => 'canceled' - ])); + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document($this->cancel($deployment, $duration))); } catch (TransactionException) { $deployment = $dbForProject->getDocument('deployments', $deployment->getId()); @@ -109,18 +103,14 @@ class Update extends Action } if ($deployment->getAttribute('status') !== 'canceled') { - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildEndedAt' => DateTime::now(), - 'buildDuration' => $duration, - 'status' => 'canceled' - ])); + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document($this->cancel($deployment, $duration))); } } + // Best-effort cleanup — the deployment is already marked 'canceled'. try { - $executor->deleteRuntime($project->getId(), $deploymentId . "-build"); + $deployments->cancel($deploymentId); } catch (\Throwable) { - // Best-effort cleanup — deployment status is already 'canceled' } $queueForEvents @@ -129,4 +119,28 @@ class Update extends Action $response->dynamic($deployment, Response::MODEL_DEPLOYMENT); } + + /** + * The sparse update marking a build canceled. Jobs-backed builds (identified + * by a buildPath set at submission) have no cancel worker to write the + * closing log line the executor's Builds worker adds, so it is appended here; + * executor deployments get it from their worker. + * + * @return array + */ + private function cancel(Document $deployment, int $duration): array + { + $update = [ + 'buildEndedAt' => DateTime::now(), + 'buildDuration' => $duration, + 'status' => 'canceled', + ]; + + if ($deployment->getAttribute('buildPath', '') !== '') { + $logs = $deployment->getAttribute('buildLogs', '') . "\033[90m[" . \date('H:i:s') . "] \033[90m[\033[0mappwrite\033[90m]\033[33m Build has been canceled. \033[0m\n"; + $update['buildLogs'] = \substr($logs, -APP_LOG_LENGTH_LIMIT); + } + + return $update; + } } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 772fd88720..93cc7270b3 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -2,8 +2,8 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments\Template; +use Appwrite\Deployment\Backend; use Appwrite\Event\Event; -use Appwrite\Event\Message\Build as BuildMessage; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -84,6 +84,7 @@ class Create extends Base ->inject('publisherForBuilds') ->inject('vcsFactory') ->inject('authorization') + ->inject('deployments') ->inject('platform') ->callback($this->action(...)); } @@ -105,6 +106,7 @@ class Create extends Base BuildPublisher $publisherForBuilds, VcsFactory $vcsFactory, Authorization $authorization, + Backend $deployments, array $platform ) { $site = $dbForProject->getDocument('sites', $siteId); @@ -139,6 +141,7 @@ class Create extends Base vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, + deployments: $deployments, platform: $platform ); @@ -161,6 +164,18 @@ class Create extends Base $commands[] = $site->getAttribute('buildCommand', ''); } + // Templates can pin a version range (e.g. "0.3.*"); codeload only takes + // a concrete ref, so resolve it to the highest matching tag via GitHub. + $ref = $reference; + if ($type === Git::CLONE_TYPE_TAG && \str_contains($reference, '*')) { + try { + $tags = $vcsFactory->fromProvider('github')->listTags($owner, $repository, $reference); + $ref = \end($tags) ?: $reference; + } catch (\Throwable) { + // Fall back to the raw reference; the build surfaces a bad ref. + } + } + $deploymentId = ID::unique(); $deployment = $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, @@ -179,7 +194,9 @@ class Create extends Base 'providerRepositoryOwner' => $owner, 'providerRepositoryUrl' => $repositoryUrl, 'providerBranchUrl' => $branchUrl, - 'providerBranch' => $type == Git::CLONE_TYPE_BRANCH ? $reference : '', + // The resolved concrete ref, so a duplicate can re-fetch the source. + 'providerBranch' => $ref, + 'providerRootDirectory' => $rootDirectory, 'adapter' => $site->getAttribute('adapter', ''), 'fallbackFile' => $site->getAttribute('fallbackFile', ''), 'type' => 'vcs', @@ -215,14 +232,18 @@ class Create extends Base $this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform, $authorization); - $publisherForBuilds->enqueue(new BuildMessage( - project: $project, - resource: $site, - deployment: $deployment, - type: BUILD_TYPE_DEPLOYMENT, - template: $template, - platform: $platform, - )); + // Public template: pull the source straight from GitHub's public repo + // (codeload tarball on the jobs backend, a plain git clone on the + // executor); unarchive/checkout strips down to the rootDirectory. + $deployment = $deployments->createFromRef( + $site, + $deployment, + $owner, + $repository, + $type, + $ref, + $rootDirectory, + ); $queueForEvents ->setParam('siteId', $site->getId()) diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php index 6e79b8fa81..4e10ed7822 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments\Vcs; +use Appwrite\Deployment\Backend; use Appwrite\Event\Event; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; @@ -76,6 +77,7 @@ class Create extends Base ->inject('publisherForBuilds') ->inject('vcsFactory') ->inject('authorization') + ->inject('deployments') ->inject('platform') ->callback($this->action(...)); } @@ -94,6 +96,7 @@ class Create extends Base BuildPublisher $publisherForBuilds, VcsFactory $vcsFactory, Authorization $authorization, + Backend $deployments, array $platform ) { $site = $dbForProject->getDocument('sites', $siteId); @@ -122,6 +125,7 @@ class Create extends Base vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, + deployments: $deployments, reference: $reference, referenceType: $type, platform: $platform diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index 6ae47e5a6e..216c861a82 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Sites\Http\Sites; +use Appwrite\Deployment\Backend; use Appwrite\Event\Event; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; @@ -19,6 +20,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Http\Adapter\Swoole\Request; use Utopia\Platform\Action; @@ -113,6 +115,8 @@ class Update extends Base ->inject('vcsFactory') ->inject('repositoryWebhooks') ->inject('executor') + ->inject('authorization') + ->inject('deployments') ->inject('platform') ->callback($this->action(...)); } @@ -151,6 +155,8 @@ class Update extends Base VcsFactory $vcsFactory, RepositoryWebhooks $repositoryWebhooks, Executor $executor, + Authorization $authorization, + Backend $deployments, array $platform ) { if (!empty($adapter)) { @@ -331,7 +337,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); + $this->redeployVcsSite($request, $site, $project, $installation, $dbForProject, $dbForPlatform, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $authorization, $deployments, $platform); } $queueForEvents->setParam('siteId', $site->getId()); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 10fe086f37..8939e869c1 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -1308,6 +1308,11 @@ final class SitesCustomServerTest extends Scope $this->assertEquals('ready', $deployment['body']['status']); }, 120000, 500); + // Adapter detection pinned the site on its first successful build. + $site = $this->getSite($siteId); + $this->assertEquals('static', $site['body']['adapter']); + $this->assertEquals('main.html', $site['body']['fallbackFile']); + $deployment = $this->createDeployment($siteId, [ 'code' => $this->packageSite('static-single-file'), 'activate' => 'false' diff --git a/tests/unit/Deployment/BackendTest.php b/tests/unit/Deployment/BackendTest.php new file mode 100644 index 0000000000..db024a4ef0 --- /dev/null +++ b/tests/unit/Deployment/BackendTest.php @@ -0,0 +1,44 @@ + [ + 'envCommand' => 'cp .env.example .env', + 'bundleCommand' => 'npm run bundle', + ], + ]); + + $resource = new Document([ + '$collection' => 'sites', + 'framework' => 'astro', + ]); + $deployment = new Document([ + 'buildCommands' => 'npm run build', + ]); + + $this->assertSame( + 'cp .env.example .env && npm run build && npm run bundle', + Backend::command($resource, $deployment) + ); + } + + public function testFunctionCommandIsDeploymentBuildCommands(): void + { + $resource = new Document(['$collection' => 'functions']); + $deployment = new Document(['buildCommands' => 'npm install']); + + $this->assertSame('npm install', Backend::command($resource, $deployment)); + } +} diff --git a/tests/unit/Deployment/DetectionTest.php b/tests/unit/Deployment/DetectionTest.php new file mode 100644 index 0000000000..42ec656830 --- /dev/null +++ b/tests/unit/Deployment/DetectionTest.php @@ -0,0 +1,27 @@ +assertSame('static', $detection->getName()); + $this->assertSame('main.html', $detection->getFallbackFile()); + } + + public function testRenderingFindsAstroSSR(): void + { + $detection = Detection::rendering('astro', ['./client/index.html', './server/entry.mjs']); + + $this->assertSame('ssr', $detection->getName()); + $this->assertNull($detection->getFallbackFile()); + } +} diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index a14a8137b3..f4260c433e 100644 --- a/tests/unit/Functions/BuildsTest.php +++ b/tests/unit/Functions/BuildsTest.php @@ -7,7 +7,6 @@ namespace Tests\Unit\Functions; use Appwrite\Platform\Modules\Functions\Workers\Builds; use PHPUnit\Framework\TestCase; use ReflectionMethod; -use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -20,29 +19,6 @@ final class BuildsTest extends TestCase $this->builds = new Builds(); } - public function testSiteCommandIncludesFrameworkAndDeploymentCommands(): void - { - Config::setParam('frameworks', [ - 'astro' => [ - 'envCommand' => 'cp .env.example .env', - 'bundleCommand' => 'npm run bundle', - ], - ]); - - $resource = new Document([ - '$collection' => 'sites', - 'framework' => 'astro', - ]); - $deployment = new Document([ - 'buildCommands' => 'npm run build', - ]); - - $this->assertSame( - 'cp .env.example .env && npm run build && npm run bundle', - $this->callBuilds('getCommand', $resource, $deployment) - ); - } - public function testSiteBuildCommandAppendsDetectionFileListing(): void { $command = $this->callBuilds('prepareSiteBuildCommand', 'npm run build', './dist folder', 'astro'); @@ -74,22 +50,6 @@ final class BuildsTest extends TestCase $this->assertSame("\n./index.html\n./server/entry.mjs\n", $result['detectionLogs']); } - public function testDetectSiteRenderingFindsStaticFallbackFile(): void - { - $detection = $this->callBuilds('detectSiteRendering', 'other', "./main.html\n"); - - $this->assertSame('static', $detection->getName()); - $this->assertSame('main.html', $detection->getFallbackFile()); - } - - public function testDetectSiteRenderingFindsAstroSSR(): void - { - $detection = $this->callBuilds('detectSiteRendering', 'astro', "./client/index.html\n./server/entry.mjs\n"); - - $this->assertSame('ssr', $detection->getName()); - $this->assertNull($detection->getFallbackFile()); - } - public function testDisconnectVcsClearsAllFieldsAndDeletesRepository(): void { $resource = new Document([