From bcc1ef7fba3f71f8379230a7f8a84f748c8a1ded Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Fri, 14 Jul 2023 16:31:16 +0100 Subject: [PATCH 01/13] feat: Add ability to specify args that will be passed to docker during the build --- src/BuildProcess/BuildContainerImage.php | 15 +++++- src/Commands/BuildCommand.php | 7 ++- src/Docker.php | 31 ++++++++++--- src/Manifest.php | 11 +++++ tests/BuildContainerImageTest.php | 2 +- tests/DockerTest.php | 58 ++++++++++++++++++++---- 6 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/BuildProcess/BuildContainerImage.php b/src/BuildProcess/BuildContainerImage.php index 87aef931..e843ff34 100644 --- a/src/BuildProcess/BuildContainerImage.php +++ b/src/BuildProcess/BuildContainerImage.php @@ -44,6 +44,13 @@ class BuildContainerImage */ protected $cliBuildArgs; + /** + * The Docker CLI arguments. + * + * @var array + */ + protected $cliDockerArgs; + /** * The Docker manifest build arguments. * @@ -55,13 +62,16 @@ class BuildContainerImage * Create a new project builder. * * @param string|null $environment - * @param array $buildArgs + * @param array $cliDockerArgs + * @param array $cliBuildArgs + * @param array $manifestBuildArgs * @return void */ - public function __construct($environment = null, $cliBuildArgs = [], $manifestBuildArgs = []) + public function __construct($environment = null, $cliDockerArgs = [], $cliBuildArgs = [], $manifestBuildArgs = []) { $this->baseConstructor($environment); + $this->cliDockerArgs = $cliDockerArgs; $this->cliBuildArgs = $cliBuildArgs; $this->manifestBuildArgs = $manifestBuildArgs; } @@ -96,6 +106,7 @@ public function __invoke() $this->appPath, Manifest::name(), $this->environment, + $this->cliDockerArgs, $this->formatBuildArguments() ); } diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 1383faaa..6d368698 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -88,7 +88,12 @@ public function handle() new ExtractVendorToSeparateDirectory($this->argument('environment')), new CompressApplication($this->argument('environment')), new CompressVendor($this->argument('environment')), - new BuildContainerImage($this->argument('environment'), $this->option('build-arg'), Manifest::dockerBuildArgs($this->argument('environment'))), + new BuildContainerImage( + $this->argument('environment'), + $this->option('docker-arg'), + $this->option('build-arg'), + Manifest::dockerBuildArgs($this->argument('environment')) + ), ])->each->__invoke(); $time = (new DateTime())->diff($startedAt)->format('%im%Ss'); diff --git a/src/Docker.php b/src/Docker.php index 2dd30f69..032ff998 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -14,13 +14,21 @@ class Docker * @param string $path * @param string $project * @param string $environment + * @param array $cliDockerArgs * @param array $cliBuildArgs * @return void */ - public static function build($path, $project, $environment, $cliBuildArgs) + public static function build($path, $project, $environment, $cliDockerArgs, $cliBuildArgs) { Process::fromShellCommandline( - static::buildCommand($project, $environment, $cliBuildArgs, Manifest::dockerBuildArgs($environment)), + static::buildCommand( + $project, + $environment, + $cliDockerArgs, + Manifest::dockerArgs($environment), + $cliBuildArgs, + Manifest::dockerBuildArgs($environment) + ), $path )->setTimeout(null)->mustRun(function ($type, $line) { Helpers::write($line); @@ -32,16 +40,17 @@ public static function build($path, $project, $environment, $cliBuildArgs) * * @param string $project * @param string $environment + * @param array $cliDockerBuildArgs * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return string */ - public static function buildCommand($project, $environment, $cliBuildArgs, $manifestBuildArgs) + public static function buildCommand($project, $environment, $cliDockerArgs, $manifestDockerArgs, $cliBuildArgs, $manifestBuildArgs) { - return sprintf('docker build --pull --file=%s --tag=%s %s.', + return sprintf('docker build --pull --file=%s --tag=%s %s %s .', Manifest::dockerfile($environment), Str::slug($project).':'.$environment, - Collection::make($manifestBuildArgs) + trim(Collection::make($manifestBuildArgs) ->merge(Collection::make($cliBuildArgs) ->mapWithKeys(function ($value) { [$key, $value] = explode('=', $value, 2); @@ -50,7 +59,17 @@ public static function buildCommand($project, $environment, $cliBuildArgs, $mani }) )->map(function ($value, $key) { return '--build-arg='.escapeshellarg("{$key}={$value}").' '; - })->implode('') + })->implode('')), + trim(Collection::make($manifestDockerArgs) + ->merge(Collection::make($cliDockerArgs) + ->mapWithKeys(function ($value) { + [$key, $value] = explode('=', $value, 2); + + return [$key => $value]; + }) + )->map(function ($value, $key) { + return "--${key}=".escapeshellarg($value).' '; + })->implode('')) ); } diff --git a/src/Manifest.php b/src/Manifest.php index 07c0325b..0f8e1e7f 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -95,6 +95,17 @@ public static function dockerBuildArgs($environment) return static::current()['environments'][$environment]['docker-build-args'] ?? []; } + /** + * Get the Docker arguments. + * + * @param string $environment + * @return array + */ + public static function dockerArgs($environment) + { + return static::current()['environments'][$environment]['docker-args'] ?? []; + } + /** * Determine if the environment uses a database proxy. * diff --git a/tests/BuildContainerImageTest.php b/tests/BuildContainerImageTest.php index 16eaedbd..cf25c030 100644 --- a/tests/BuildContainerImageTest.php +++ b/tests/BuildContainerImageTest.php @@ -55,7 +55,7 @@ public function test_docker_build_arguments_can_be_formatted_correctly() ], ])); - $buildArgs = (new BuildContainerImage('production', ['FOO=BAR', 'BAR=BAZ']))->formatBuildArguments(); + $buildArgs = (new BuildContainerImage('production', [], ['FOO=BAR', 'BAR=BAZ']))->formatBuildArguments(); $this->assertSame(['__VAPOR_RUNTIME=docker', 'FOO=BAR', 'BAR=BAZ'], $buildArgs); } diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 7ec017bd..9469643d 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -24,26 +24,26 @@ protected function tearDown(): void public function test_build_command_no_build_args() { - $command = Docker::buildCommand('my-project', 'production', [], []); - $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .'; + $command = Docker::buildCommand('my-project', 'production', [], [], [], [], []); + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .'; $this->assertEquals($expectedCommand, $command); } public function test_build_command_cli_build_args() { $cliBuildArgs = ['FOO=BAR', 'FIZZ=BUZZ']; - $command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, []); + $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); } public function test_build_command_manifest_build_args() { $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; - $command = Docker::buildCommand('my-project', 'production', [], $manifestBuildArgs); + $command = Docker::buildCommand('my-project', 'production', [], [], [], $manifestBuildArgs); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); } @@ -51,9 +51,47 @@ public function test_build_command_cli_and_manifest_build_args() { $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; - $command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, $manifestBuildArgs); + $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, $manifestBuildArgs); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; + $this->assertEquals($expectedCommand, $command); + } + + public function test_build_command_cli_docker_args() + { + $cliDockerArgs = ['FOO=BAR', 'FIZZ=BUZZ']; + $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, [], [], []); + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + "--FOO='BAR' --FIZZ='BUZZ' ."; + $this->assertEquals($expectedCommand, $command); + } + + public function test_build_command_manifest_docker_args() + { + $manifestDockerArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; + $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerArgs, [], []); + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + "--FOO='BAR' --FIZZ='BUZZ' ."; + $this->assertEquals($expectedCommand, $command); + } + + public function test_build_command_cli_and_manifest_docker_args() + { + $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ']; + $manifestDockerArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; + $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, $manifestDockerArgs, [], []); + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + "--FOO='BAR' --FIZZ='BAZZ' --BAR='FOO' ."; + $this->assertEquals($expectedCommand, $command); + } + + public function test_build_command_cli_docker_args_and_cli_build_args() + { + $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ']; + $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; + $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, [], $cliBuildArgs, []); + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + "--build-arg='BAR=FOO' --build-arg='FIZZ=BAZZ' --BAR='FOO' --FIZZ='BAZZ' ."; $this->assertEquals($expectedCommand, $command); } @@ -69,8 +107,8 @@ public function test_dockerfile_from_manifest() ], ], ])); - $command = Docker::buildCommand('my-project', 'production', [], []); - $expectedCommand = 'docker build --pull --file=docker/shared.Dockerfile --tag=my-project:production .'; + $command = Docker::buildCommand('my-project', 'production', [], [], [], []); + $expectedCommand = 'docker build --pull --file=docker/shared.Dockerfile --tag=my-project:production .'; $this->assertEquals($expectedCommand, $command); } } From 43c376a8986c0bb3145ceeb38c602570697299db Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Mon, 17 Jul 2023 07:33:43 +0100 Subject: [PATCH 02/13] fix: docblock --- src/Docker.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Docker.php b/src/Docker.php index 032ff998..6ca8028e 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -40,7 +40,8 @@ public static function build($path, $project, $environment, $cliDockerArgs, $cli * * @param string $project * @param string $environment - * @param array $cliDockerBuildArgs + * @param array $cliDockerArgs + * @param array $manifestDockerArgs * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return string From 4c798283abd8b860e33d4f4463dd3ccbec03e329 Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Wed, 2 Aug 2023 13:29:17 +0100 Subject: [PATCH 03/13] chore: handle docker args flags as well as long flags --- src/Docker.php | 16 +++++++++++++++- tests/DockerTest.php | 14 +++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Docker.php b/src/Docker.php index 6ca8028e..a24df4d0 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -62,13 +62,27 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man return '--build-arg='.escapeshellarg("{$key}={$value}").' '; })->implode('')), trim(Collection::make($manifestDockerArgs) + ->mapWithKeys(function ($value) { + if (is_array($value)) { + return $value; + } + + return [$value => null]; + }) ->merge(Collection::make($cliDockerArgs) ->mapWithKeys(function ($value) { - [$key, $value] = explode('=', $value, 2); + if (!str_contains($value, '=')) { + return [$value => null]; + } + [$key, $value] = explode('=', $value, 2); return [$key => $value]; }) )->map(function ($value, $key) { + if ($value === null) { + return "--${key} "; + } + return "--${key}=".escapeshellarg($value).' '; })->implode('')) ); diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 9469643d..e4016572 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -59,29 +59,29 @@ public function test_build_command_cli_and_manifest_build_args() public function test_build_command_cli_docker_args() { - $cliDockerArgs = ['FOO=BAR', 'FIZZ=BUZZ']; + $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, [], [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--FOO='BAR' --FIZZ='BUZZ' ."; + "--BAR='FOO' --FIZZ='BAZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } public function test_build_command_manifest_docker_args() { - $manifestDockerArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; + $manifestDockerArgs = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerArgs, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--FOO='BAR' --FIZZ='BUZZ' ."; + "--FOO='BAR' --FIZZ='BUZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } public function test_build_command_cli_and_manifest_docker_args() { - $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ']; - $manifestDockerArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; + $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; + $manifestDockerArgs = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, $manifestDockerArgs, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--FOO='BAR' --FIZZ='BAZZ' --BAR='FOO' ."; + "--FOO='BAR' --FIZZ='BAZZ' --FIZZLY --BUZZLY --BAR='FOO' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } From add7f44dad6c395011da80eb13f22c4cb3c763dd Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Wed, 2 Aug 2023 13:32:02 +0100 Subject: [PATCH 04/13] fix: formatting --- src/Docker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Docker.php b/src/Docker.php index a24df4d0..37bfde82 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -71,7 +71,7 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man }) ->merge(Collection::make($cliDockerArgs) ->mapWithKeys(function ($value) { - if (!str_contains($value, '=')) { + if (! str_contains($value, '=')) { return [$value => null]; } From 76beb90e8a5c0563cf90cd6d4ff66dd816034be0 Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Wed, 2 Aug 2023 13:35:16 +0100 Subject: [PATCH 05/13] fix: spacing --- src/Docker.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Docker.php b/src/Docker.php index 37bfde82..b8ea329d 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -76,6 +76,7 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man } [$key, $value] = explode('=', $value, 2); + return [$key => $value]; }) )->map(function ($value, $key) { From d849cf3fa2b972d94d4cb137d01a558a48ccff91 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 3 Aug 2023 09:15:51 +0100 Subject: [PATCH 06/13] raname args to options --- src/BuildProcess/BuildContainerImage.php | 12 ++++++------ src/BuildProcess/RemoveIgnoredFiles.php | 10 +++++----- src/Commands/BuildCommand.php | 2 +- src/Docker.php | 22 +++++++++++----------- src/Manifest.php | 6 +++--- tests/DockerTest.php | 24 ++++++++++++------------ 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/BuildProcess/BuildContainerImage.php b/src/BuildProcess/BuildContainerImage.php index e843ff34..eba7e057 100644 --- a/src/BuildProcess/BuildContainerImage.php +++ b/src/BuildProcess/BuildContainerImage.php @@ -45,11 +45,11 @@ class BuildContainerImage protected $cliBuildArgs; /** - * The Docker CLI arguments. + * The Docker CLI options. * * @var array */ - protected $cliDockerArgs; + protected $cliDockerOptions; /** * The Docker manifest build arguments. @@ -62,16 +62,16 @@ class BuildContainerImage * Create a new project builder. * * @param string|null $environment - * @param array $cliDockerArgs + * @param array $cliDockerOptions * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return void */ - public function __construct($environment = null, $cliDockerArgs = [], $cliBuildArgs = [], $manifestBuildArgs = []) + public function __construct($environment = null, $cliDockerOptions = [], $cliBuildArgs = [], $manifestBuildArgs = []) { $this->baseConstructor($environment); - $this->cliDockerArgs = $cliDockerArgs; + $this->cliDockerOptions = $cliDockerOptions; $this->cliBuildArgs = $cliBuildArgs; $this->manifestBuildArgs = $manifestBuildArgs; } @@ -106,7 +106,7 @@ public function __invoke() $this->appPath, Manifest::name(), $this->environment, - $this->cliDockerArgs, + $this->cliDockerOptions, $this->formatBuildArguments() ); } diff --git a/src/BuildProcess/RemoveIgnoredFiles.php b/src/BuildProcess/RemoveIgnoredFiles.php index ae2179a5..36343eb2 100644 --- a/src/BuildProcess/RemoveIgnoredFiles.php +++ b/src/BuildProcess/RemoveIgnoredFiles.php @@ -40,7 +40,7 @@ protected function removeDefaultIgnoredFiles() Path::app().'/.env.example', Path::app().'/.phpunit.result.cache', Path::app().'/package-lock.json', - Path::app().'/phpunit.xml', + // Path::app().'/phpunit.xml', Path::app().'/readme.md', Path::app().'/server.php', Path::app().'/storage/oauth-private.key', @@ -132,10 +132,10 @@ protected function removeUserIgnoredFiles() $this->files->deleteDirectory($directory.'/'.$filePattern, $preserve = false); } else { $files = (new Finder()) - ->in($directory) - ->depth('== 0') - ->ignoreDotFiles(false) - ->name($filePattern); + ->in($directory) + ->depth('== 0') + ->ignoreDotFiles(false) + ->name($filePattern); foreach ($files as $file) { Helpers::step('Removing Ignored File: '.str_replace(Path::app().'/', '', $file->getRealPath())); diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 6d368698..fa9639a0 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -90,7 +90,7 @@ public function handle() new CompressVendor($this->argument('environment')), new BuildContainerImage( $this->argument('environment'), - $this->option('docker-arg'), + $this->option('docker-option'), $this->option('build-arg'), Manifest::dockerBuildArgs($this->argument('environment')) ), diff --git a/src/Docker.php b/src/Docker.php index b8ea329d..0b8104ba 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -14,18 +14,18 @@ class Docker * @param string $path * @param string $project * @param string $environment - * @param array $cliDockerArgs + * @param array $cliDockerOptions * @param array $cliBuildArgs * @return void */ - public static function build($path, $project, $environment, $cliDockerArgs, $cliBuildArgs) + public static function build($path, $project, $environment, $cliDockerOptions, $cliBuildArgs) { Process::fromShellCommandline( static::buildCommand( $project, $environment, - $cliDockerArgs, - Manifest::dockerArgs($environment), + $cliDockerOptions, + Manifest::dockerOptions($environment), $cliBuildArgs, Manifest::dockerBuildArgs($environment) ), @@ -40,13 +40,13 @@ public static function build($path, $project, $environment, $cliDockerArgs, $cli * * @param string $project * @param string $environment - * @param array $cliDockerArgs - * @param array $manifestDockerArgs + * @param array $cliDockerOptions + * @param array $manifestDockerOptions * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return string */ - public static function buildCommand($project, $environment, $cliDockerArgs, $manifestDockerArgs, $cliBuildArgs, $manifestBuildArgs) + public static function buildCommand($project, $environment, $cliDockerOptions, $manifestDockerOptions, $cliBuildArgs, $manifestBuildArgs) { return sprintf('docker build --pull --file=%s --tag=%s %s %s .', Manifest::dockerfile($environment), @@ -61,7 +61,7 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man )->map(function ($value, $key) { return '--build-arg='.escapeshellarg("{$key}={$value}").' '; })->implode('')), - trim(Collection::make($manifestDockerArgs) + trim(Collection::make($manifestDockerOptions) ->mapWithKeys(function ($value) { if (is_array($value)) { return $value; @@ -69,7 +69,7 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man return [$value => null]; }) - ->merge(Collection::make($cliDockerArgs) + ->merge(Collection::make($cliDockerOptions) ->mapWithKeys(function ($value) { if (! str_contains($value, '=')) { return [$value => null]; @@ -81,10 +81,10 @@ public static function buildCommand($project, $environment, $cliDockerArgs, $man }) )->map(function ($value, $key) { if ($value === null) { - return "--${key} "; + return "--{$key} "; } - return "--${key}=".escapeshellarg($value).' '; + return "--{$key}=".escapeshellarg($value).' '; })->implode('')) ); } diff --git a/src/Manifest.php b/src/Manifest.php index 0f8e1e7f..7671c6d7 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -96,14 +96,14 @@ public static function dockerBuildArgs($environment) } /** - * Get the Docker arguments. + * Get the Docker options. * * @param string $environment * @return array */ - public static function dockerArgs($environment) + public static function dockerOptions($environment) { - return static::current()['environments'][$environment]['docker-args'] ?? []; + return static::current()['environments'][$environment]['docker-options'] ?? []; } /** diff --git a/tests/DockerTest.php b/tests/DockerTest.php index e4016572..1d82f126 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -57,19 +57,19 @@ public function test_build_command_cli_and_manifest_build_args() $this->assertEquals($expectedCommand, $command); } - public function test_build_command_cli_docker_args() + public function test_build_command_cli_docker_options() { - $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, [], [], []); + $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; + $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, [], [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--BAR='FOO' --FIZZ='BAZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } - public function test_build_command_manifest_docker_args() + public function test_build_command_manifest_docker_options() { - $manifestDockerArgs = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerArgs, [], []); + $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; + $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerOptions, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BUZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -77,19 +77,19 @@ public function test_build_command_manifest_docker_args() public function test_build_command_cli_and_manifest_docker_args() { - $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; - $manifestDockerArgs = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, $manifestDockerArgs, [], []); + $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; + $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; + $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, $manifestDockerOptions, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BAZZ' --FIZZLY --BUZZLY --BAR='FOO' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } - public function test_build_command_cli_docker_args_and_cli_build_args() + public function test_build_command_cli_docker_options_and_cli_build_args() { - $cliDockerArgs = ['BAR=FOO', 'FIZZ=BAZZ']; + $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ']; $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerArgs, [], $cliBuildArgs, []); + $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, [], $cliBuildArgs, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='BAR=FOO' --build-arg='FIZZ=BAZZ' --BAR='FOO' --FIZZ='BAZZ' ."; $this->assertEquals($expectedCommand, $command); From f0fdf18ce4eb95c4c37c380123d9cca36a39859d Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 3 Aug 2023 10:54:21 +0100 Subject: [PATCH 07/13] fix output formatting --- src/Docker.php | 75 +++++++++++++++++++++++++------------------- tests/DockerTest.php | 16 +++++----- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/Docker.php b/src/Docker.php index 0b8104ba..2acd9681 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -48,45 +48,54 @@ public static function build($path, $project, $environment, $cliDockerOptions, $ */ public static function buildCommand($project, $environment, $cliDockerOptions, $manifestDockerOptions, $cliBuildArgs, $manifestBuildArgs) { - return sprintf('docker build --pull --file=%s --tag=%s %s %s .', + $command = sprintf( + 'docker build --pull --file=%s --tag=%s ', Manifest::dockerfile($environment), - Str::slug($project).':'.$environment, - trim(Collection::make($manifestBuildArgs) - ->merge(Collection::make($cliBuildArgs) - ->mapWithKeys(function ($value) { - [$key, $value] = explode('=', $value, 2); - - return [$key => $value]; - }) - )->map(function ($value, $key) { - return '--build-arg='.escapeshellarg("{$key}={$value}").' '; - })->implode('')), - trim(Collection::make($manifestDockerOptions) + Str::slug($project).':'.$environment + ); + + $buildArgs = Collection::make($manifestBuildArgs) + ->merge(Collection::make($cliBuildArgs) ->mapWithKeys(function ($value) { - if (is_array($value)) { - return $value; - } + [$key, $value] = explode('=', $value, 2); - return [$value => null]; + return [$key => $value]; }) - ->merge(Collection::make($cliDockerOptions) - ->mapWithKeys(function ($value) { - if (! str_contains($value, '=')) { - return [$value => null]; - } - - [$key, $value] = explode('=', $value, 2); - - return [$key => $value]; - }) - )->map(function ($value, $key) { - if ($value === null) { - return "--{$key} "; + )->map(function ($value, $key) { + return '--build-arg='.escapeshellarg("{$key}={$value}"); + })->implode(' '); + + $dockerOptions = Collection::make($manifestDockerOptions) + ->mapWithKeys(function ($value) { + if (is_array($value)) { + return $value; + } + + return [$value => null]; + }) + ->merge(Collection::make($cliDockerOptions) + ->mapWithKeys(function ($value) { + if (! str_contains($value, '=')) { + return [$value => null]; } - return "--{$key}=".escapeshellarg($value).' '; - })->implode('')) - ); + [$key, $value] = explode('=', $value, 2); + + return [$key => $value]; + }) + )->map(function ($value, $key) { + if ($value === null) { + return "--{$key}"; + } + + return "--{$key}=".escapeshellarg($value); + })->implode(' '); + + $command = $buildArgs ? $command.$buildArgs.' ' : $command; + $command = $dockerOptions ? $command.$dockerOptions.' ' : $command; + $command .= '.'; + + return $command; } /** diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 1d82f126..4ede638d 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -25,7 +25,7 @@ protected function tearDown(): void public function test_build_command_no_build_args() { $command = Docker::buildCommand('my-project', 'production', [], [], [], [], []); - $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .'; + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .'; $this->assertEquals($expectedCommand, $command); } @@ -34,7 +34,7 @@ public function test_build_command_cli_build_args() $cliBuildArgs = ['FOO=BAR', 'FIZZ=BUZZ']; $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); } @@ -43,7 +43,7 @@ public function test_build_command_manifest_build_args() $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; $command = Docker::buildCommand('my-project', 'production', [], [], [], $manifestBuildArgs); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); } @@ -53,7 +53,7 @@ public function test_build_command_cli_and_manifest_build_args() $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, $manifestBuildArgs); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. - "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; + "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; $this->assertEquals($expectedCommand, $command); } @@ -61,7 +61,7 @@ public function test_build_command_cli_docker_options() { $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, [], [], []); - $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--BAR='FOO' --FIZZ='BAZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } @@ -70,7 +70,7 @@ public function test_build_command_manifest_docker_options() { $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerOptions, [], []); - $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BUZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } @@ -80,7 +80,7 @@ public function test_build_command_cli_and_manifest_docker_args() $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, $manifestDockerOptions, [], []); - $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. + $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BAZZ' --FIZZLY --BUZZLY --BAR='FOO' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); } @@ -108,7 +108,7 @@ public function test_dockerfile_from_manifest() ], ])); $command = Docker::buildCommand('my-project', 'production', [], [], [], []); - $expectedCommand = 'docker build --pull --file=docker/shared.Dockerfile --tag=my-project:production .'; + $expectedCommand = 'docker build --pull --file=docker/shared.Dockerfile --tag=my-project:production .'; $this->assertEquals($expectedCommand, $command); } } From 9c2b0d871128d98351b4c1a69181de48385d52d0 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 3 Aug 2023 10:54:32 +0100 Subject: [PATCH 08/13] pass options --- src/Commands/BuildCommand.php | 3 ++- src/Commands/DeployCommand.php | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index fa9639a0..ede9c268 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -45,6 +45,7 @@ protected function configure() ->addArgument('environment', InputArgument::OPTIONAL, 'The environment name') ->addOption('asset-url', null, InputOption::VALUE_OPTIONAL, 'The asset base URL') ->addOption('build-arg', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build argument') + ->addOption('build-option', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build option') ->setDescription('Build the project archive'); } @@ -90,7 +91,7 @@ public function handle() new CompressVendor($this->argument('environment')), new BuildContainerImage( $this->argument('environment'), - $this->option('docker-option'), + $this->option('build-option'), $this->option('build-arg'), Manifest::dockerBuildArgs($this->argument('environment')) ), diff --git a/src/Commands/DeployCommand.php b/src/Commands/DeployCommand.php index 89a78b77..d8eb1a43 100644 --- a/src/Commands/DeployCommand.php +++ b/src/Commands/DeployCommand.php @@ -34,6 +34,7 @@ protected function configure() ->addOption('without-waiting', null, InputOption::VALUE_NONE, 'Deploy without waiting for progress') ->addOption('fresh-assets', null, InputOption::VALUE_NONE, 'Upload a fresh copy of all assets') ->addOption('build-arg', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build argument') + ->addOption('build-option', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build option') ->addOption('debug', null, InputOption::VALUE_OPTIONAL, 'Deploy with debug mode enabled', 'unset') ->setDescription('Deploy an environment'); } @@ -123,6 +124,7 @@ protected function buildProject(array $project) '--asset-url' => $this->assetDomain($project).'/'.$uuid, '--manifest' => Path::manifest(), '--build-arg' => $this->option('build-arg'), + '--build-option' => $this->option('build-option'), ]); return $this->uploadArtifact( From 18f4d303c7169b2a35bfd579726db02b8b9d1a07 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 3 Aug 2023 11:04:53 +0100 Subject: [PATCH 09/13] rename to buildOptions --- src/BuildProcess/BuildContainerImage.php | 10 +++++----- src/Docker.php | 20 ++++++++++---------- src/Manifest.php | 4 ++-- tests/DockerTest.php | 18 +++++++++--------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/BuildProcess/BuildContainerImage.php b/src/BuildProcess/BuildContainerImage.php index eba7e057..b46354f3 100644 --- a/src/BuildProcess/BuildContainerImage.php +++ b/src/BuildProcess/BuildContainerImage.php @@ -49,7 +49,7 @@ class BuildContainerImage * * @var array */ - protected $cliDockerOptions; + protected $cliBuildOptions; /** * The Docker manifest build arguments. @@ -62,16 +62,16 @@ class BuildContainerImage * Create a new project builder. * * @param string|null $environment - * @param array $cliDockerOptions + * @param array $cliBuildOptions * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return void */ - public function __construct($environment = null, $cliDockerOptions = [], $cliBuildArgs = [], $manifestBuildArgs = []) + public function __construct($environment = null, $cliBuildOptions = [], $cliBuildArgs = [], $manifestBuildArgs = []) { $this->baseConstructor($environment); - $this->cliDockerOptions = $cliDockerOptions; + $this->cliBuildOptions = $cliBuildOptions; $this->cliBuildArgs = $cliBuildArgs; $this->manifestBuildArgs = $manifestBuildArgs; } @@ -106,7 +106,7 @@ public function __invoke() $this->appPath, Manifest::name(), $this->environment, - $this->cliDockerOptions, + $this->cliBuildOptions, $this->formatBuildArguments() ); } diff --git a/src/Docker.php b/src/Docker.php index 2acd9681..9f112988 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -14,18 +14,18 @@ class Docker * @param string $path * @param string $project * @param string $environment - * @param array $cliDockerOptions + * @param array $cliBuildOptions * @param array $cliBuildArgs * @return void */ - public static function build($path, $project, $environment, $cliDockerOptions, $cliBuildArgs) + public static function build($path, $project, $environment, $cliBuildOptions, $cliBuildArgs) { Process::fromShellCommandline( static::buildCommand( $project, $environment, - $cliDockerOptions, - Manifest::dockerOptions($environment), + $cliBuildOptions, + Manifest::dockerBuildOptions($environment), $cliBuildArgs, Manifest::dockerBuildArgs($environment) ), @@ -40,13 +40,13 @@ public static function build($path, $project, $environment, $cliDockerOptions, $ * * @param string $project * @param string $environment - * @param array $cliDockerOptions - * @param array $manifestDockerOptions + * @param array $cliBuildOptions + * @param array $manifestBuildOptions * @param array $cliBuildArgs * @param array $manifestBuildArgs * @return string */ - public static function buildCommand($project, $environment, $cliDockerOptions, $manifestDockerOptions, $cliBuildArgs, $manifestBuildArgs) + public static function buildCommand($project, $environment, $cliBuildOptions, $manifestBuildOptions, $cliBuildArgs, $manifestBuildArgs) { $command = sprintf( 'docker build --pull --file=%s --tag=%s ', @@ -65,7 +65,7 @@ public static function buildCommand($project, $environment, $cliDockerOptions, $ return '--build-arg='.escapeshellarg("{$key}={$value}"); })->implode(' '); - $dockerOptions = Collection::make($manifestDockerOptions) + $buildOptions = Collection::make($manifestBuildOptions) ->mapWithKeys(function ($value) { if (is_array($value)) { return $value; @@ -73,7 +73,7 @@ public static function buildCommand($project, $environment, $cliDockerOptions, $ return [$value => null]; }) - ->merge(Collection::make($cliDockerOptions) + ->merge(Collection::make($cliBuildOptions) ->mapWithKeys(function ($value) { if (! str_contains($value, '=')) { return [$value => null]; @@ -92,7 +92,7 @@ public static function buildCommand($project, $environment, $cliDockerOptions, $ })->implode(' '); $command = $buildArgs ? $command.$buildArgs.' ' : $command; - $command = $dockerOptions ? $command.$dockerOptions.' ' : $command; + $command = $buildOptions ? $command.$buildOptions.' ' : $command; $command .= '.'; return $command; diff --git a/src/Manifest.php b/src/Manifest.php index 7671c6d7..719afd26 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -101,9 +101,9 @@ public static function dockerBuildArgs($environment) * @param string $environment * @return array */ - public static function dockerOptions($environment) + public static function dockerBuildOptions($environment) { - return static::current()['environments'][$environment]['docker-options'] ?? []; + return static::current()['environments'][$environment]['docker-build-options'] ?? []; } /** diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 4ede638d..13ea91ab 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -59,8 +59,8 @@ public function test_build_command_cli_and_manifest_build_args() public function test_build_command_cli_docker_options() { - $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, [], [], []); + $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; + $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, [], [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--BAR='FOO' --FIZZ='BAZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -68,8 +68,8 @@ public function test_build_command_cli_docker_options() public function test_build_command_manifest_docker_options() { - $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', [], $manifestDockerOptions, [], []); + $manifestBuildOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; + $command = Docker::buildCommand('my-project', 'production', [], $manifestBuildOptions, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BUZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -77,9 +77,9 @@ public function test_build_command_manifest_docker_options() public function test_build_command_cli_and_manifest_docker_args() { - $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; - $manifestDockerOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, $manifestDockerOptions, [], []); + $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; + $manifestBuildOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; + $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, $manifestBuildOptions, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BAZZ' --FIZZLY --BUZZLY --BAR='FOO' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -87,9 +87,9 @@ public function test_build_command_cli_and_manifest_docker_args() public function test_build_command_cli_docker_options_and_cli_build_args() { - $cliDockerOptions = ['BAR=FOO', 'FIZZ=BAZZ']; + $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ']; $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; - $command = Docker::buildCommand('my-project', 'production', $cliDockerOptions, [], $cliBuildArgs, []); + $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, [], $cliBuildArgs, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='BAR=FOO' --build-arg='FIZZ=BAZZ' --BAR='FOO' --FIZZ='BAZZ' ."; $this->assertEquals($expectedCommand, $command); From ea6f4276f22d0ceb5eb22f33d4b12c0dbb9f1236 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 3 Aug 2023 11:06:32 +0100 Subject: [PATCH 10/13] revert --- src/BuildProcess/RemoveIgnoredFiles.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/BuildProcess/RemoveIgnoredFiles.php b/src/BuildProcess/RemoveIgnoredFiles.php index 36343eb2..ae2179a5 100644 --- a/src/BuildProcess/RemoveIgnoredFiles.php +++ b/src/BuildProcess/RemoveIgnoredFiles.php @@ -40,7 +40,7 @@ protected function removeDefaultIgnoredFiles() Path::app().'/.env.example', Path::app().'/.phpunit.result.cache', Path::app().'/package-lock.json', - // Path::app().'/phpunit.xml', + Path::app().'/phpunit.xml', Path::app().'/readme.md', Path::app().'/server.php', Path::app().'/storage/oauth-private.key', @@ -132,10 +132,10 @@ protected function removeUserIgnoredFiles() $this->files->deleteDirectory($directory.'/'.$filePattern, $preserve = false); } else { $files = (new Finder()) - ->in($directory) - ->depth('== 0') - ->ignoreDotFiles(false) - ->name($filePattern); + ->in($directory) + ->depth('== 0') + ->ignoreDotFiles(false) + ->name($filePattern); foreach ($files as $file) { Helpers::step('Removing Ignored File: '.str_replace(Path::app().'/', '', $file->getRealPath())); From a5fe7e1e6b995013afc36e94cf0ef0db5cf84540 Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Fri, 4 Aug 2023 14:32:50 +0100 Subject: [PATCH 11/13] chore: output docker build command --- src/Docker.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Docker.php b/src/Docker.php index 9f112988..796007a5 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -20,15 +20,19 @@ class Docker */ public static function build($path, $project, $environment, $cliBuildOptions, $cliBuildArgs) { + $buildCommand = static::buildCommand( + $project, + $environment, + $cliBuildOptions, + Manifest::dockerBuildOptions($environment), + $cliBuildArgs, + Manifest::dockerBuildArgs($environment) + ); + + Helpers::line(sprintf('Build command: %s', $buildCommand)); + Process::fromShellCommandline( - static::buildCommand( - $project, - $environment, - $cliBuildOptions, - Manifest::dockerBuildOptions($environment), - $cliBuildArgs, - Manifest::dockerBuildArgs($environment) - ), + $buildCommand, $path )->setTimeout(null)->mustRun(function ($type, $line) { Helpers::write($line); @@ -49,7 +53,7 @@ public static function build($path, $project, $environment, $cliBuildOptions, $c public static function buildCommand($project, $environment, $cliBuildOptions, $manifestBuildOptions, $cliBuildArgs, $manifestBuildArgs) { $command = sprintf( - 'docker build --pull --file=%s --tag=%s ', + 'docker build --pull --file=%s --tag=%s ', Manifest::dockerfile($environment), Str::slug($project).':'.$environment ); From 64589104d3c324e063a2ba3dc590dda09e600c58 Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Sat, 5 Aug 2023 09:36:30 +0100 Subject: [PATCH 12/13] chore: move args in front of options in function signatures --- src/BuildProcess/BuildContainerImage.php | 6 +++--- src/Commands/BuildCommand.php | 2 +- src/Docker.php | 12 ++++++------ tests/BuildContainerImageTest.php | 4 ++-- tests/DockerTest.php | 16 ++++++++-------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/BuildProcess/BuildContainerImage.php b/src/BuildProcess/BuildContainerImage.php index b46354f3..ea1b0a6f 100644 --- a/src/BuildProcess/BuildContainerImage.php +++ b/src/BuildProcess/BuildContainerImage.php @@ -62,17 +62,17 @@ class BuildContainerImage * Create a new project builder. * * @param string|null $environment - * @param array $cliBuildOptions * @param array $cliBuildArgs + * @param array $cliBuildOptions * @param array $manifestBuildArgs * @return void */ - public function __construct($environment = null, $cliBuildOptions = [], $cliBuildArgs = [], $manifestBuildArgs = []) + public function __construct($environment = null, $cliBuildArgs = [], $cliBuildOptions = [], $manifestBuildArgs = []) { $this->baseConstructor($environment); - $this->cliBuildOptions = $cliBuildOptions; $this->cliBuildArgs = $cliBuildArgs; + $this->cliBuildOptions = $cliBuildOptions; $this->manifestBuildArgs = $manifestBuildArgs; } diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index ede9c268..e9f71802 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -91,8 +91,8 @@ public function handle() new CompressVendor($this->argument('environment')), new BuildContainerImage( $this->argument('environment'), - $this->option('build-option'), $this->option('build-arg'), + $this->option('build-option'), Manifest::dockerBuildArgs($this->argument('environment')) ), ])->each->__invoke(); diff --git a/src/Docker.php b/src/Docker.php index 796007a5..7c56d5e9 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -23,10 +23,10 @@ public static function build($path, $project, $environment, $cliBuildOptions, $c $buildCommand = static::buildCommand( $project, $environment, - $cliBuildOptions, - Manifest::dockerBuildOptions($environment), $cliBuildArgs, - Manifest::dockerBuildArgs($environment) + Manifest::dockerBuildArgs($environment), + $cliBuildOptions, + Manifest::dockerBuildOptions($environment) ); Helpers::line(sprintf('Build command: %s', $buildCommand)); @@ -44,13 +44,13 @@ public static function build($path, $project, $environment, $cliBuildOptions, $c * * @param string $project * @param string $environment - * @param array $cliBuildOptions - * @param array $manifestBuildOptions * @param array $cliBuildArgs * @param array $manifestBuildArgs + * @param array $cliBuildOptions + * @param array $manifestBuildOptions * @return string */ - public static function buildCommand($project, $environment, $cliBuildOptions, $manifestBuildOptions, $cliBuildArgs, $manifestBuildArgs) + public static function buildCommand($project, $environment, $cliBuildArgs, $manifestBuildArgs, $cliBuildOptions, $manifestBuildOptions) { $command = sprintf( 'docker build --pull --file=%s --tag=%s ', diff --git a/tests/BuildContainerImageTest.php b/tests/BuildContainerImageTest.php index cf25c030..1e544f66 100644 --- a/tests/BuildContainerImageTest.php +++ b/tests/BuildContainerImageTest.php @@ -55,7 +55,7 @@ public function test_docker_build_arguments_can_be_formatted_correctly() ], ])); - $buildArgs = (new BuildContainerImage('production', [], ['FOO=BAR', 'BAR=BAZ']))->formatBuildArguments(); + $buildArgs = (new BuildContainerImage('production', ['FOO=BAR', 'BAR=BAZ'], []))->formatBuildArguments(); $this->assertSame(['__VAPOR_RUNTIME=docker', 'FOO=BAR', 'BAR=BAZ'], $buildArgs); } @@ -72,7 +72,7 @@ public function test_runtime_variable_cannot_be_overridden() ], ])); - $buildArgs = (new BuildContainerImage('production', ['__VAPOR_RUNTIME=foo']))->formatBuildArguments(); + $buildArgs = (new BuildContainerImage('production', [], ['__VAPOR_RUNTIME=foo']))->formatBuildArguments(); $this->assertSame(['__VAPOR_RUNTIME=docker'], $buildArgs); } diff --git a/tests/DockerTest.php b/tests/DockerTest.php index 13ea91ab..34729048 100644 --- a/tests/DockerTest.php +++ b/tests/DockerTest.php @@ -24,7 +24,7 @@ protected function tearDown(): void public function test_build_command_no_build_args() { - $command = Docker::buildCommand('my-project', 'production', [], [], [], [], []); + $command = Docker::buildCommand('my-project', 'production', [], [], [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .'; $this->assertEquals($expectedCommand, $command); } @@ -32,7 +32,7 @@ public function test_build_command_no_build_args() public function test_build_command_cli_build_args() { $cliBuildArgs = ['FOO=BAR', 'FIZZ=BUZZ']; - $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, []); + $command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, [], [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); @@ -41,7 +41,7 @@ public function test_build_command_cli_build_args() public function test_build_command_manifest_build_args() { $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; - $command = Docker::buildCommand('my-project', 'production', [], [], [], $manifestBuildArgs); + $command = Docker::buildCommand('my-project', 'production', [], $manifestBuildArgs, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' ."; $this->assertEquals($expectedCommand, $command); @@ -51,7 +51,7 @@ public function test_build_command_cli_and_manifest_build_args() { $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; $manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ']; - $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildArgs, $manifestBuildArgs); + $command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, $manifestBuildArgs, [], []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' ."; $this->assertEquals($expectedCommand, $command); @@ -60,7 +60,7 @@ public function test_build_command_cli_and_manifest_build_args() public function test_build_command_cli_docker_options() { $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, [], [], []); + $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildOptions, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--BAR='FOO' --FIZZ='BAZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -69,7 +69,7 @@ public function test_build_command_cli_docker_options() public function test_build_command_manifest_docker_options() { $manifestBuildOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLE', 'BUZZLE']; - $command = Docker::buildCommand('my-project', 'production', [], $manifestBuildOptions, [], []); + $command = Docker::buildCommand('my-project', 'production', [], [], [], $manifestBuildOptions); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BUZZ' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -79,7 +79,7 @@ public function test_build_command_cli_and_manifest_docker_args() { $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ', 'FIZZLE', 'BUZZLE']; $manifestBuildOptions = [['FOO' => 'BAR'], ['FIZZ' => 'BUZZ'], 'FIZZLY', 'BUZZLY']; - $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, $manifestBuildOptions, [], []); + $command = Docker::buildCommand('my-project', 'production', [], [], $cliBuildOptions, $manifestBuildOptions); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--FOO='BAR' --FIZZ='BAZZ' --FIZZLY --BUZZLY --BAR='FOO' --FIZZLE --BUZZLE ."; $this->assertEquals($expectedCommand, $command); @@ -89,7 +89,7 @@ public function test_build_command_cli_docker_options_and_cli_build_args() { $cliBuildOptions = ['BAR=FOO', 'FIZZ=BAZZ']; $cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ']; - $command = Docker::buildCommand('my-project', 'production', $cliBuildOptions, [], $cliBuildArgs, []); + $command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, [], $cliBuildOptions, []); $expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '. "--build-arg='BAR=FOO' --build-arg='FIZZ=BAZZ' --BAR='FOO' --FIZZ='BAZZ' ."; $this->assertEquals($expectedCommand, $command); From a3a70fe84f5923413162fa14a069deda9fe0f53e Mon Sep 17 00:00:00 2001 From: Chris Neal Date: Wed, 9 Aug 2023 15:55:11 +0100 Subject: [PATCH 13/13] chore: move docker build args in front of build options in signature --- src/BuildProcess/BuildContainerImage.php | 4 ++-- src/Docker.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/BuildProcess/BuildContainerImage.php b/src/BuildProcess/BuildContainerImage.php index ea1b0a6f..b8ac02d8 100644 --- a/src/BuildProcess/BuildContainerImage.php +++ b/src/BuildProcess/BuildContainerImage.php @@ -106,8 +106,8 @@ public function __invoke() $this->appPath, Manifest::name(), $this->environment, - $this->cliBuildOptions, - $this->formatBuildArguments() + $this->formatBuildArguments(), + $this->cliBuildOptions ); } diff --git a/src/Docker.php b/src/Docker.php index 7c56d5e9..cdac0ac2 100644 --- a/src/Docker.php +++ b/src/Docker.php @@ -14,11 +14,11 @@ class Docker * @param string $path * @param string $project * @param string $environment - * @param array $cliBuildOptions * @param array $cliBuildArgs + * @param array $cliBuildOptions * @return void */ - public static function build($path, $project, $environment, $cliBuildOptions, $cliBuildArgs) + public static function build($path, $project, $environment, $cliBuildArgs, $cliBuildOptions) { $buildCommand = static::buildCommand( $project,