Skip to content

Commit d3dcfee

Browse files
committed
feat: add support for container image deployments
1 parent ad05bbf commit d3dcfee

22 files changed

+471
-91
lines changed

config/services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ services:
6969
- '@Ymir\Cli\Build\ModifyWordPressConfigurationStep'
7070
- '@Ymir\Cli\Build\ExtractAssetFilesStep'
7171
- '@Ymir\Cli\Build\CompressBuildFilesStep'
72+
- '@Ymir\Cli\Build\BuildContainerImageStep'
7273

7374
Ymir\Cli\Command\Project\DeployProjectCommand:
7475
arguments:
7576
$deploymentSteps:
76-
- '@Ymir\Cli\Deployment\UploadBuildStep'
77+
- '@Ymir\Cli\Deployment\UploadFunctionCodeStep'
7778
- '@Ymir\Cli\Deployment\ProcessAssetsStep'
7879
- '@Ymir\Cli\Deployment\StartAndMonitorDeploymentStep'
7980

src/ApiClient.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ public function getDeployment(int $deploymentId): Collection
531531
return $this->request('get', "/deployments/{$deploymentId}");
532532
}
533533

534+
/**
535+
* Get the container image used by the deployment.
536+
*/
537+
public function getDeploymentImage(int $deploymentId): Collection
538+
{
539+
return $this->request('get', "/deployments/{$deploymentId}/image");
540+
}
541+
534542
/**
535543
* Get all the deployments for the given project on the given environment.
536544
*/

src/Build/AbstractBuildStep.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Build;
15+
16+
use Ymir\Cli\ProjectConfiguration;
17+
18+
abstract class AbstractBuildStep implements BuildStepInterface
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function isNeeded(string $environment, ProjectConfiguration $projectConfiguration): bool
24+
{
25+
return true;
26+
}
27+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Build;
15+
16+
use Symfony\Component\Filesystem\Filesystem;
17+
use Symfony\Component\Process\Exception\RuntimeException;
18+
use Tightenco\Collect\Support\Arr;
19+
use Ymir\Cli\Docker;
20+
use Ymir\Cli\ProjectConfiguration;
21+
22+
class BuildContainerImageStep implements BuildStepInterface
23+
{
24+
/**
25+
* The build directory where the project files are copied to.
26+
*
27+
* @var string
28+
*/
29+
private $buildDirectory;
30+
31+
/**
32+
* The file system.
33+
*
34+
* @var Filesystem
35+
*/
36+
private $filesystem;
37+
38+
/**
39+
* Constructor.
40+
*/
41+
public function __construct(string $buildDirectory, Filesystem $filesystem)
42+
{
43+
$this->buildDirectory = rtrim($buildDirectory, '/');
44+
$this->filesystem = $filesystem;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function getDescription(): string
51+
{
52+
return 'Building container image';
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function isNeeded(string $environment, ProjectConfiguration $projectConfiguration): bool
59+
{
60+
return 'image' === Arr::get((array) $projectConfiguration->getEnvironment($environment), 'deployment');
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function perform(string $environment, ProjectConfiguration $projectConfiguration)
67+
{
68+
$file = 'Dockerfile';
69+
70+
if ($this->filesystem->exists($this->buildDirectory.sprintf('/.%s.Dockerfile', $environment))) {
71+
$file = sprintf('.%s.Dockerfile', $environment);
72+
} elseif ($this->filesystem->exists($this->buildDirectory.sprintf('/%s.Dockerfile', $environment))) {
73+
$file = sprintf('%s.Dockerfile', $environment);
74+
}
75+
76+
if (!$this->filesystem->exists($this->buildDirectory.'/'.$file)) {
77+
throw new RuntimeException('Unable to find a Dockerfile to build the container image');
78+
}
79+
80+
Docker::build($file, sprintf('%s:%s', $projectConfiguration->getProjectName(), $environment), $this->buildDirectory);
81+
}
82+
}

src/Build/BuildStepInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ interface BuildStepInterface
2222
*/
2323
public function getDescription(): string;
2424

25+
/**
26+
* Check if the build step needs to be performed for the given project environment.
27+
*/
28+
public function isNeeded(string $environment, ProjectConfiguration $projectConfiguration): bool;
29+
2530
/**
2631
* Perform the build step.
2732
*/

src/Build/CompressBuildFilesStep.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function getDescription(): string
5252
return 'Compressing build files';
5353
}
5454

55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function isNeeded(string $environment, ProjectConfiguration $projectConfiguration): bool
59+
{
60+
return 'image' !== Arr::get((array) $projectConfiguration->getEnvironment($environment), 'deployment');
61+
}
62+
5563
/**
5664
* {@inheritdoc}
5765
*/

src/Build/CopyMustUsePluginStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Filesystem\Filesystem;
1818
use Ymir\Cli\ProjectConfiguration;
1919

20-
class CopyMustUsePluginStep implements BuildStepInterface
20+
class CopyMustUsePluginStep extends AbstractBuildStep
2121
{
2222
/**
2323
* The build directory where the project files are copied to.

src/Build/CopyWordPressFilesStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Tightenco\Collect\Support\Arr;
2020
use Ymir\Cli\ProjectConfiguration;
2121

22-
class CopyWordPressFilesStep implements BuildStepInterface
22+
class CopyWordPressFilesStep extends AbstractBuildStep
2323
{
2424
/**
2525
* The build directory where the project files are copied to.

src/Build/DownloadWpCliStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Filesystem\Filesystem;
1717
use Ymir\Cli\ProjectConfiguration;
1818

19-
class DownloadWpCliStep implements BuildStepInterface
19+
class DownloadWpCliStep extends AbstractBuildStep
2020
{
2121
/**
2222
* The path to the WP-CLI bin directory.

src/Build/EnsurePluginIsInstalledStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Finder\Finder;
1818
use Ymir\Cli\ProjectConfiguration;
1919

20-
class EnsurePluginIsInstalledStep implements BuildStepInterface
20+
class EnsurePluginIsInstalledStep extends AbstractBuildStep
2121
{
2222
/**
2323
* The build directory where the project files are copied to.

0 commit comments

Comments
 (0)