Skip to content

Commit f1cf008

Browse files
author
Jonathan Visser
committed
Add high-performance static content deployment task
Integrate elgentos/magento2-static-deploy Go binary for 230-380x faster static content deployment. The task overrides magento:deploy:assets when enabled via enableHighPerformanceStaticDeploy() in the configuration. Features: - Downloads Go binary from GitHub releases (supports 'latest' or specific version) - Deploys both frontend and adminhtml areas - Uses Deployer variables: magento_themes, static_content_locales, content_version - Auto-detects Hyvä vs Luma themes (Luma dispatched to bin/magento)
1 parent a8f94a8 commit f1cf008

2 files changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy\Deployer\Task\Build;
6+
7+
use Hypernode\Deploy\Deployer\Task\TaskBase;
8+
use Hypernode\DeployConfiguration\Configuration;
9+
10+
use function Deployer\get;
11+
use function Deployer\run;
12+
use function Deployer\task;
13+
use function Deployer\within;
14+
15+
/**
16+
* High-performance static content deployment using elgentos/magento2-static-deploy.
17+
*
18+
* @see https://github.com/elgentos/magento2-static-deploy
19+
*/
20+
class HighPerformanceStaticDeployTask extends TaskBase
21+
{
22+
private const BINARY_URL_VERSIONED = 'https://github.com/elgentos/magento2-static-deploy/releases/download/%s/magento2-static-deploy-linux-amd64';
23+
private const BINARY_URL_LATEST = 'https://github.com/elgentos/magento2-static-deploy/releases/latest/download/magento2-static-deploy-linux-amd64';
24+
private const DEFAULT_VERSION = 'latest';
25+
26+
public function configure(Configuration $config): void
27+
{
28+
if (!$this->isEnabled($config)) {
29+
return;
30+
}
31+
32+
$version = $this->getVersion($config);
33+
34+
task('magento:deploy:assets', function () use ($version) {
35+
within('{{release_or_current_path}}', function () use ($version) {
36+
run(sprintf('curl -sL -o /tmp/magento2-static-deploy %s', $this->getBinaryUrl($version)));
37+
run('chmod +x /tmp/magento2-static-deploy');
38+
});
39+
40+
$themes = get('magento_themes', []);
41+
$themeArgs = $this->buildThemeArgs($themes);
42+
$locales = get('static_content_locales', 'en_US');
43+
$contentVersion = get('content_version', time());
44+
45+
within('{{release_or_current_path}}', function () use ($themeArgs, $locales, $contentVersion) {
46+
run("/tmp/magento2-static-deploy --force --area=frontend --area=adminhtml $themeArgs --content-version=$contentVersion --verbose $locales");
47+
});
48+
})->select('stage=build');
49+
}
50+
51+
public function isEnabled(Configuration $config): bool
52+
{
53+
$variables = $config->getVariables();
54+
$buildVariables = $config->getVariables('build');
55+
56+
return $variables['high_performance_static_deploy']
57+
?? $buildVariables['high_performance_static_deploy']
58+
?? false;
59+
}
60+
61+
public function getVersion(Configuration $config): string
62+
{
63+
$variables = $config->getVariables();
64+
$buildVariables = $config->getVariables('build');
65+
66+
return $variables['high_performance_static_deploy_version']
67+
?? $buildVariables['high_performance_static_deploy_version']
68+
?? self::DEFAULT_VERSION;
69+
}
70+
71+
public function getBinaryUrl(string $version): string
72+
{
73+
if ($version === 'latest') {
74+
return self::BINARY_URL_LATEST;
75+
}
76+
77+
return sprintf(self::BINARY_URL_VERSIONED, $version);
78+
}
79+
80+
/**
81+
* @param array<string, string> $themes
82+
*/
83+
public function buildThemeArgs(array $themes): string
84+
{
85+
return implode(' ', array_map(fn($t) => "--theme=$t", array_keys($themes)));
86+
}
87+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy\Tests\Unit\Deployer\Task\Build;
6+
7+
use Hypernode\Deploy\Deployer\Task\Build\HighPerformanceStaticDeployTask;
8+
use Hypernode\DeployConfiguration\Configuration;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class HighPerformanceStaticDeployTaskTest extends TestCase
12+
{
13+
private HighPerformanceStaticDeployTask $task;
14+
15+
protected function setUp(): void
16+
{
17+
$this->task = new HighPerformanceStaticDeployTask();
18+
}
19+
20+
public function testIsEnabledReturnsFalseWhenNotConfigured(): void
21+
{
22+
$config = $this->createMock(Configuration::class);
23+
$config->method('getVariables')->willReturn([]);
24+
25+
$this->assertFalse($this->task->isEnabled($config));
26+
}
27+
28+
public function testIsEnabledReturnsTrueWhenEnabledInVariables(): void
29+
{
30+
$config = $this->createMock(Configuration::class);
31+
$config->method('getVariables')
32+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
33+
'all' => ['high_performance_static_deploy' => true],
34+
default => [],
35+
});
36+
37+
$this->assertTrue($this->task->isEnabled($config));
38+
}
39+
40+
public function testIsEnabledReturnsTrueWhenEnabledInBuildVariables(): void
41+
{
42+
$config = $this->createMock(Configuration::class);
43+
$config->method('getVariables')
44+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
45+
'build' => ['high_performance_static_deploy' => true],
46+
default => [],
47+
});
48+
49+
$this->assertTrue($this->task->isEnabled($config));
50+
}
51+
52+
public function testIsEnabledReturnsFalseWhenExplicitlyDisabled(): void
53+
{
54+
$config = $this->createMock(Configuration::class);
55+
$config->method('getVariables')
56+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
57+
'all' => ['high_performance_static_deploy' => false],
58+
default => [],
59+
});
60+
61+
$this->assertFalse($this->task->isEnabled($config));
62+
}
63+
64+
public function testGetVersionReturnsDefaultWhenNotConfigured(): void
65+
{
66+
$config = $this->createMock(Configuration::class);
67+
$config->method('getVariables')->willReturn([]);
68+
69+
$this->assertSame('latest', $this->task->getVersion($config));
70+
}
71+
72+
public function testGetVersionReturnsConfiguredVersion(): void
73+
{
74+
$config = $this->createMock(Configuration::class);
75+
$config->method('getVariables')
76+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
77+
'all' => ['high_performance_static_deploy_version' => '1.0.0'],
78+
default => [],
79+
});
80+
81+
$this->assertSame('1.0.0', $this->task->getVersion($config));
82+
}
83+
84+
public function testGetVersionReturnsBuildVersionWhenNotInAllVariables(): void
85+
{
86+
$config = $this->createMock(Configuration::class);
87+
$config->method('getVariables')
88+
->willReturnCallback(fn(string $stage = 'all') => match ($stage) {
89+
'build' => ['high_performance_static_deploy_version' => '2.0.0'],
90+
default => [],
91+
});
92+
93+
$this->assertSame('2.0.0', $this->task->getVersion($config));
94+
}
95+
96+
public function testGetBinaryUrlReturnsLatestUrl(): void
97+
{
98+
$result = $this->task->getBinaryUrl('latest');
99+
100+
$this->assertSame(
101+
'https://github.com/elgentos/magento2-static-deploy/releases/latest/download/magento2-static-deploy-linux-amd64',
102+
$result
103+
);
104+
}
105+
106+
public function testGetBinaryUrlReturnsVersionedUrl(): void
107+
{
108+
$result = $this->task->getBinaryUrl('0.0.8');
109+
110+
$this->assertSame(
111+
'https://github.com/elgentos/magento2-static-deploy/releases/download/0.0.8/magento2-static-deploy-linux-amd64',
112+
$result
113+
);
114+
}
115+
116+
public function testGetBinaryUrlWithDifferentVersion(): void
117+
{
118+
$result = $this->task->getBinaryUrl('1.2.3');
119+
120+
$this->assertStringContainsString('1.2.3', $result);
121+
$this->assertStringContainsString('magento2-static-deploy-linux-amd64', $result);
122+
}
123+
124+
public function testBuildThemeArgsWithSingleTheme(): void
125+
{
126+
$themes = ['Vendor/theme' => 'nl_NL en_US'];
127+
128+
$result = $this->task->buildThemeArgs($themes);
129+
130+
$this->assertSame('--theme=Vendor/theme', $result);
131+
}
132+
133+
public function testBuildThemeArgsWithMultipleThemes(): void
134+
{
135+
$themes = [
136+
'Vendor/theme1' => 'nl_NL',
137+
'Vendor/theme2' => 'en_US',
138+
'Vendor/theme3' => 'de_DE',
139+
];
140+
141+
$result = $this->task->buildThemeArgs($themes);
142+
143+
$this->assertSame('--theme=Vendor/theme1 --theme=Vendor/theme2 --theme=Vendor/theme3', $result);
144+
}
145+
146+
public function testBuildThemeArgsWithEmptyArray(): void
147+
{
148+
$result = $this->task->buildThemeArgs([]);
149+
150+
$this->assertSame('', $result);
151+
}
152+
}

0 commit comments

Comments
 (0)