mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-22 11:12:20 +00:00
* Add notification for deployment completion Add notification for deployment completion status. * Create `DeploymentCompleted` notification class in `app/Notifications/DeploymentCompleted.php` to handle deployment completion notifications. * Update `app/Actions/Site/Deploy.php` to send `DeploymentCompleted` notification using `Notifier` when a deployment completes or fails. * Import `Notifier` and `DeploymentCompleted` classes in `app/Actions/Site/Deploy.php`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/vitodeploy/vito?shareId=XXXX-XXXX-XXXX-XXXX). * Format with pint * Add tests * Pint format * Delete tests/Feature/Notifications/DeploymentCompletedTest.php * Delete tests/Unit/Notifications/DeploymentCompletedTest.php * 🍻🍻 * tests --------- Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com> Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Site;
|
|
|
|
use App\Enums\DeploymentStatus;
|
|
use App\Exceptions\DeploymentScriptIsEmptyException;
|
|
use App\Exceptions\SSHError;
|
|
use App\Facades\Notifier;
|
|
use App\Models\Deployment;
|
|
use App\Models\ServerLog;
|
|
use App\Models\Site;
|
|
use App\Notifications\DeploymentCompleted;
|
|
|
|
class Deploy
|
|
{
|
|
/**
|
|
* @throws DeploymentScriptIsEmptyException
|
|
* @throws SSHError
|
|
*/
|
|
public function run(Site $site): Deployment
|
|
{
|
|
if ($site->sourceControl) {
|
|
$site->sourceControl->getRepo($site->repository);
|
|
}
|
|
|
|
if (! $site->deploymentScript?->content) {
|
|
throw new DeploymentScriptIsEmptyException;
|
|
}
|
|
|
|
$deployment = new Deployment([
|
|
'site_id' => $site->id,
|
|
'deployment_script_id' => $site->deploymentScript->id,
|
|
'status' => DeploymentStatus::DEPLOYING,
|
|
]);
|
|
$lastCommit = $site->sourceControl?->provider()?->getLastCommit($site->repository, $site->branch);
|
|
if ($lastCommit) {
|
|
$deployment->commit_id = $lastCommit['commit_id'];
|
|
$deployment->commit_data = $lastCommit['commit_data'];
|
|
}
|
|
$deployment->save();
|
|
|
|
dispatch(function () use ($site, $deployment) {
|
|
/** @var ServerLog $log */
|
|
$log = ServerLog::make($site->server, 'deploy-'.strtotime('now'))
|
|
->forSite($site);
|
|
$log->save();
|
|
$deployment->log_id = $log->id;
|
|
$deployment->save();
|
|
$site->server->os()->runScript(
|
|
path: $site->path,
|
|
script: $site->deploymentScript->content,
|
|
serverLog: $log,
|
|
user: $site->user,
|
|
variables: $site->environmentVariables($deployment),
|
|
);
|
|
$deployment->status = DeploymentStatus::FINISHED;
|
|
$deployment->save();
|
|
Notifier::send($site, new DeploymentCompleted($deployment, $site));
|
|
})->catch(function () use ($deployment, $site) {
|
|
$deployment->status = DeploymentStatus::FAILED;
|
|
$deployment->save();
|
|
Notifier::send($site, new DeploymentCompleted($deployment, $site));
|
|
})->onConnection('ssh');
|
|
|
|
return $deployment;
|
|
}
|
|
}
|