Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker build output to task result #805

Merged
merged 1 commit into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/805-docker_image-build-output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_image - return docker build output (https://github.com/ansible-collections/community.general/pull/805)."
22 changes: 17 additions & 5 deletions plugins/modules/cloud/docker/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@
returned: success
type: dict
sample: {}
stdout:
description: Docker build output when building an image.
returned: success
type: str
sample: ""
raags marked this conversation as resolved.
Show resolved Hide resolved
version_added: 1.3.0
'''

import errno
Expand Down Expand Up @@ -506,7 +512,8 @@ def present(self):
self.results['actions'].append("Built image %s from %s" % (image_name, self.build_path))
self.results['changed'] = True
if not self.check_mode:
self.results['image'] = self.build_image()
self.results.update(self.build_image())

elif self.source == 'load':
# Load the image from an archive
if not os.path.isfile(self.load_path):
Expand Down Expand Up @@ -713,7 +720,7 @@ def build_image(self):
)
if self.client.docker_py_version < LooseVersion('3.0.0'):
params['stream'] = True
build_output = []

if self.tag:
params['tag'] = "%s:%s" % (self.name, self.tag)
if self.container_limits:
Expand All @@ -737,11 +744,14 @@ def build_image(self):
if self.target:
params['target'] = self.target

build_output = []
for line in self.client.build(**params):
# line = json.loads(line)
self.log(line, pretty_print=True)
if "stream" in line:
build_output.append(line["stream"])
if "stream" in line or "status" in line:
build_line = line.get("stream") or line.get("status")
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
build_output.append(build_line)

if line.get('error'):
if line.get('errorDetail'):
errorDetail = line.get('errorDetail')
Expand All @@ -754,7 +764,9 @@ def build_image(self):
else:
self.fail("Error building %s - message: %s, logs: %s" % (
self.name, line.get('error'), build_output))
return self.client.find_image(name=self.name, tag=self.tag)

return {"stdout": "\n".join(build_output),
"image": self.client.find_image(name=self.name, tag=self.tag)}

def load_image(self):
'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
- repository_1 is changed
- repository_2 is not changed

# Uncomment in community.docker
# - assert:
# that:
# - 'FROM busybox' in repository_1.stdout

- name: Get facts of image
docker_image_info:
name: "{{ test_image_base }}:latest"
Expand Down