Skip to content

Commit 76daebd

Browse files
committed
feat: skip assets processing if no changes were made to assets
1 parent a723a80 commit 76daebd

8 files changed

Lines changed: 54 additions & 12 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/polyfill-php80": "^1.24",
3333
"symfony/process": "^5.4|^6.0",
3434
"symfony/yaml": "^5.4|^6.0",
35-
"ymirapp/ymir-sdk-php": "^0.1.1"
35+
"ymirapp/ymir-sdk-php": "^0.1.2"
3636
},
3737
"require-dev": {
3838
"fakerphp/faker": "^1.17",

src/ApiClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public function createDatabaseUser(int $databaseServerId, string $username, arra
150150
/**
151151
* Create a new deployment for the given project on the given environment.
152152
*/
153-
public function createDeployment(int $projectId, string $environment, ProjectConfiguration $projectConfiguration): Collection
153+
public function createDeployment(int $projectId, string $environment, ProjectConfiguration $projectConfiguration, ?string $assetsHash = null): Collection
154154
{
155-
return $this->client->createDeployment($projectId, $environment, $projectConfiguration->toArray());
155+
return $this->client->createDeployment($projectId, $environment, $projectConfiguration->toArray(), $assetsHash);
156156
}
157157

158158
/**

src/Command/Project/AbstractProjectDeploymentCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function perform(InputInterface $input, OutputInterface $output)
5656
$environment = $this->getStringArgument($input, 'environment');
5757

5858
foreach ($this->deploymentSteps as $deploymentStep) {
59-
$deploymentStep->perform($deployment, $output);
59+
$deploymentStep->perform($deployment, $environment, $output);
6060
}
6161

6262
$output->info($this->getSuccessMessage($environment));

src/Command/Project/DeployProjectCommand.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class DeployProjectCommand extends AbstractProjectDeploymentCommand
4040
*/
4141
public const NAME = 'project:deploy';
4242

43+
/**
44+
* The assets directory where the asset files were copied to.
45+
*
46+
* @var string
47+
*/
48+
private $assetsDirectory;
49+
4350
/**
4451
* The build "uploads" directory.
4552
*
@@ -50,10 +57,11 @@ class DeployProjectCommand extends AbstractProjectDeploymentCommand
5057
/**
5158
* Constructor.
5259
*/
53-
public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, string $uploadsDirectory, array $deploymentSteps = [])
60+
public function __construct(ApiClient $apiClient, string $assetsDirectory, CliConfiguration $cliConfiguration, ProjectConfiguration $projectConfiguration, string $uploadsDirectory, array $deploymentSteps = [])
5461
{
5562
parent::__construct($apiClient, $cliConfiguration, $projectConfiguration, $deploymentSteps);
5663

64+
$this->assetsDirectory = $assetsDirectory;
5765
$this->uploadsDirectory = $uploadsDirectory;
5866
}
5967

@@ -86,7 +94,7 @@ protected function createDeployment(InputInterface $input, OutputInterface $outp
8694
$this->invoke($output, ImportUploadsCommand::NAME, ['path' => $this->uploadsDirectory, '--environment' => $environment, '--force' => null]);
8795
}
8896

89-
$deployment = $this->apiClient->createDeployment($projectId, $environment, $this->projectConfiguration);
97+
$deployment = $this->apiClient->createDeployment($projectId, $environment, $this->projectConfiguration, $this->generateDirectoryHash($this->assetsDirectory));
9098

9199
if (!$deployment->has('id')) {
92100
throw new RuntimeException('There was an error creating the deployment');
@@ -102,4 +110,20 @@ protected function getSuccessMessage(string $environment): string
102110
{
103111
return sprintf('Project deployed successfully to "<comment>%s</comment>" environment', $environment);
104112
}
113+
114+
/**
115+
* Generate a hash for the content of the given directory.
116+
*/
117+
private function generateDirectoryHash(string $directory): string
118+
{
119+
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::SELF_FIRST);
120+
121+
return hash('sha256', collect($iterator)->filter(function (\SplFileInfo $file) {
122+
return $file->isFile();
123+
})->mapWithKeys(function (\SplFileInfo $file) {
124+
return [substr($file->getRealPath(), (int) strrpos($file->getRealPath(), '.ymir')) => $file->getRealPath()];
125+
})->map(function (string $realPath, string $relativePath) {
126+
return sprintf('%s|%s', $relativePath, hash_file('sha256', $realPath));
127+
})->implode(''));
128+
}
105129
}

