Merge pull request #12896 from appwrite/fix/opaque-build-artifact-name

Declare the build artifact path from its compression instead of discovering it
This commit is contained in:
Luke B. Silver authored and GitHub committed 2026-07-16 12:15:30 +01:00
commit a497648362
3 files changed
+31 -84

No files matched your search

@@ -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.
@@ -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);
}
@@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Platform\Modules\Functions\Workers;
use Appwrite\Platform\Modules\Functions\Workers\Jobs;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Utopia\Database\Document;
use Utopia\Storage\Device;
final class JobsTest extends TestCase
{
public function testFindBuildPathKeepsExistingDeploymentPath(): void
{
$path = '/storage/builds/app-project/deployment/code.tar.gz';
$device = $this->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);
}
}