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); - } -}