diff --git a/README.md b/README.md index aa8a480..a2101b3 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,14 @@ Upload changed files and update the remote sync-state: 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: ```bash diff --git a/src/Console/Commands/CallFtpDeployApiCommand.php b/src/Console/Commands/CallFtpDeployApiCommand.php index 5bedea1..f8d2d25 100644 --- a/src/Console/Commands/CallFtpDeployApiCommand.php +++ b/src/Console/Commands/CallFtpDeployApiCommand.php @@ -39,6 +39,7 @@ class CallFtpDeployApiCommand extends Command $response = Http::acceptJson() ->withToken($token) ->timeout($timeout) + ->connectTimeout(min(5, $timeout)) ->get($url) ->throw(); diff --git a/src/Console/Commands/DeployFtpChangedFilesCommand.php b/src/Console/Commands/DeployFtpChangedFilesCommand.php index 5017e30..9fd5a51 100644 --- a/src/Console/Commands/DeployFtpChangedFilesCommand.php +++ b/src/Console/Commands/DeployFtpChangedFilesCommand.php @@ -25,6 +25,7 @@ use function Laravel\Prompts\warning; {--disk= : Filesystem disk configured for the FTP server} {--state= : Remote sync-state file path} {--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} {--force : Upload every included local file} {--yes : Skip the confirmation prompt}')] @@ -55,6 +56,15 @@ class DeployFtpChangedFilesCommand extends Command $remoteDisk = Storage::disk($diskName); $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); 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>} $currentState * @param array> $changedFiles diff --git a/tests/Feature/LaravelFtpDeployTest.php b/tests/Feature/LaravelFtpDeployTest.php index a7c45b1..f1c32c0 100644 --- a/tests/Feature/LaravelFtpDeployTest.php +++ b/tests/Feature/LaravelFtpDeployTest.php @@ -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); }); +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 () { ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents'); ftpDeployWriteLocalFile($this->localRoot, 'other.txt', 'other contents'); @@ -208,12 +248,12 @@ function ftpDeployWriteLocalFile(string $root, string $path, string $contents): /** * @param array $files */ -function ftpDeploySyncState(array $files): string +function ftpDeploySyncState(array $files, int $generatedTime = 1): string { return json_encode([ 'description' => 'test sync state', 'version' => '1.0.0', - 'generatedTime' => 1, + 'generatedTime' => $generatedTime, 'data' => collect($files) ->map(fn (string $contents, string $name): array => [ 'type' => 'file',