Merge pull request #5698 from docker/artagnon-build-args-services

Allow buildargs for multi-service builds
This commit is contained in:
Joffrey F 2018-02-21 16:59:07 -08:00 committed by GitHub
commit cd3ffe651b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 5 deletions

View File

@ -234,19 +234,21 @@ class TopLevelCommand(object):
--no-cache Do not use cache when building the image.
--pull Always attempt to pull a newer version of the image.
-m, --memory MEM Sets memory limit for the build container.
--build-arg key=val Set build-time variables for one service.
--build-arg key=val Set build-time variables for services.
"""
service_names = options['SERVICE']
build_args = options.get('--build-arg', None)
if build_args:
if not service_names and docker.utils.version_lt(self.project.client.api_version, '1.25'):
raise UserError(
'--build-arg is only supported when services are specified for API version < 1.25.'
' Please use a Compose file version > 2.2 or specify which services to build.'
)
environment = Environment.from_env_file(self.project_dir)
build_args = resolve_build_args(build_args, environment)
if not service_names and build_args:
raise UserError("Need service name for --build-arg option")
self.project.build(
service_names=service_names,
service_names=options['SERVICE'],
no_cache=bool(options.get('--no-cache', False)),
pull=bool(options.get('--pull', False)),
force_rm=bool(options.get('--force-rm', False)),

View File

@ -658,6 +658,33 @@ class CLITestCase(DockerClientTestCase):
result = self.dispatch(['build', '--no-cache', '--memory', '96m', 'service'], None)
assert 'memory: 100663296' in result.stdout # 96 * 1024 * 1024
def test_build_with_buildarg_from_compose_file(self):
pull_busybox(self.client)
self.base_dir = 'tests/fixtures/build-args'
result = self.dispatch(['build'], None)
assert 'Favorite Touhou Character: mariya.kirisame' in result.stdout
def test_build_with_buildarg_cli_override(self):
pull_busybox(self.client)
self.base_dir = 'tests/fixtures/build-args'
result = self.dispatch(['build', '--build-arg', 'favorite_th_character=sakuya.izayoi'], None)
assert 'Favorite Touhou Character: sakuya.izayoi' in result.stdout
@mock.patch.dict(os.environ)
def test_build_with_buildarg_old_api_version(self):
pull_busybox(self.client)
self.base_dir = 'tests/fixtures/build-args'
os.environ['COMPOSE_API_VERSION'] = '1.24'
result = self.dispatch(
['build', '--build-arg', 'favorite_th_character=reimu.hakurei'], None, returncode=1
)
assert '--build-arg is only supported when services are specified' in result.stderr
result = self.dispatch(
['build', '--build-arg', 'favorite_th_character=hong.meiling', 'web'], None
)
assert 'Favorite Touhou Character: hong.meiling' in result.stdout
def test_bundle_with_digests(self):
self.base_dir = 'tests/fixtures/bundle-with-digests/'
tmpdir = pytest.ensuretemp('cli_test_bundle')

4
tests/fixtures/build-args/Dockerfile vendored Normal file
View File

@ -0,0 +1,4 @@
FROM busybox:latest
LABEL com.docker.compose.test_image=true
ARG favorite_th_character
RUN echo "Favorite Touhou Character: ${favorite_th_character}"

View File

@ -0,0 +1,7 @@
version: '2.2'
services:
web:
build:
context: .
args:
- favorite_th_character=mariya.kirisame