From 8034ef57aabc534e31626a484ab3ff0e84074f1c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 15 Jul 2026 17:05:44 +0400 Subject: [PATCH 1/2] fix: persist opaque build output paths --- app/controllers/general.php | 3 +- src/Appwrite/Compute/Job.php | 15 ++--- .../Platform/Modules/Compute/Base.php | 8 +-- .../Functions/Http/Deployments/Create.php | 7 +- .../Http/Deployments/Download/Get.php | 9 ++- .../Http/Deployments/Duplicate/Create.php | 5 +- .../Http/Deployments/Template/Create.php | 2 +- .../Functions/Http/Executions/Create.php | 3 +- .../Modules/Functions/Workers/Builds.php | 4 -- .../Modules/Functions/Workers/Jobs.php | 24 ++++++- .../Sites/Http/Deployments/Download/Get.php | 9 ++- .../Modules/VCS/Http/GitHub/Deployment.php | 7 +- src/Appwrite/Platform/Workers/Functions.php | 3 +- .../Functions/FunctionsConsoleClientTest.php | 2 +- .../Services/Sites/SitesCustomServerTest.php | 2 +- tests/unit/Functions/JobsTest.php | 65 +++++++++++++++++++ 16 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 tests/unit/Functions/JobsTest.php diff --git a/app/controllers/general.php b/app/controllers/general.php index f04697724f..686bfcdfb4 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -539,7 +539,6 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S 'site' => '', }; $source = $deployment->getAttribute('buildPath', ''); - $extension = str_ends_with($source, '.tar') ? 'tar' : 'tar.gz'; $startCommand = $runtime['startCommand']; if ($type === 'site') { @@ -560,7 +559,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $runtimeEntrypoint = match ($version) { 'v2' => '', - default => "cp /tmp/code.$extension /mnt/code/code.$extension && nohup helpers/start.sh \"$startCommand\"", + default => "cp /tmp/code.* /mnt/code/ && nohup helpers/start.sh \"$startCommand\"", }; $entrypoint = match ($type) { diff --git a/src/Appwrite/Compute/Job.php b/src/Appwrite/Compute/Job.php index ac8d2ce84e..79bf0fa106 100644 --- a/src/Appwrite/Compute/Job.php +++ b/src/Appwrite/Compute/Job.php @@ -21,7 +21,7 @@ use Utopia\System\System; * + unarchive, run by the sidecar) — a GET has no request-body cap, so large * sources are fine. The build output and package-manager cache instead go on a * mounted volume: the builds storage volume is attached to the build worker at - * its Appwrite path, so build.sh writes code.tar.gz + the cache squashfs + * its Appwrite path, so build.sh writes the build artifact + cache squashfs * straight onto the volume Appwrite already reads. That keeps the multi-hundred-MB * output off the (capped) HTTP upload path and out of the Appwrite process. * @@ -98,14 +98,14 @@ final class Job } // Output + cache live on the mounted builds volume (see class doc): the - // build writes code.tar.gz to the deployment's build dir and the cache + // build writes its artifact to the deployment's build dir and the cache // squashfs to its per-function keyed path — both directly on the volume // Appwrite reads, so nothing large crosses the HTTP boundary. $cacheKey = self::cacheKey($projectId, $function->getId(), $runtime['image'] ?? ''); $command = $deployment->getAttribute('buildCommands', ''); $env = self::variables($project, $function, $deployment, $runtime, $cpus, $memory, $endpoint, $timeout) + [ 'OPEN_RUNTIMES_BUILD_INPUT_DIR' => '/mnt/code/source', - 'OPEN_RUNTIMES_BUILD_OUTPUT_DIR' => \dirname(self::buildPath($projectId, $deploymentId)), + 'OPEN_RUNTIMES_BUILD_OUTPUT_DIR' => self::outputDirectory($projectId, $deploymentId), 'OPEN_RUNTIMES_BUILD_CACHE_ARTIFACT' => self::cachePath($projectId, $cacheKey), ]; @@ -153,13 +153,12 @@ final class Job } /** - * The build output path on the builds volume. build.sh writes code.tar.gz - * into OPEN_RUNTIMES_BUILD_OUTPUT_DIR (this file's directory); pre-computed - * so it can be persisted on the deployment before the job is submitted. + * The build output directory on the builds volume. The produced artifact's + * complete path is discovered and persisted after the job finishes. */ - public static function buildPath(string $projectId, string $deploymentId): string + public static function outputDirectory(string $projectId, string $deploymentId): string { - return APP_STORAGE_BUILDS . "/app-{$projectId}/{$deploymentId}/code.tar.gz"; + return APP_STORAGE_BUILDS . "/app-{$projectId}/{$deploymentId}"; } /** diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 25f071d253..c432b3df3c 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -124,16 +124,12 @@ class Base extends Action // when the caller opts in ($jobs) and _APP_BUILDS_BACKEND=orchestrator. // 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). When on jobs the deployment - // is pre-declared 'waiting' with its buildPath so build.sh writes output - // onto the mounted volume. + // jobs-service itself when on orchestrator). $useJobs = $jobs !== null && $function->getCollection() === 'functions' && $template->isEmpty() && System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator'; - $buildFields = $useJobs - ? ['status' => 'waiting', 'buildPath' => Job::buildPath($project->getId(), $deploymentId)] - : []; + $buildFields = $useJobs ? ['status' => 'waiting'] : []; $deployment = $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php index f6e4c59e44..722489ae39 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php @@ -339,13 +339,8 @@ class Create extends Action $useJobs = System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator'; // Fields marking the deployment as queued. The build worker - // promotes 'waiting' → 'building' on the first callback. The - // jobs path also pre-declares buildPath so build.sh writes the - // output straight onto the mounted builds volume. + // promotes 'waiting' → 'building' on the first callback. $buildFields = ['status' => 'waiting']; - if ($useJobs) { - $buildFields['buildPath'] = Job::buildPath($project->getId(), $deploymentId); - } if ($deployment->isEmpty()) { $deployment = $dbForProject->createDocument('deployments', new Document([ diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php index f089ac9a57..2b950d0afe 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php @@ -125,11 +125,16 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } + $contentType = $type === 'source' ? 'application/gzip' : 'application/octet-stream'; + $filename = $type === 'source' + ? $deploymentId . '-source.tar.gz' + : $deploymentId . '-output-' . \basename($path); + $response - ->setContentType('application/gzip') + ->setContentType($contentType) ->addHeader('Cache-Control', 'private, max-age=3888000') // 45 days ->addHeader('X-Peak', \memory_get_peak_usage()) - ->addHeader('Content-Disposition', 'attachment; filename="' . $deploymentId . '-' . $type . '.tar.gz"'); + ->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); $size = $device->getFileSize($path); $rangeHeader = $request->getHeaderLine('range'); 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 1e50fcd214..f56d0154ac 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -106,8 +106,7 @@ class Create extends Action $deploymentId = ID::unique(); // Build backend for the rebuild: 'orchestrator' (jobs-service, submitted - // here) or 'executor' (default; Builds worker). The jobs path pre-declares - // buildPath so build.sh writes output straight onto the mounted volume. + // here) or 'executor' (default; Builds worker). $useJobs = System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator'; $destination = $deviceForFunctions->getPath($deploymentId . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); @@ -127,7 +126,7 @@ class Create extends Action 'buildDuration' => null, 'buildSize' => null, 'status' => 'waiting', - 'buildPath' => $useJobs ? Job::buildPath($project->getId(), $deploymentId) : '', + 'buildPath' => '', 'buildLogs' => '', ])); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index 202e1c34d3..708a753bf6 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -188,7 +188,7 @@ class Create extends Base 'type' => 'vcs', 'activate' => $activate, 'status' => 'waiting', - 'buildPath' => $useJobs ? Job::buildPath($project->getId(), $deploymentId) : '', + 'buildPath' => '', ])); $this->updateEmptyManualRule($project, $function, $deployment, $dbForPlatform, $authorization); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 44f2b7b362..6fb5783194 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -417,8 +417,7 @@ class Create extends Base } $source = $deployment->getAttribute('buildPath', ''); - $extension = str_ends_with($source, '.tar') ? 'tar' : 'tar.gz'; - $command = $version === 'v2' ? '' : "cp /tmp/code.$extension /mnt/code/code.$extension && nohup helpers/start.sh \"$command\""; + $command = $version === 'v2' ? '' : "cp /tmp/code.* /mnt/code/ && nohup helpers/start.sh \"$command\""; try { $executionResponse = $executor->createExecution( projectId: $project->getId(), diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index fccafa256d..e8f7812e23 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -532,10 +532,6 @@ class Builds extends Action // builds volume, progress via callbacks to the jobs worker. if ($resource->getCollection() === 'functions' && System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator') { - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ - 'buildPath' => Job::buildPath($project->getId(), $deploymentId), - ])); - $ref = $deployment->getAttribute('providerCommitHash') ?: $branchName; $jobs->create(...Job::build($project, $resource, $deployment, $platform, [ 'url' => $github->getRepositoryPresignedUrl($cloneOwner, $cloneRepository, $ref), diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 9086ffa550..848ca53086 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -209,8 +209,12 @@ class Jobs extends Action return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $github); } - $path = Job::buildPath($project->getId(), $deployment->getId()); - $size = $deviceForBuilds->exists($path) ? $deviceForBuilds->getFileSize($path) : 0; + $path = $this->findBuildPath($deviceForBuilds, $project->getId(), $deployment); + if ($path === '') { + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build must produce exactly one output artifact.', $usage, $publisherForUsage, $github); + } + + $size = $deviceForBuilds->getFileSize($path); $limit = (int) System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000'); if ($limit !== 0 && $size > $limit) { @@ -218,7 +222,19 @@ class Jobs extends Action return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $github); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $github, $size); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $github, $size, $path); + } + + private function findBuildPath(Device $deviceForBuilds, string $projectId, Document $deployment): string + { + $path = (string) $deployment->getAttribute('buildPath', ''); + if ($path !== '' && $deviceForBuilds->exists($path)) { + return $path; + } + + $files = $deviceForBuilds->getFiles(Job::outputDirectory($projectId, $deployment->getId())); + + return \count($files) === 1 ? (string) $files[0] : ''; } /** @@ -237,6 +253,7 @@ class Jobs extends Action UsagePublisher $publisherForUsage, GitHub $github, int $buildSize = 0, + string $buildPath = '', ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -252,6 +269,7 @@ class Jobs extends Action 'buildLogs' => $this->truncate($logs . $trailer), ]; if ($success) { + $update['buildPath'] = $buildPath; $update['buildSize'] = $buildSize; $update['totalSize'] = $buildSize + (int) $deployment->getAttribute('sourceSize', 0); } 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 79ef3d2b1d..40bdbe8e8e 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php @@ -100,14 +100,19 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } + $contentType = $type === 'source' ? 'application/gzip' : 'application/octet-stream'; + $filename = $type === 'source' + ? $deploymentId . '-source.tar.gz' + : $deploymentId . '-output-' . \basename($path); + $size = $device->getFileSize($path); $rangeHeader = $request->getHeaderLine('range'); $response - ->setContentType('application/gzip') + ->setContentType($contentType) ->addHeader('Cache-Control', 'private, max-age=3888000') // 45 days ->addHeader('X-Peak', \memory_get_peak_usage()) - ->addHeader('Content-Disposition', 'attachment; filename="' . $deploymentId . '-' . $type . '.tar.gz"'); + ->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); if (!empty($rangeHeader)) { $start = $request->getRangeStart(); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index a16ad62593..34512a398d 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -365,14 +365,11 @@ trait Deployment // Build function deployments on the jobs-service when the caller // opts in ($jobs) and _APP_BUILDS_BACKEND=orchestrator. Sites stay - // on the executor. On jobs the deployment is pre-declared 'waiting' - // with its buildPath so build.sh writes onto the mounted volume. + // on the executor. $useJobs = $jobs !== null && $resourceCollection === 'functions' && System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator'; - $buildFields = $useJobs - ? ['status' => 'waiting', 'buildPath' => Job::buildPath($projectId, $deploymentId)] - : []; + $buildFields = $useJobs ? ['status' => 'waiting'] : []; $deployment = $authorization->skip(fn () => $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index c09b95b6f0..bdc93700af 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -550,8 +550,7 @@ class Functions extends Action } $source = $deployment->getAttribute('buildPath', ''); - $extension = str_ends_with($source, '.tar') ? 'tar' : 'tar.gz'; - $command = $version === 'v2' ? '' : "cp /tmp/code.$extension /mnt/code/code.$extension && nohup helpers/start.sh \"$command\""; + $command = $version === 'v2' ? '' : "cp /tmp/code.* /mnt/code/ && nohup helpers/start.sh \"$command\""; try { $executionResponse = $executor->createExecution( projectId: $project->getId(), diff --git a/tests/e2e/Services/Functions/FunctionsConsoleClientTest.php b/tests/e2e/Services/Functions/FunctionsConsoleClientTest.php index 8f8e9e7f35..e350b500de 100644 --- a/tests/e2e/Services/Functions/FunctionsConsoleClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsConsoleClientTest.php @@ -899,7 +899,7 @@ final class FunctionsConsoleClientTest extends Scope $response = $this->getDeploymentDownload($functionId, $deploymentId, 'output'); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals('application/gzip', $response['headers']['content-type']); + $this->assertEquals('application/octet-stream', $response['headers']['content-type']); $this->assertGreaterThan(0, $response['headers']['content-length']); $this->assertGreaterThan(0, \strlen($response['body'])); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 88e50ec7b4..177c875d86 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -2341,7 +2341,7 @@ final class SitesCustomServerTest extends Scope $response = $this->getDeploymentDownload($siteId, $deploymentId, 'output'); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals('application/gzip', $response['headers']['content-type']); + $this->assertEquals('application/octet-stream', $response['headers']['content-type']); $this->assertGreaterThan(0, $response['headers']['content-length']); $this->assertGreaterThan(0, \strlen($response['body'])); diff --git a/tests/unit/Functions/JobsTest.php b/tests/unit/Functions/JobsTest.php new file mode 100644 index 0000000000..0fb1367b99 --- /dev/null +++ b/tests/unit/Functions/JobsTest.php @@ -0,0 +1,65 @@ +createMock(Device::class); + $device->expects($this->once())->method('exists')->with($path)->willReturn(true); + $device->expects($this->never())->method('getFiles'); + + $this->assertSame($path, $this->findBuildPath($device, new Document([ + '$id' => 'deployment', + 'buildPath' => $path, + ]))); + } + + public function testFindBuildPathDiscoversOpaqueArtifactPath(): void + { + $path = '/storage/builds/app-project/deployment/code.sqfs'; + $device = $this->createMock(Device::class); + $device->expects($this->never())->method('exists'); + $device + ->expects($this->once()) + ->method('getFiles') + ->with('/storage/builds/app-project/deployment') + ->willReturn([$path]); + + $this->assertSame($path, $this->findBuildPath($device, new Document([ + '$id' => 'deployment', + 'buildPath' => '', + ]))); + } + + public function testFindBuildPathRejectsAmbiguousOutput(): void + { + $device = $this->createMock(Device::class); + $device + ->expects($this->once()) + ->method('getFiles') + ->willReturn(['/build/code.sqfs', '/build/code.next']); + + $this->assertSame('', $this->findBuildPath($device, new Document([ + '$id' => 'deployment', + 'buildPath' => '', + ]))); + } + + private function findBuildPath(Device $device, Document $deployment): string + { + $method = new ReflectionMethod(Jobs::class, 'findBuildPath'); + + return $method->invoke(new Jobs(), $device, 'project', $deployment); + } +} From 36155d86d1675886c6c44e32da67240e54260d93 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 16 Jul 2026 11:40:50 +0100 Subject: [PATCH 2/2] Declare the build artifact path from its compression instead of discovering it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The artifact's filename is a pure function of the compression the job is told to use (none → code.tar, squashfs → code.sqfs, default gzip → code.tar.gz), so the payload now passes OPEN_RUNTIMES_BUILD_COMPRESSION (_APP_COMPUTE_BUILD_COMPRESSION, default gzip) and buildPath is declared at submission again — outputDirectory() plus the deterministic name. Post-build discovery via Device::getFiles() is dropped: its return shape differs per device (Local returns a path list, the S3 family returns the raw ListObjectsV2 response), so on S3-backed builds storage every completed build would have counted response metadata and finalized as "must produce exactly one output artifact". A declared path keeps the worker to a plain exists/size check that behaves identically on every device, and the strategy override (e.g. Cloud's S3 artifacts) can reuse artifact() to name its upload. Co-Authored-By: Claude Fable 5 --- .../Deployment/Backend/Orchestrator.php | 27 ++++++++ .../Modules/Functions/Workers/Jobs.php | 23 ++----- .../Modules/Functions/Workers/JobsTest.php | 65 ------------------- 3 files changed, 31 insertions(+), 84 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Functions/Workers/JobsTest.php diff --git a/src/Appwrite/Deployment/Backend/Orchestrator.php b/src/Appwrite/Deployment/Backend/Orchestrator.php index e62ac5a2ac..a003b036a0 100644 --- a/src/Appwrite/Deployment/Backend/Orchestrator.php +++ b/src/Appwrite/Deployment/Backend/Orchestrator.php @@ -84,6 +84,7 @@ readonly class Orchestrator extends Backend $deployment = $this->dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'status' => 'waiting', + 'buildPath' => static::buildPath($this->project->getId(), $deployment->getId()), ])); $this->jobs->create(...static::payload($this->project, $resource, $deployment, $this->platform, $source)); @@ -165,6 +166,7 @@ readonly class Orchestrator extends Backend $command = $deployment->getAttribute('buildCommands', ''); $env = self::variables($project, $function, $deployment, $runtime, $cpus, $memory, $endpoint, $timeout) + [ 'OPEN_RUNTIMES_BUILD_INPUT_DIR' => '/mnt/code/source', + 'OPEN_RUNTIMES_BUILD_COMPRESSION' => static::compression(), ] + $output['environment']; return [ @@ -218,6 +220,31 @@ readonly class Orchestrator extends Backend return APP_STORAGE_BUILDS . "/app-{$projectId}/{$deploymentId}"; } + /** + * The build output path on the builds volume, declared at submission. + */ + public static function buildPath(string $projectId, string $deploymentId): string + { + return static::outputDirectory($projectId, $deploymentId) . '/' . static::artifact(); + } + + /** + * The artifact filename build.sh produces for the configured compression. + */ + public static function artifact(): string + { + return match (static::compression()) { + 'none' => 'code.tar', + 'squashfs' => 'code.sqfs', + default => 'code.tar.gz', + }; + } + + protected static function compression(): string + { + return System::getEnv('_APP_COMPUTE_BUILD_COMPRESSION', 'gzip'); + } + /** * Deterministic build-cache key, shared across a function's deployments so * package-manager caches (npm/yarn/pnpm) survive between builds. diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 2f90e11f2e..a70960588a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\Functions\Workers; -use Appwrite\Deployment\Backend\Orchestrator; use Appwrite\Event\Event; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Jobs as JobsMessage; @@ -266,9 +265,9 @@ class Jobs extends Action Device $deviceForBuilds, VcsFactory $vcsFactory, ): Document { - $path = $this->findBuildPath($deviceForBuilds, $project->getId(), $deployment); - if ($path === '') { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build must produce exactly one output artifact.', $usage, $publisherForUsage, $vcsFactory); + $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); } $size = $deviceForBuilds->getFileSize($path); @@ -280,19 +279,7 @@ class Jobs extends Action 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, true, '', $usage, $publisherForUsage, $vcsFactory, $size, $path); - } - - private function findBuildPath(Device $deviceForBuilds, string $projectId, Document $deployment): string - { - $path = (string) $deployment->getAttribute('buildPath', ''); - if ($path !== '' && $deviceForBuilds->exists($path)) { - return $path; - } - - $files = $deviceForBuilds->getFiles(Orchestrator::outputDirectory($projectId, $deployment->getId())); - - return \count($files) === 1 ? (string) $files[0] : ''; + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size); } /** @@ -311,7 +298,6 @@ class Jobs extends Action UsagePublisher $publisherForUsage, VcsFactory $vcsFactory, int $buildSize = 0, - string $buildPath = '', ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -327,7 +313,6 @@ class Jobs extends Action 'buildLogs' => $this->truncate($logs . $trailer), ]; if ($success) { - $update['buildPath'] = $buildPath; $update['buildSize'] = $buildSize; $update['totalSize'] = $buildSize + (int) $deployment->getAttribute('sourceSize', 0); } diff --git a/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php b/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php deleted file mode 100644 index db6fa07d16..0000000000 --- a/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php +++ /dev/null @@ -1,65 +0,0 @@ -createMock(Device::class); - $device->expects($this->once())->method('exists')->with($path)->willReturn(true); - $device->expects($this->never())->method('getFiles'); - - $this->assertSame($path, $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => $path, - ]))); - } - - public function testFindBuildPathDiscoversOpaqueArtifactPath(): void - { - $path = '/storage/builds/app-project/deployment/code.sqfs'; - $device = $this->createMock(Device::class); - $device->expects($this->never())->method('exists'); - $device - ->expects($this->once()) - ->method('getFiles') - ->with('/storage/builds/app-project/deployment') - ->willReturn([$path]); - - $this->assertSame($path, $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => '', - ]))); - } - - public function testFindBuildPathRejectsAmbiguousOutput(): void - { - $device = $this->createMock(Device::class); - $device - ->expects($this->once()) - ->method('getFiles') - ->willReturn(['/build/code.sqfs', '/build/code.next']); - - $this->assertSame('', $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => '', - ]))); - } - - private function findBuildPath(Device $device, Document $deployment): string - { - $method = new ReflectionMethod(Jobs::class, 'findBuildPath'); - - return $method->invoke(new Jobs(), $device, 'project', $deployment); - } -}