Skip to content

Commit 00f4c00

Browse files
authored
Merge pull request #161 from damiantw/docker-build-arg
Support Docker --build-arg option
2 parents afe4aaa + 7d4fbb7 commit 00f4c00

File tree

6 files changed

+114
-8
lines changed

6 files changed

+114
-8
lines changed

src/BuildProcess/BuildContainerImage.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,30 @@
88

99
class BuildContainerImage
1010
{
11-
use ParticipatesInBuildProcess;
11+
use ParticipatesInBuildProcess {
12+
ParticipatesInBuildProcess::__construct as baseConstructor;
13+
}
14+
15+
/**
16+
* The Docker build arguments.
17+
*
18+
* @var array
19+
*/
20+
protected $buildArgs;
21+
22+
/**
23+
* Create a new project builder.
24+
*
25+
* @param string|null $environment
26+
* @param array $buildArgs
27+
* @return void
28+
*/
29+
public function __construct($environment = null, $buildArgs = [])
30+
{
31+
$this->baseConstructor($environment);
32+
33+
$this->buildArgs = $buildArgs;
34+
}
1235

1336
/**
1437
* Execute the build process step.
@@ -23,7 +46,7 @@ public function __invoke()
2346

2447
Helpers::step('<options=bold>Building Container Image</>');
2548

26-
Docker::build($this->appPath, Manifest::name(), $this->environment);
49+
Docker::build($this->appPath, Manifest::name(), $this->environment, $this->buildArgs);
2750
}
2851

2952
/**

src/Commands/BuildCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected function configure()
4343
->setName('build')
4444
->addArgument('environment', InputArgument::OPTIONAL, 'The environment name', 'staging')
4545
->addOption('asset-url', null, InputOption::VALUE_OPTIONAL, 'The asset base URL')
46+
->addOption('build-arg', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build argument')
4647
->setDescription('Build the project archive');
4748
}
4849

@@ -85,7 +86,7 @@ public function handle()
8586
new ExtractVendorToSeparateDirectory($this->argument('environment')),
8687
new CompressApplication($this->argument('environment')),
8788
new CompressVendor($this->argument('environment')),
88-
new BuildContainerImage($this->argument('environment')),
89+
new BuildContainerImage($this->argument('environment'), $this->option('build-arg')),
8990
])->each->__invoke();
9091

9192
$time = (new DateTime())->diff($startedAt)->format('%im%Ss');

src/Commands/DeployCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function configure()
3333
->addOption('message', null, InputOption::VALUE_OPTIONAL, 'The message for the commit that is being deployed')
3434
->addOption('without-waiting', null, InputOption::VALUE_NONE, 'Deploy without waiting for progress')
3535
->addOption('fresh-assets', null, InputOption::VALUE_NONE, 'Upload a fresh copy of all assets')
36+
->addOption('build-arg', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Docker build argument')
3637
->setDescription('Deploy an environment');
3738
}
3839

@@ -119,6 +120,7 @@ protected function buildProject(array $project)
119120
'environment' => $this->argument('environment'),
120121
'--asset-url' => $this->assetDomain($project).'/'.$uuid,
121122
'--manifest' => Path::manifest(),
123+
'--build-arg' => $this->option('build-arg'),
122124
]);
123125

124126
return $this->uploadArtifact(

src/Docker.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\VaporCli;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
67
use Symfony\Component\Process\Process;
78

@@ -15,19 +16,43 @@ class Docker
1516
* @param string $environment
1617
* @return void
1718
*/
18-
public static function build($path, $project, $environment)
19+
public static function build($path, $project, $environment, $cliBuildArgs)
1920
{
2021
Process::fromShellCommandline(
21-
sprintf('docker build --pull --file=%s.Dockerfile --tag=%s .',
22-
$environment,
23-
Str::slug($project).':'.$environment
24-
),
22+
static::buildCommand($project, $environment, $cliBuildArgs, Manifest::dockerBuildArgs($environment)),
2523
$path
2624
)->setTimeout(null)->mustRun(function ($type, $line) {
2725
Helpers::write($line);
2826
});
2927
}
3028

29+
/**
30+
* Create the Docker build command string.
31+
*
32+
* @param string $project
33+
* @param string $environment
34+
* @param array $cliBuildArgs
35+
* @param array $manifestBuildArgs
36+
* @return string
37+
*/
38+
public static function buildCommand($project, $environment, $cliBuildArgs, $manifestBuildArgs)
39+
{
40+
return sprintf('docker build --pull --file=%s.Dockerfile --tag=%s %s.',
41+
$environment,
42+
Str::slug($project).':'.$environment,
43+
Collection::make($manifestBuildArgs)
44+
->merge(Collection::make($cliBuildArgs)
45+
->mapWithKeys(function ($value) {
46+
[$key, $value] = explode('=', $value, 2);
47+
48+
return [$key => $value];
49+
})
50+
)->map(function ($value, $key) {
51+
return '--build-arg='.escapeshellarg("{$key}={$value}").' ';
52+
})->implode('')
53+
);
54+
}
55+
3156
/**
3257
* Publish a docker image.
3358
*

src/Manifest.php

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public static function buildCommands($environment)
5555
return static::current()['environments'][$environment]['build'] ?? [];
5656
}
5757

58+
/**
59+
* Get the Docker build arguments.
60+
*
61+
* @param string $environment
62+
* @return array
63+
*/
64+
public static function dockerBuildArgs($environment)
65+
{
66+
return static::current()['environments'][$environment]['docker-build-args'] ?? [];
67+
}
68+
5869
/**
5970
* Determine if the environment uses a database proxy.
6071
*

tests/DockerTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Laravel\VaporCli\Tests;
4+
5+
use Laravel\VaporCli\Docker;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DockerTest extends TestCase
9+
{
10+
public function test_build_command_no_build_args()
11+
{
12+
$command = Docker::buildCommand('my-project', 'production', [], []);
13+
$expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production .';
14+
$this->assertEquals($expectedCommand, $command);
15+
}
16+
17+
public function test_build_command_cli_build_args()
18+
{
19+
$cliBuildArgs = ['FOO=BAR', 'FIZZ=BUZZ'];
20+
$command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, []);
21+
$expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '.
22+
"--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' .";
23+
$this->assertEquals($expectedCommand, $command);
24+
}
25+
26+
public function test_build_command_manifest_build_args()
27+
{
28+
$manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ'];
29+
$command = Docker::buildCommand('my-project', 'production', [], $manifestBuildArgs);
30+
$expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '.
31+
"--build-arg='FOO=BAR' --build-arg='FIZZ=BUZZ' .";
32+
$this->assertEquals($expectedCommand, $command);
33+
}
34+
35+
public function test_build_command_cli_and_manifest_build_args()
36+
{
37+
$cliBuildArgs = ['BAR=FOO', 'FIZZ=BAZZ'];
38+
$manifestBuildArgs = ['FOO' => 'BAR', 'FIZZ' => 'BUZZ'];
39+
$command = Docker::buildCommand('my-project', 'production', $cliBuildArgs, $manifestBuildArgs);
40+
$expectedCommand = 'docker build --pull --file=production.Dockerfile --tag=my-project:production '.
41+
"--build-arg='FOO=BAR' --build-arg='FIZZ=BAZZ' --build-arg='BAR=FOO' .";
42+
$this->assertEquals($expectedCommand, $command);
43+
}
44+
}

0 commit comments

Comments
 (0)