Merge pull request #10820 from appwrite/ser-550

Update deploymentId in sites rule for first deployment
This commit is contained in:
Matej Bačo 2025-11-25 09:50:16 +01:00 committed by GitHub
commit 3d8cee4676
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 204 additions and 24 deletions

View file

@ -5,6 +5,7 @@ use Appwrite\Event\Build;
use Appwrite\Event\Delete;
use Appwrite\Extend\Exception;
use Appwrite\Network\Validator\Redirect;
use Appwrite\Platform\Modules\Compute\Base as ComputeBase;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
@ -433,6 +434,8 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId
}
}
ComputeBase::updateEmptyManualRule($project, $resource, $deployment, $dbForPlatform);
if ($resource->getCollection() === 'sites' && !empty($latestCommentId) && !empty($previewRuleId)) {
$retries = 0;
$lockAcquired = false;

View file

@ -13,6 +13,7 @@ use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Swoole\Request;
use Utopia\System\System;
@ -52,7 +53,7 @@ class Base extends Action
return $allowedSpecifications[0] ?? APP_COMPUTE_SPECIFICATION_DEFAULT;
}
public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, Build $queueForBuilds, Document $template, GitHub $github, bool $activate, string $referenceType = 'branch', string $reference = ''): Document
public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, Build $queueForBuilds, Document $template, GitHub $github, bool $activate, string $referenceType = 'branch', string $reference = ''): Document
{
$deploymentId = ID::unique();
$entrypoint = $function->getAttribute('entrypoint', '');
@ -133,6 +134,8 @@ class Base extends Action
->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', ''));
$dbForProject->updateDocument('functions', $function->getId(), $function);
$this->updateEmptyManualRule($project, $function, $deployment, $dbForPlatform);
$queueForBuilds
->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($function)
@ -327,6 +330,8 @@ class Base extends Action
}
}
$this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform);
$queueForBuilds
->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($site)
@ -335,4 +340,34 @@ class Base extends Action
return $deployment;
}
/**
* 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.
*
* @param \Utopia\Database\Document $project
* @param \Utopia\Database\Document $resource
* @param \Utopia\Database\Document $deployment
* @param \Utopia\Database\Database $dbForPlatform
* @return void
*/
public static function updateEmptyManualRule(Document $project, Document $resource, Document $deployment, Database $dbForPlatform)
{
$resourceType = $resource->getCollection() === 'sites' ? 'site' : 'function';
$queries = [
Query::equal('projectInternalId', [$project->getSequence()]),
Query::equal('deploymentResourceInternalId', [$resource->getSequence()]),
Query::equal('deploymentResourceType', [$resourceType]),
Query::equal('deploymentId', ['']),
Query::equal('type', ['deployment']),
Query::equal('trigger', ['manual']),
];
$dbForPlatform->forEach('rules', function (Document $rule) use ($deployment, $dbForPlatform) {
Authorization::skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([
'deploymentId' => $deployment->getId(),
'deploymentInternalId' => $deployment->getSequence(),
])));
}, $queries);
}
}

View file

@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Functions\Http\Deployments;
use Appwrite\Event\Build;
use Appwrite\Event\Event;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Compute\Base;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
@ -31,7 +32,7 @@ use Utopia\Validator\Boolean;
use Utopia\Validator\Nullable;
use Utopia\Validator\Text;
class Create extends Action
class Create extends Base
{
use HTTP;
@ -82,6 +83,7 @@ class Create extends Action
->inject('request')
->inject('response')
->inject('dbForProject')
->inject('dbForPlatform')
->inject('queueForEvents')
->inject('project')
->inject('deviceForFunctions')
@ -100,6 +102,7 @@ class Create extends Action
Request $request,
Response $response,
Database $dbForProject,
Database $dbForPlatform,
Event $queueForEvents,
Document $project,
Device $deviceForFunctions,
@ -301,6 +304,8 @@ class Create extends Action
}
}
$this->updateEmptyManualRule($project, $function, $deployment, $dbForPlatform);
$metadata = null;
$queueForEvents

