This commit is contained in:
Andras Bacsai
2023-05-30 15:52:17 +02:00
parent 6e72889294
commit 0f50d1accd
36 changed files with 715 additions and 273 deletions

View File

@@ -1,17 +1,22 @@
<?php
use App\Jobs\ApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
function queue_application_deployment(Application $application, $extra_attributes)
function queue_application_deployment(int $application_id, string $deployment_uuid, int|null $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false)
{
ray('Queuing deployment: ' . $deployment_uuid . ' of applicationID: ' . $application_id . ' pull request: ' . $pull_request_id . ' with commit: ' . $commit . ' and is it forced: ' . $force_rebuild);
$deployment = ApplicationDeploymentQueue::create([
'application_id' => $application->id,
'extra_attributes' => $extra_attributes,
'application_id' => $application_id,
'deployment_uuid' => $deployment_uuid,
'pull_request_id' => $pull_request_id,
'force_rebuild' => $force_rebuild,
'commit' => $commit,
]);
$queued_deployments = ApplicationDeploymentQueue::where('application_id', $application->id)->where('status', 'queued')->get()->sortByDesc('created_at');
$running_deployments = ApplicationDeploymentQueue::where('application_id', $application->id)->where('status', 'in_progress')->get()->sortByDesc('created_at');
$queued_deployments = ApplicationDeploymentQueue::where('application_id', $application_id)->where('status', 'queued')->get()->sortByDesc('created_at');
$running_deployments = ApplicationDeploymentQueue::where('application_id', $application_id)->where('status', 'in_progress')->get()->sortByDesc('created_at');
ray('Queued deployments: ' . $queued_deployments->count());
ray('Running deployments: ' . $running_deployments->count());
if ($queued_deployments->count() > 1) {
$queued_deployments = $queued_deployments->skip(1);
$queued_deployments->each(function ($queued_deployment, $key) {
@@ -24,9 +29,10 @@ function queue_application_deployment(Application $application, $extra_attribute
}
dispatch(new ApplicationDeploymentJob(
application_deployment_queue_id: $deployment->id,
deployment_uuid: $extra_attributes['deployment_uuid'],
application_uuid: $extra_attributes['application_uuid'],
force_rebuild: $extra_attributes['force_rebuild'],
commit: $extra_attributes['commit'] ?? null,
application_id: $application_id,
deployment_uuid: $deployment_uuid,
force_rebuild: $force_rebuild,
rollback_commit: $commit,
pull_request_id: $pull_request_id,
));
}

View File

@@ -38,3 +38,12 @@ function get_container_status(Server $server, string $container_id, bool $throwE
$container = format_docker_command_output_to_json($container);
return $container[0]['Status'];
}
function generate_container_name(string $uuid, int|null $pull_request_id = null)
{
if ($pull_request_id) {
return $uuid . '_pr_' . $pull_request_id;
} else {
return $uuid;
}
}

View File

@@ -1,6 +1,7 @@
<?php
use App\Models\GithubApp;
use App\Models\GitlabApp;
use Illuminate\Support\Facades\Http;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
@@ -45,3 +46,20 @@ function generate_github_jwt_token(GithubApp $source)
->toString();
return $issuedToken;
}
function get_from_git_api(GithubApp|GitlabApp $source, $endpoint)
{
if ($source->getMorphClass() == 'App\Models\GithubApp') {
if ($source->is_public) {
$response = Http::github($source->api_url)->get($endpoint);
}
}
$json = $response->json();
if ($response->status() !== 200) {
throw new \Exception("Failed to get data from {$source->name} with error: " . $json['message']);
}
return [
'rate_limit_remaining' => $response->header('X-RateLimit-Remaining'),
'data' => collect($json)
];
}

View File

@@ -85,7 +85,7 @@ function instant_remote_process(array $command, Server $server, $throwError = tr
$output = trim($process->output());
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
Log::info($process->errorOutput());
ray($process->errorOutput());
if (!$throwError) {
return null;
}

View File

@@ -9,6 +9,7 @@ use Illuminate\Support\Str;
function general_error_handler(\Throwable $e, $that = null, $isJson = false)
{
try {
ray('ERROR OCCURED: ' . $e->getMessage());
if ($e instanceof QueryException) {
if ($e->errorInfo[0] === '23505') {
throw new \Exception('Duplicate entry found.', '23505');
@@ -29,7 +30,7 @@ function general_error_handler(\Throwable $e, $that = null, $isJson = false)
'error' => $error->getMessage(),
]);
} else {
// dump($error);
ray($error);
}
}
}