Added refresh-remote-state option to deploy command
This commit is contained in:
4 files changed
+68
-2
No files matched your search
@@ -85,6 +85,14 @@ Upload changed files and update the remote sync-state:
|
|||||||
php artisan ftp:deploy --yes
|
php artisan ftp:deploy --yes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Refresh the remote server sync-state through the JSON API before deploying:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan ftp:deploy --refresh-remote-state --yes
|
||||||
|
```
|
||||||
|
|
||||||
|
This option calls the same API implementation used by `ftp:api-summary`, then continues the FTP deploy using the refreshed remote state file.
|
||||||
|
|
||||||
Generate a sync-state file locally:
|
Generate a sync-state file locally:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class CallFtpDeployApiCommand extends Command
|
|||||||
$response = Http::acceptJson()
|
$response = Http::acceptJson()
|
||||||
->withToken($token)
|
->withToken($token)
|
||||||
->timeout($timeout)
|
->timeout($timeout)
|
||||||
|
->connectTimeout(min(5, $timeout))
|
||||||
->get($url)
|
->get($url)
|
||||||
->throw();
|
->throw();
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use function Laravel\Prompts\warning;
|
|||||||
{--disk= : Filesystem disk configured for the FTP server}
|
{--disk= : Filesystem disk configured for the FTP server}
|
||||||
{--state= : Remote sync-state file path}
|
{--state= : Remote sync-state file path}
|
||||||
{--exclude=* : Extra exclude path or wildcard pattern; comma-separated values are supported}
|
{--exclude=* : Extra exclude path or wildcard pattern; comma-separated values are supported}
|
||||||
|
{--refresh-remote-state : Call the configured JSON API before comparing the remote sync-state}
|
||||||
{--dry-run : Show what would be uploaded without changing the FTP server}
|
{--dry-run : Show what would be uploaded without changing the FTP server}
|
||||||
{--force : Upload every included local file}
|
{--force : Upload every included local file}
|
||||||
{--yes : Skip the confirmation prompt}')]
|
{--yes : Skip the confirmation prompt}')]
|
||||||
@@ -55,6 +56,15 @@ class DeployFtpChangedFilesCommand extends Command
|
|||||||
|
|
||||||
$remoteDisk = Storage::disk($diskName);
|
$remoteDisk = Storage::disk($diskName);
|
||||||
$currentState = $this->ftpDeployManager->buildCurrentState($localRoot, $excludedPatterns);
|
$currentState = $this->ftpDeployManager->buildCurrentState($localRoot, $excludedPatterns);
|
||||||
|
|
||||||
|
if ((bool) $this->option('refresh-remote-state')) {
|
||||||
|
$refreshExitCode = $this->refreshRemoteState();
|
||||||
|
|
||||||
|
if ($refreshExitCode !== self::SUCCESS) {
|
||||||
|
return $refreshExitCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$remoteState = $this->ftpDeployManager->readRemoteState($remoteDisk, $statePath);
|
$remoteState = $this->ftpDeployManager->readRemoteState($remoteDisk, $statePath);
|
||||||
|
|
||||||
if ($remoteState === null) {
|
if ($remoteState === null) {
|
||||||
@@ -102,6 +112,13 @@ class DeployFtpChangedFilesCommand extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function refreshRemoteState(): int
|
||||||
|
{
|
||||||
|
info('Refreshing remote sync-state before deployment.');
|
||||||
|
|
||||||
|
return $this->call('ftp:api-summary');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array{data: array<int, array<string, mixed>>} $currentState
|
* @param array{data: array<int, array<string, mixed>>} $currentState
|
||||||
* @param array<int, array<string, mixed>> $changedFiles
|
* @param array<int, array<string, mixed>> $changedFiles
|
||||||
|
|||||||
@@ -177,6 +177,46 @@ test('ftp deploy dry run leaves remote files and state untouched', function () {
|
|||||||
->and(Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'))->toBe($remoteState);
|
->and(Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'))->toBe($remoteState);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('ftp deploy can refresh remote sync state through api before comparing files', function () {
|
||||||
|
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
||||||
|
|
||||||
|
Storage::disk('ftp_deploy')->put('same.txt', 'same contents');
|
||||||
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', ftpDeploySyncState([]));
|
||||||
|
|
||||||
|
$refreshedState = ftpDeploySyncState([
|
||||||
|
'same.txt' => 'same contents',
|
||||||
|
], generatedTime: 123);
|
||||||
|
|
||||||
|
Http::fake(function () use ($refreshedState) {
|
||||||
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', $refreshedState);
|
||||||
|
|
||||||
|
return Http::response([
|
||||||
|
'success' => true,
|
||||||
|
'path' => '/var/www/storage/app/.ftp-deploy-sync-state.json',
|
||||||
|
'files' => 1,
|
||||||
|
'folders' => 0,
|
||||||
|
'generated_time' => 123,
|
||||||
|
'state' => json_decode($refreshedState, true, flags: JSON_THROW_ON_ERROR),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->artisan('ftp:deploy', [
|
||||||
|
'--refresh-remote-state' => true,
|
||||||
|
'--yes' => true,
|
||||||
|
])->assertExitCode(0);
|
||||||
|
|
||||||
|
$state = json_decode(
|
||||||
|
Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'),
|
||||||
|
true,
|
||||||
|
flags: JSON_THROW_ON_ERROR,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect($state['generatedTime'])->toBe(123);
|
||||||
|
|
||||||
|
Http::assertSent(fn ($request): bool => $request->url() === 'https://example.test/ftp/generate-sync-state'
|
||||||
|
&& $request->hasHeader('Authorization', 'Bearer secret-token'));
|
||||||
|
});
|
||||||
|
|
||||||
test('ftp deploy force uploads every included file', function () {
|
test('ftp deploy force uploads every included file', function () {
|
||||||
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
||||||
ftpDeployWriteLocalFile($this->localRoot, 'other.txt', 'other contents');
|
ftpDeployWriteLocalFile($this->localRoot, 'other.txt', 'other contents');
|
||||||
@@ -208,12 +248,12 @@ function ftpDeployWriteLocalFile(string $root, string $path, string $contents):
|
|||||||
/**
|
/**
|
||||||
* @param array<string, string> $files
|
* @param array<string, string> $files
|
||||||
*/
|
*/
|
||||||
function ftpDeploySyncState(array $files): string
|
function ftpDeploySyncState(array $files, int $generatedTime = 1): string
|
||||||
{
|
{
|
||||||
return json_encode([
|
return json_encode([
|
||||||
'description' => 'test sync state',
|
'description' => 'test sync state',
|
||||||
'version' => '1.0.0',
|
'version' => '1.0.0',
|
||||||
'generatedTime' => 1,
|
'generatedTime' => $generatedTime,
|
||||||
'data' => collect($files)
|
'data' => collect($files)
|
||||||
->map(fn (string $contents, string $name): array => [
|
->map(fn (string $contents, string $name): array => [
|
||||||
'type' => 'file',
|
'type' => 'file',
|
||||||
|
|||||||
Reference in new issue
Block a user