View file

@ -124,6 +124,7 @@ class Create extends Base
project: $project,
installation: $installation,
dbForProject: $dbForProject,
dbForPlatform: $dbForPlatform,
queueForBuilds: $queueForBuilds,
template: $template,
github: $github,
@ -170,6 +171,9 @@ class Create extends Base
->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', ''));
$dbForProject->updateDocument('functions', $function->getId(), $function);
$this->updateEmptyManualRule($project, $function, $deployment, $dbForPlatform);
$queueForBuilds
->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($function)

View file

@ -105,6 +105,7 @@ class Create extends Base
project: $project,
installation: $installation,
dbForProject: $dbForProject,
dbForPlatform: $dbForPlatform,
queueForBuilds: $queueForBuilds,
template: $template,
github: $github,

View file

@ -305,12 +305,14 @@ class Create extends Base
$template = new Document();
$installation = $dbForPlatform->getDocument('installations', $function->getAttribute('installationId'));
// TODO: Is this still needed? Can this be removed?
$deployment = $this->redeployVcsFunction(
request: $request,
function: $function,
project: $project,
installation: $installation,
dbForProject: $dbForProject,
dbForPlatform: $dbForPlatform,
queueForBuilds: $queueForBuilds,
template: $template,
github: $github,

View file

@ -273,7 +273,7 @@ class Update extends Base
// Redeploy logic
if (!$isConnected && !empty($providerRepositoryId)) {
$this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $queueForBuilds, new Document(), $github, true);
$this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $dbForPlatform, $queueForBuilds, new Document(), $github, true);
}
// Inform scheduler if function is still active

View file

@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Sites\Http\Deployments;
use Appwrite\Event\Build;
use Appwrite\Event\Event;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Compute\Base;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
@ -32,7 +33,7 @@ use Utopia\Validator\Boolean;
use Utopia\Validator\Nullable;
use Utopia\Validator\Text;
class Create extends Action
class Create extends Base
{
use HTTP;
@ -365,6 +366,8 @@ class Create extends Action
}
}
$this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform);
$metadata = null;
$queueForEvents

View file

@ -208,6 +208,8 @@ class Create extends Base
]))
);
$this->updateEmptyManualRule($project, $site, $deployment, $dbForPlatform);
$queueForBuilds
->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($site)

View file

@ -272,7 +272,7 @@ class Update extends Base
// Redeploy logic
if (!$isConnected && !empty($providerRepositoryId)) {
$this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $queueForBuilds, new Document(), $github, true);
$this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $dbForPlatform, $queueForBuilds, new Document(), $github, true);
}
$queueForEvents->setParam('siteId', $site->getId());

View file

@ -432,4 +432,27 @@ trait FunctionsBase
return $specifications;
}
protected function createFunctionRule(string $functionId, string $domain): mixed
{
$rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/function', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => $functionId,
'domain' => $domain,
]);
return $rule;
}
protected function getFunctionRule(string $ruleId): mixed
{
$rule = $this->client->call(Client::METHOD_GET, '/proxy/rules/' . $ruleId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $rule;
}
}

View file

