Skip to content

Commit

Permalink
Add docker build output to task result
Browse files Browse the repository at this point in the history
The build output is only added during failures, but its useful to
have this available during normal task execution as well.
  • Loading branch information
raags committed Nov 15, 2020
1 parent 726aa5e commit 276ae03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
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: ""
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")
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

0 comments on commit 276ae03

Please sign in to comment.