Skip to content

Commit

Permalink
Add build.target option to docker_image module (ansible#58487)
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Bardoux <[email protected]>
  • Loading branch information
Pluggi authored and ansibot committed Jul 2, 2019
1 parent 4da6d8c commit 7f15331
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_image - Add ``build.target`` option."
14 changes: 14 additions & 0 deletions lib/ansible/modules/cloud/docker/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
be set in the container being built.
- Needs Docker SDK for Python >= 3.7.0.
type: bool
target:
description:
- When building an image specifies an intermediate build stage by
name as a final stage for the resulting image.
type: str
version_added: "2.9"
version_added: "2.8"
archive_path:
description:
Expand Down Expand Up @@ -461,6 +467,7 @@ def __init__(self, client, results):
self.nocache = build.get('nocache', False)
self.build_path = build.get('path')
self.pull = build.get('pull')
self.target = build.get('target')
self.repository = parameters.get('repository')
self.rm = build.get('rm', True)
self.state = parameters.get('state')
Expand Down Expand Up @@ -732,6 +739,8 @@ def build_image(self):
# use_config_proxy is True and buildargs is None
if 'buildargs' not in params:
params['buildargs'] = {}
if self.target:
params['target'] = self.target

for line in self.client.build(**params):
# line = json.loads(line)
Expand Down Expand Up @@ -798,6 +807,7 @@ def main():
rm=dict(type='bool', default=True),
args=dict(type='dict'),
use_config_proxy=dict(type='bool'),
target=dict(type='str'),
)),
archive_path=dict(type='path'),
container_limits=dict(type='dict', options=dict(
Expand Down Expand Up @@ -838,12 +848,16 @@ def detect_build_cache_from(client):
def detect_build_network(client):
return client.module.params['build'] and client.module.params['build'].get('network') is not None

def detect_build_target(client):
return client.module.params['build'] and client.module.params['build'].get('target') is not None

def detect_use_config_proxy(client):
return client.module.params['build'] and client.module.params['build'].get('use_config_proxy') is not None

option_minimal_versions = dict()
option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from)
option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network)
option_minimal_versions["build.target"] = dict(docker_py_version='2.4.0', detect_usage=detect_build_target)
option_minimal_versions["build.use_config_proxy"] = dict(docker_py_version='3.7.0', detect_usage=detect_use_config_proxy)

client = AnsibleDockerClient(
Expand Down
7 changes: 7 additions & 0 deletions test/integration/targets/docker_image/files/StagedDockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM busybox AS first
ENV dir /first
WORKDIR ${dir}

FROM busybox AS second
ENV dir /second
WORKDIR ${dir}
26 changes: 26 additions & 0 deletions test/integration/targets/docker_image/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,29 @@
that:
- path_1 is changed
- path_2 is not changed

####################################################################
## target ##########################################################
####################################################################

- name: Build multi-stage image
docker_image:
name: "{{ iname }}"
build:
path: "{{ role_path }}/files"
dockerfile: "StagedDockerfile"
target: first
pull: no
source: build
register: dockerfile_2

- name: cleanup
docker_image:
name: "{{ iname }}"
state: absent
force_absent: yes

- assert:
that:
- dockerfile_2 is changed
- dockerfile_2.image.Config.WorkingDir == '/first'

0 comments on commit 7f15331

Please sign in to comment.