@ -14,6 +14,7 @@ use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Query;
use Utopia\Database\Validator\Datetime as DatetimeValidator;
use Utopia\System\System;
class FunctionsCustomServerTest extends Scope
{
@ -392,6 +393,10 @@ class FunctionsCustomServerTest extends Scope
$functionId = $function['body']['$id'] ?? '';
$domain = ID::unique() . '.' . System::getEnv('_APP_DOMAIN_FUNCTIONS', '');
$rule = $this->createFunctionRule($functionId, $domain);
$this->assertEquals(201, $rule['headers']['status-code']);
$deployment = $this->createTemplateDeployment(
$functionId,
[
@ -408,6 +413,20 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(202, $deployment['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']);
$rule = $this->getFunctionRule($rule['body']['$id']);
$this->assertEquals(200, $rule['headers']['status-code']);
$this->assertEquals($deployment['body']['$id'], $rule['body']['deploymentId']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString("Deployment is still building", $response['body']);
$this->assertStringContainsString("The page will update after the build completes.", $response['body']);
$deployment = $this->getDeployment($functionId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
// Wait for deployment to be ready
$deploymentId = $deployment['body']['$id'];
$this->assertEventually(function () use ($functionId, $deploymentId) {
@ -417,7 +436,6 @@ class FunctionsCustomServerTest extends Scope
// Verify deployment sizes
$deployment = $this->getDeployment($functionId, $deploymentId);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertGreaterThan(0, $deployment['body']['sourceSize']);
$this->assertGreaterThan(0, $deployment['body']['buildSize']);
$totalSize = $deployment['body']['sourceSize'] + $deployment['body']['buildSize'];
@ -662,6 +680,10 @@ class FunctionsCustomServerTest extends Scope
*/
$functionId = $data['functionId'];
$domain = ID::unique() . '.' . System::getEnv('_APP_DOMAIN_FUNCTIONS', '');
$rule = $this->createFunctionRule($functionId, $domain);
$this->assertEquals(201, $rule['headers']['status-code']);
$deployment = $this->createDeployment($functionId, [
'code' => $this->packageFunction('basic'),
'activate' => true
@ -673,6 +695,18 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(true, (new DatetimeValidator())->isValid($deployment['body']['$createdAt']));
$this->assertEquals('index.js', $deployment['body']['entrypoint']);
$rule = $this->getFunctionRule($rule['body']['$id']);
$this->assertEquals(200, $rule['headers']['status-code']);
$this->assertEquals($deployment['body']['$id'], $rule['body']['deploymentId']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString("Deployment is still building", $response['body']);
$this->assertStringContainsString('The page will update after the build completes.', $response['body']);
$deploymentIdActive = $deployment['body']['$id'] ?? '';
$this->assertEventually(function () use ($functionId, $deploymentIdActive) {
@ -2392,6 +2426,12 @@ class FunctionsCustomServerTest extends Scope
$domain = $this->setupFunctionDomain($functionId);
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertStringContainsString('No active deployments', $response['body']);
$this->assertStringContainsString('View deployments', $response['body']);
$deployment = $this->createDeployment($functionId, [
'code' => $this->packageFunction('basic'),
'activate' => true
@ -2404,11 +2444,22 @@ class FunctionsCustomServerTest extends Scope
'x-appwrite-project' => $this->getProject()['$id']
]));
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertStringContainsString('No active deployments', $response['body']);
$this->assertStringContainsString('View deployments', $response['body']);
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString('Deployment is still building', $response['body']);
$this->assertStringContainsString('The page will update after the build completes.', $response['body']);
// canceled deployment
$functionId = $this->setupFunction([
'functionId' => ID::unique(),
'name' => 'Test Error Pages',
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'timeout' => 15,
'commands' => 'cd non-existing-directory',
'execute' => ['any']
]);
$domain = $this->setupFunctionDomain($functionId);
$proxyClient->setEndpoint('http://' . $domain);
$deployment = $this->createDeployment($functionId, [
'code' => $this->packageFunction('basic'),
'activate' => true
@ -2426,9 +2477,9 @@ class FunctionsCustomServerTest extends Scope
'x-appwrite-project' => $this->getProject()['$id']
]));
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertStringContainsString('No active deployments', $response['body']);
$this->assertStringContainsString('View deployments', $response['body']);
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString('Deployment build canceled', $response['body']);
$this->assertStringContainsString('This build was canceled and won\'t be deployed.', $response['body']);
$this->cleanupFunction($functionId);
}

View file

@ -474,4 +474,27 @@ trait SitesBase
return $specifications;
}
protected function createSiteRule(string $siteId, string $domain): mixed
{
$rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/site', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'siteId' => $siteId,
'domain' => $domain,
]);
return $rule;
}
protected function getSiteRule(string $ruleId): mixed
{
$rule = $this->client->call(Client::METHOD_GET, '/proxy/rules/' . $ruleId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $rule;
}
}

View file

@ -861,6 +861,10 @@ class SitesCustomServerTest extends Scope
$this->assertNotNull($siteId);
$domain = ID::unique() . '.' . System::getEnv('_APP_DOMAIN_SITES', '');
$rule = $this->createSiteRule($siteId, $domain);
$this->assertEquals(201, $rule['headers']['status-code']);
$deployment = $this->createDeployment($siteId, [
'siteId' => $siteId,
'code' => $this->packageSite('static-single-file'),
@ -872,6 +876,18 @@ class SitesCustomServerTest extends Scope
$this->assertEquals('waiting', $deployment['body']['status']);
$this->assertEquals(true, (new DatetimeValidator())->isValid($deployment['body']['$createdAt']));
$rule = $this->getSiteRule($rule['body']['$id']);
$this->assertEquals(200, $rule['headers']['status-code']);
$this->assertEquals($deployment['body']['$id'], $rule['body']['deploymentId']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString("Deployment is still building", $response['body']);
$this->assertStringContainsString('The page will update after the build completes.', $response['body']);
$deploymentIdActive = $deployment['body']['$id'] ?? '';
$this->assertEventually(function () use ($siteId, $deploymentIdActive) {
@ -1563,6 +1579,10 @@ class SitesCustomServerTest extends Scope
$this->assertNotEmpty($siteId);
$domain = ID::unique() . '.' . System::getEnv('_APP_DOMAIN_SITES', '');
$rule = $this->createSiteRule($siteId, $domain);
$this->assertEquals(201, $rule['headers']['status-code']);
$deployment = $this->createTemplateDeployment($siteId, [
'repository' => $template['providerRepositoryId'],
'owner' => $template['providerOwner'],
@ -1575,6 +1595,18 @@ class SitesCustomServerTest extends Scope
$this->assertEquals(202, $deployment['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']);
$rule = $this->getSiteRule($rule['body']['$id']);
$this->assertEquals(200, $rule['headers']['status-code']);
$this->assertEquals($deployment['body']['$id'], $rule['body']['deploymentId']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString("Deployment is still building", $response['body']);
$this->assertStringContainsString('The page will update after the build completes.', $response['body']);
$deployment = $this->getDeployment($siteId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertEquals(0, $deployment['body']['sourceSize']);
@ -1586,10 +1618,6 @@ class SitesCustomServerTest extends Scope
$this->assertNotEmpty($site['body']['deploymentId']);
}, 50000, 500);
$domain = $this->setupSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
@ -2584,7 +2612,8 @@ class SitesCustomServerTest extends Scope
}, 100000, 500);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertStringContainsString('This page is empty, activate a deployment to make it live.', $response['body']);
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertStringContainsString('Deployment build failed', $response['body']);
$this->cleanupSite($siteId);
}
@ -2679,6 +2708,12 @@ class SitesCustomServerTest extends Scope
$this->assertNotEmpty($siteId);
$domain = $this->setupSiteDomain($siteId);
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertStringContainsString('No active deployments', $response['body']);
$this->assertStringContainsString('View deployments', $response['body']);
// test canceled deployment error page
$deployment = $this->createDeployment($siteId, [
@ -2714,13 +2749,6 @@ class SitesCustomServerTest extends Scope
$this->assertStringContainsString("Deployment build canceled", $response['body']);
$this->assertStringContainsString("View deployments", $response['body']);
// check site domain for no active deployments
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertStringContainsString('No active deployments', $response['body']);
$this->assertStringContainsString('View deployments', $response['body']);
$deployment = $this->createDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => 'true'