From 5607627ed13d652a8ac2bb16349157c0098c6441 Mon Sep 17 00:00:00 2001 From: Damian Crisafulli Date: Sat, 2 Apr 2022 09:00:27 -0400 Subject: [PATCH] Specify Environment Dockerfile Path In Manifest --- src/Docker.php | 4 ++-- src/Manifest.php | 11 +++++++++++ src/Path.php | 2 +- tests/DockerTest.php | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Docker.php b/src/Docker.php index 4cce37e6..2dd30f69 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -38,8 +38,8 @@ public static function build($path, $project, $environment, $cliBuildArgs) */ public static function buildCommand($project, $environment, $cliBuildArgs, $manifestBuildArgs) { - return sprintf('docker build --pull --file=%s.Dockerfile --tag=%s %s.', - $environment, + return sprintf('docker build --pull --file=%s --tag=%s %s.', + Manifest::dockerfile($environment), Str::slug($project).':'.$environment, Collection::make($manifestBuildArgs) ->merge(Collection::make($cliBuildArgs) diff --git a/src/Manifest.php b/src/Manifest.php index 6034d9ae..8a55251f 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -66,6 +66,17 @@ public static function dockerBuildArgs($environment) return static::current()['environments'][$environment]['docker-build-args'] ?? []; } + /** + * Get the Dockerfile. + * + * @param string $environment + * @return string + */ + public static function dockerfile($environment) + { + return static::current()['environments'][$environment]['dockerfile'] ?? "{$environment}.Dockerfile"; + } + /** * Determine if the environment uses a database proxy. * diff --git a/src/Path.php b/src/Path.php index 58b29821..9b7c915e 100644 --- a/src/Path.php +++ b/src/Path.php @@ -112,6 +112,6 @@ public static function vapor() */ public static function dockerfile($environment) { - return getcwd().'/'.$environment.'.Dockerfile'; + return getcwd().'/'.Manifest::dockerfile($environment); } } diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 0a823542..b24ef203 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -2,11 +2,26 @@ namespace Laravel\VaporCli\Tests; +use Illuminate\Container\Container; use Laravel\VaporCli\Docker; use PHPUnit\Framework\TestCase; +use Symfony\Component\Yaml\Yaml; class DockerTest extends TestCase { + protected function setUp(): void + { + parent::setUp(); + touch($testManifest = getcwd().'/test.vapor.yml'); + Container::getInstance()->offsetSet('manifest', $testManifest); + } + + protected function tearDown(): void + { + @unlink(Container::getInstance()->offsetGet('manifest')); + parent::tearDown(); + } + public function test_build_command_no_build_args() { $command = Docker::buildCommand('my-project', 'production', [], []); @@ -41,4 +56,21 @@ public function test_build_command_cli_and_manifest_build_args() "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; $this->assertEquals($expectedCommand, $command); } + + public function test_dockerfile_from_manifest() + { + file_put_contents(Container::getInstance()->offsetGet('manifest') , Yaml::dump([ + 'id' => 1, + 'name' => 'Test', + 'environments' => [ + 'production' => [ + 'runtime' => 'docker', + 'dockerfile' => 'docker/shared.Dockerfile', + ] + ] + ])); + $command = Docker::buildCommand('my-project', 'production', [], []); + $expectedCommand = 'docker build --pull --file=docker/shared.Dockerfile --tag=my-project:production .'; + $this->assertEquals($expectedCommand, $command); + } }