Skip to content

Commit

Permalink
deployment script variables (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jun 16, 2024
1 parent eec83f5 commit 7d36746
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
7 changes: 6 additions & 1 deletion app/Actions/Site/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public function run(Site $site): Deployment
$log->save();
$deployment->log_id = $log->id;
$deployment->save();
$site->server->os()->runScript($site->path, $site->deploymentScript->content, $log);
$site->server->os()->runScript(
path: $site->path,
script: $site->deploymentScript->content,
serverLog: $log,
variables: $site->environmentVariables($deployment)
);
$deployment->status = DeploymentStatus::FINISHED;
$deployment->save();
})->catch(function () use ($deployment) {
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,17 @@ public function hasSSL(): bool
{
return $this->ssls->isNotEmpty();
}

public function environmentVariables(?Deployment $deployment = null): array
{
return [
'SITE_PATH' => $this->path,
'DOMAIN' => $this->domain,
'BRANCH' => $this->branch ?? '',
'REPOSITORY' => $this->repository ?? '',
'COMMIT_ID' => $deployment?->commit_id ?? '',
'PHP_VERSION' => $this->php_version,
'PHP_PATH' => '/usr/bin/php'.$this->php_version,
];
}
}
20 changes: 12 additions & 8 deletions app/SSH/OS/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,23 @@ public function tail(string $path, int $lines): string
);
}

public function runScript(string $path, string $script, ?ServerLog $serverLog, ?string $user = null): ServerLog
public function runScript(string $path, string $script, ?ServerLog $serverLog, ?string $user = null, ?array $variables = []): ServerLog
{
$ssh = $this->server->ssh($user);
if ($serverLog) {
$ssh->setLog($serverLog);
}
$ssh->exec(
$this->getScript('run-script.sh', [
'path' => $path,
'script' => $script,
]),
'run-script'
);
$command = '';
foreach ($variables as $key => $variable) {
$command .= "$key=$variable".PHP_EOL;
}
$command .= $this->getScript('run-script.sh', [
'path' => $path,
'script' => $script,
]);
$ssh->exec($command, 'run-script');

info($command);

return $ssh->log;
}
Expand Down
21 changes: 21 additions & 0 deletions resources/views/application/deployment-script.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ class="p-6"
@enderror
</div>

<div class="mt-6">
<div class="flex items-center">
<x-input-label class="mr-1" :value="__('Available Variables')" />
(
<a
href="https://vitodeploy.com/sites/application.html#deployment-script"
target="_blank"
class="text-primary-500"
>
{{ __("How to use?") }}
</a>
)
</div>
<div class="mt-1 rounded-lg bg-gray-100 p-4 dark:bg-gray-700">
@foreach ($site->environmentVariables() as $key => $variable)
{{ $key }}={{ $variable }}
<br />
@endforeach
</div>
</div>

<div class="mt-6 flex items-center justify-end">
<x-secondary-button type="button" x-on:click="$dispatch('close')">
{{ __("Cancel") }}
Expand Down

0 comments on commit 7d36746

Please sign in to comment.