Skip to content

Commit

Permalink
Merge pull request #6314 from PeterSurda/docker-build-target
Browse files Browse the repository at this point in the history
Add `target` parameter for docker build
  • Loading branch information
p12tic committed Jan 10, 2022
2 parents 7779344 + f482f7e commit b9c5582
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion master/buildbot/test/fake/docker.py
Expand Up @@ -50,11 +50,13 @@ def stop(self, id):
def wait(self, id):
return 0

def build(self, fileobj, pull, tag):
def build(self, fileobj, tag, pull, target):
if fileobj.read() == b'BUG':
pass
elif pull != bool(pull):
pass
elif target != "":
pass
else:
logs = []
for line in logs:
Expand Down
17 changes: 10 additions & 7 deletions master/buildbot/worker/docker.py
Expand Up @@ -166,7 +166,7 @@ def reconfigService(self, name, password, docker_host, image=None,
version=None, tls=None, followStartupLogs=False,
masterFQDN=None, hostconfig=None, autopull=False,
alwaysPull=False, custom_context=False,
encoding='gzip', buildargs=None,
encoding='gzip', target="", buildargs=None,
hostname=None, **kwargs):

yield super().reconfigService(name, password, image, masterFQDN, **kwargs)
Expand All @@ -181,6 +181,7 @@ def reconfigService(self, name, password, docker_host, image=None,
self.alwaysPull = alwaysPull
self.custom_context = custom_context
self.encoding = encoding
self.target = target
self.buildargs = buildargs
# Prepare the parameters for the Docker Client object (except docker_host which is
# renderable and will be available only when starting containers).
Expand Down Expand Up @@ -217,18 +218,20 @@ def _getDockerClient(self, client_args):
def renderWorkerProps(self, build):
return build.render((self.docker_host, self.image, self.dockerfile,
self.volumes, self.hostconfig, self.custom_context,
self.encoding, self.buildargs, self.hostname))
self.encoding, self.target, self.buildargs,
self.hostname))

@defer.inlineCallbacks
def start_instance(self, build):
if self.instance is not None:
raise ValueError('instance active')
docker_host, image, dockerfile, volumes, hostconfig, custom_context, encoding, buildargs, \
docker_host, image, dockerfile, volumes, hostconfig, custom_context, \
encoding, target, buildargs, \
hostname = yield self.renderWorkerPropsOnStart(build)

res = yield threads.deferToThread(self._thd_start_instance, docker_host, image,
dockerfile, volumes, hostconfig, custom_context,
encoding, buildargs, hostname)
encoding, target, buildargs, hostname)
return res

def _image_exists(self, client, name):
Expand All @@ -242,7 +245,7 @@ def _image_exists(self, client, name):
return False

def _thd_start_instance(self, docker_host, image, dockerfile, volumes, host_config,
custom_context, encoding, buildargs, hostname):
custom_context, encoding, target, buildargs, hostname):
curr_client_args = self.client_args.copy()
curr_client_args['base_url'] = docker_host

Expand Down Expand Up @@ -274,12 +277,12 @@ def _thd_start_instance(self, docker_host, image, dockerfile, volumes, host_conf
custom_context=custom_context,
encoding=encoding, tag=image,
pull=self.alwaysPull,
target=target,
buildargs=buildargs)
else:
lines = docker_client.build(
fileobj=BytesIO(dockerfile.encode('utf-8')),
pull=self.alwaysPull,
tag=image,
tag=image, pull=self.alwaysPull, target=target,
)

for line in lines:
Expand Down
4 changes: 4 additions & 0 deletions master/docs/manual/configuration/workers-docker.rst
Expand Up @@ -245,6 +245,10 @@ In addition to the arguments available for any :ref:`Latent-Workers`, :class:`Do
Always pulls (update) image if autopull is set to true.
Also affects the base image specified by `FROM ....` if using a dockerfile, autopull is not needed then.

``target``
(renderable string, optional)
Sets target build stage for multi-stage builds when using a dockerfile.

``custom_context``
(renderable boolean, optional)
Boolean indicating that the user wants to use custom build arguments for the docker environment. Defaults to False.
Expand Down
1 change: 1 addition & 0 deletions newsfragments/multi-stage-dockerfile.feature
@@ -0,0 +1 @@
Added ``target`` support when using ``dockerfile`` parameter of ``DockerLatentWorker``.

0 comments on commit b9c5582

Please sign in to comment.