src/Deployment/DeploymentStepInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface DeploymentStepInterface
2121
/**
2222
* Perform the deployment step and generate the console output.
2323
*/
24-
public function perform(Collection $deployment, OutputInterface $output);
24+
public function perform(Collection $deployment, string $environment, OutputInterface $output);
2525
}

src/Deployment/ProcessAssetsStep.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Ymir\Cli\ApiClient;
2121
use Ymir\Cli\Console\OutputInterface;
2222
use Ymir\Cli\FileUploader;
23+
use Ymir\Cli\ProjectConfiguration\ProjectConfiguration;
2324

2425
class ProcessAssetsStep implements DeploymentStepInterface
2526
{
@@ -37,6 +38,13 @@ class ProcessAssetsStep implements DeploymentStepInterface
3738
*/
3839
private $assetsDirectory;
3940

41+
/**
42+
* The Ymir project configuration.
43+
*
44+
* @var ProjectConfiguration
45+
*/
46+
private $projectConfiguration;
47+
4048
/**
4149
* The uploader used to upload all the build files.
4250
*
@@ -47,18 +55,29 @@ class ProcessAssetsStep implements DeploymentStepInterface
4755
/**
4856
* Constructor.
4957
*/
50-
public function __construct(ApiClient $apiClient, string $assetsDirectory, FileUploader $uploader)
58+
public function __construct(ApiClient $apiClient, string $assetsDirectory, ProjectConfiguration $projectConfiguration, FileUploader $uploader)
5159
{
5260
$this->apiClient = $apiClient;
5361
$this->assetsDirectory = $assetsDirectory;
62+
$this->projectConfiguration = $projectConfiguration;
5463
$this->uploader = $uploader;
5564
}
5665

5766
/**
5867
* {@inheritdoc}
5968
*/
60-
public function perform(Collection $deployment, OutputInterface $output)
69+
public function perform(Collection $deployment, string $environment, OutputInterface $output)
6170
{
71+
$deploymentWithAssetsHash = $this->apiClient->getDeployments($this->projectConfiguration->getProjectId(), $environment)
72+
->where('status', 'finished')
73+
->firstWhere('assets_hash', $deployment->get('assets_hash'));
74+
75+
if (null !== $deploymentWithAssetsHash) {
76+
$output->infoWithWarning('No assets change detected', 'skipping processing assets');
77+
78+
return;
79+
}
80+
6281
$output->info('Processing assets');
6382

6483
$output->writeStep('Getting signed asset URLs');

src/Deployment/StartAndMonitorDeploymentStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(ApiClient $apiClient)
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function perform(Collection $deployment, OutputInterface $output)
41+
public function perform(Collection $deployment, string $environment, OutputInterface $output)
4242
{
4343
$output->info(sprintf('%s starting', ucfirst($deployment->get('type', 'deployment'))));
4444

src/Deployment/UploadFunctionCodeStep.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ public function __construct(ApiClient $apiClient, string $buildArtifactPath, str
7474
/**
7575
* {@inheritdoc}
7676
*/
77-
public function perform(Collection $deployment, OutputInterface $output)
77+
public function perform(Collection $deployment, string $environment, OutputInterface $output)
7878
{
7979
$configuration = $deployment->get('configuration');
80-
$environment = Arr::first(array_keys($configuration['environments']));
8180
$deploymentType = Arr::get($configuration, sprintf('environments.%s.deployment', $environment), 'zip');
8281

8382
if ('image' === $deploymentType) {

0 commit comments

Comments
 (0)