1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00

Fix failing builds

This commit is contained in:
Matej Bačo 2023-06-28 13:31:35 +02:00
parent c6e491f2ed
commit 3a8f4e5039
5 changed files with 61 additions and 17 deletions

View file

@ -1311,7 +1311,7 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId')
throw new Exception(Exception::BUILD_NOT_FOUND); throw new Exception(Exception::BUILD_NOT_FOUND);
} }
// TODO: Somehow set commit SHA for git deployments, and file path for manual. Redeploy should use exact same source code // TODO: Somehow set commit SHA & ownerName & repoName for git deployments, and file path for manual. Redeploy should use exact same source code
$installation = $dbForConsole->getDocument('vcsInstallations', $deployment->getAttribute('vcsInstallationId', '')); $installation = $dbForConsole->getDocument('vcsInstallations', $deployment->getAttribute('vcsInstallationId', ''));

View file

@ -508,9 +508,9 @@ $createGitDeployments = function (GitHub $github, string $installationId, array
$latestCommentId = \strval($github->createComment($owner, $repositoryName, $pullRequest, $comment->generateComment())); $latestCommentId = \strval($github->createComment($owner, $repositoryName, $pullRequest, $comment->generateComment()));
} elseif (!empty($branchName)) { } elseif (!empty($branchName)) {
$gitPullRequest = $github->getBranchPullRequest($owner, $repositoryName, $branchName); $gitPullRequest = $github->getBranchPullRequest($owner, $repositoryName, $branchName);
$pullRequestId = \strval($gitPullRequest['number'] ?? ''); $pullRequest = \strval($gitPullRequest['number'] ?? '');
if (!empty($pullRequestId)) { if (!empty($pullRequest)) {
$latestCommentId = \strval($github->createComment($owner, $repositoryName, $pullRequestId, $comment->generateComment())); $latestCommentId = \strval($github->createComment($owner, $repositoryName, $pullRequest, $comment->generateComment()));
} }
} }
@ -596,10 +596,19 @@ $createGitDeployments = function (GitHub $github, string $installationId, array
$github->updateCommitStatus($repositoryName, $SHA, $owner, 'pending', $message, $targetUrl, $name); $github->updateCommitStatus($repositoryName, $SHA, $owner, 'pending', $message, $targetUrl, $name);
} }
$contribution = new Document([]);
if($external) {
$pullRequestResponse = $github->getPullRequest($owner, $repositoryName, $pullRequest);
$contribution->setAttribute('ownerName', $pullRequestResponse['head']['repo']['owner']['login']);
$contribution->setAttribute('repositoryName', $pullRequestResponse['head']['repo']['name']);
}
$buildEvent = new Build(); $buildEvent = new Build();
$buildEvent $buildEvent
->setType(BUILD_TYPE_DEPLOYMENT) ->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($function) ->setResource($function)
->setVcsContribution($contribution)
->setDeployment($deployment) ->setDeployment($deployment)
->setTargetUrl($targetUrl) ->setTargetUrl($targetUrl)
->setSHA($SHA) ->setSHA($SHA)
@ -642,7 +651,6 @@ App::post('/v1/vcs/github/incomingwebhook')
$repositoryId = $parsedPayload["repositoryId"]; $repositoryId = $parsedPayload["repositoryId"];
$installationId = $parsedPayload["installationId"]; $installationId = $parsedPayload["installationId"];
$SHA = $parsedPayload["SHA"]; $SHA = $parsedPayload["SHA"];
$owner = $parsedPayload["owner"];
$github->initialiseVariables($installationId, $privateKey, $githubAppId); $github->initialiseVariables($installationId, $privateKey, $githubAppId);
@ -685,6 +693,11 @@ App::post('/v1/vcs/github/incomingwebhook')
$SHA = $parsedPayload["SHA"]; $SHA = $parsedPayload["SHA"];
$external = $parsedPayload["external"]; $external = $parsedPayload["external"];
// Ignore sync for non-external. We handle it in push webhook
if(!$external && $parsedPayload["action"] == "synchronize") {
return $response->json($parsedPayload);
}
$github->initialiseVariables($installationId, $privateKey, $githubAppId); $github->initialiseVariables($installationId, $privateKey, $githubAppId);
$vcsRepos = $dbForConsole->find('vcsRepos', [ $vcsRepos = $dbForConsole->find('vcsRepos', [

View file

@ -51,13 +51,14 @@ class BuildsV1 extends Worker
$template = new Document($this->args['template'] ?? []); $template = new Document($this->args['template'] ?? []);
$SHA = $this->args['SHA'] ?? ''; $SHA = $this->args['SHA'] ?? '';
$targetUrl = $this->args['targetUrl'] ?? ''; $targetUrl = $this->args['targetUrl'] ?? '';
$vcsContribution = new Document($this->args['vcsContribution'] ?? []);
switch ($type) { switch ($type) {
case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_DEPLOYMENT:
case BUILD_TYPE_RETRY: case BUILD_TYPE_RETRY:
Console::info('Creating build for deployment: ' . $deployment->getId()); Console::info('Creating build for deployment: ' . $deployment->getId());
$github = new GitHub($this->getCache()); $github = new GitHub($this->getCache());
$this->buildDeployment($github, $project, $resource, $deployment, $template, $SHA, $targetUrl); $this->buildDeployment($github, $project, $resource, $deployment, $template, $SHA, $targetUrl, $vcsContribution);
break; break;
default: default:
@ -66,7 +67,7 @@ class BuildsV1 extends Worker
} }
} }
protected function buildDeployment(GitHub $github, Document $project, Document $function, Document $deployment, Document $template, string $SHA = '', string $targetUrl = '') protected function buildDeployment(GitHub $github, Document $project, Document $function, Document $deployment, Document $template, string $SHA = '', string $targetUrl = '', Document $vcsContribution = null)
{ {
global $register; global $register;
@ -165,10 +166,15 @@ class BuildsV1 extends Worker
$rootDirectory = \ltrim($rootDirectory, '/'); $rootDirectory = \ltrim($rootDirectory, '/');
$github->initialiseVariables($installationId, $privateKey, $githubAppId); $github->initialiseVariables($installationId, $privateKey, $githubAppId);
$owner = $github->getOwnerName($installationId); $owner = $github->getOwnerName($installationId);
$repositoryName = $github->getRepositoryName($repositoryId); $repositoryName = $github->getRepositoryName($repositoryId);
$cloneOwner = !empty($vcsContribution) ? $vcsContribution->getAttribute('ownerName', $owner) : $owner;
$cloneRepository = !empty($vcsContribution) ? $vcsContribution->getAttribute('repositoryName', $repositoryName) : $repositoryName;
$branchName = $deployment->getAttribute('vcsBranch'); $branchName = $deployment->getAttribute('vcsBranch');
$gitCloneCommand = $github->generateGitCloneCommand($owner, $repositoryName, $branchName, $tmpDirectory, $rootDirectory); $gitCloneCommand = $github->generateGitCloneCommand($cloneOwner, $cloneRepository, $branchName, $tmpDirectory, $rootDirectory);
$stdout = ''; $stdout = '';
$stderr = ''; $stderr = '';
Console::execute('mkdir -p /tmp/builds/' . $buildId, '', $stdout, $stderr); Console::execute('mkdir -p /tmp/builds/' . $buildId, '', $stdout, $stderr);
@ -522,7 +528,6 @@ class BuildsV1 extends Worker
$github->updateCommitStatus($repositoryName, $SHA, $owner, $state, $message, $targetUrl, $name); $github->updateCommitStatus($repositoryName, $SHA, $owner, $state, $message, $targetUrl, $name);
} }
// TODO: Fix race condition
if (!empty($commentId)) { if (!empty($commentId)) {
$retries = 0; $retries = 0;
@ -543,13 +548,24 @@ class BuildsV1 extends Worker
\sleep(1); \sleep(1);
} }
// Wrap in try/catch to ensure lock file gets deleted
$error = null;
try {
$comment = new Comment(); $comment = new Comment();
$comment->parseComment($github->getComment($owner, $repositoryName, $commentId)); $comment->parseComment($github->getComment($owner, $repositoryName, $commentId));
$comment->addBuild($project, $function, $status, $deployment->getId()); $comment->addBuild($project, $function, $status, $deployment->getId());
$github->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); $github->updateComment($owner, $repositoryName, $commentId, $comment->generateComment());
} catch (\Exception $e) {
$error = $e;
} finally {
$dbForConsole->deleteDocument('vcsCommentLocks', $commentId); $dbForConsole->deleteDocument('vcsCommentLocks', $commentId);
} }
if (!empty($error)) {
throw $error;
}
}
} }
public function shutdown(): void public function shutdown(): void

4
composer.lock generated
View file

@ -2705,7 +2705,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/vcs.git", "url": "https://github.com/utopia-php/vcs.git",
"reference": "80d1fa3022c20c6b391473c22265760a8551c5ed" "reference": "580474fcbc5a88a908a1df3e124e47ebad2c014f"
}, },
"require": { "require": {
"adhocore/jwt": "^1.1", "adhocore/jwt": "^1.1",
@ -2750,7 +2750,7 @@
"utopia", "utopia",
"vcs" "vcs"
], ],
"time": "2023-06-28T08:07:04+00:00" "time": "2023-06-28T10:54:07+00:00"
}, },
{ {
"name": "utopia-php/websocket", "name": "utopia-php/websocket",

View file

@ -13,6 +13,7 @@ class Build extends Event
protected ?Document $template = null; protected ?Document $template = null;
protected string $SHA = ''; protected string $SHA = '';
protected string $targetUrl = ''; protected string $targetUrl = '';
protected ?Document $vcsContribution = null;
public function __construct() public function __construct()
{ {
@ -71,6 +72,19 @@ class Build extends Event
return $this; return $this;
} }
/**
* Sets custom owner and repository for VCS clone command during build
*
* @param Document $owner
* @return self
*/
public function setVcsContribution(Document $vcsContribution): self
{
$this->vcsContribution = $vcsContribution;
return $this;
}
/** /**
* Returns set resource document for the build event. * Returns set resource document for the build event.
* *
@ -142,7 +156,8 @@ class Build extends Event
'type' => $this->type, 'type' => $this->type,
'template' => $this->template, 'template' => $this->template,
'SHA' => $this->SHA, 'SHA' => $this->SHA,
'targetUrl' => $this->targetUrl 'targetUrl' => $this->targetUrl,
'vcsContribution' => $this->vcsContribution
]); ]);
} }
} }