Report docker build and tag exit code to halt the tests on failure to build

Signed-off-by: Daniel <daniel@developerdan.com>
This commit is contained in:
Daniel 2020-07-16 14:59:59 -04:00
parent c8781ee427
commit 7ad7b0824f
No known key found for this signature in database
GPG Key ID: 4940B41048AF73EA
1 changed files with 21 additions and 14 deletions

View File

@ -2,11 +2,12 @@
""" Dockerfile.py - generates and build dockerfiles
Usage:
Dockerfile.py [--hub_tag=<tag>] [--arch=<arch> ...] [--debian=<version> ...] [-v] [-t] [--no-build] [--no-cache]
Dockerfile.py [--hub_tag=<tag>] [--arch=<arch> ...] [--debian=<version> ...] [-v] [-t] [--no-build] [--no-cache] [--fail-fast]
Options:
--no-build Skip building the docker images
--no-cache Build without using any cache data
--fail-fast Exit on first build error
--hub_tag=<tag> What the Docker Hub Image should be tagged as [default: None]
--arch=<arch> What Architecture(s) to build [default: amd64 armel armhf arm64]
--debian=<version> What debian version(s) to build [default: stretch buster]
@ -15,10 +16,9 @@ Options:
Examples:
"""
from docopt import docopt
import os
import sys
import subprocess
__version__ = None
@ -28,19 +28,23 @@ with open('{}/VERSION'.format(dot), 'r') as v:
__version__ = raw_version.replace('release/', 'release-')
def build_dockerfiles(args):
def build_dockerfiles(args) -> bool:
all_success = True
if args['-v']:
print(args)
if args['--no-build']:
print(" ::: Skipping Dockerfile building")
return
return all_success
for arch in args['--arch']:
for debian_version in args['--debian']:
build('pihole', arch, debian_version, args['--hub_tag'], args['-t'], args['--no-cache'], args['-v'])
all_success = build('pihole', arch, debian_version, args['--hub_tag'], args['-t'], args['--no-cache'], args['-v']) and all_success
if not all_success and args['--fail-fast']:
return False
return all_success
def run_and_stream_command_output(command, environment_vars, verbose):
def run_and_stream_command_output(command, environment_vars, verbose) -> bool:
print("Running", command)
build_result = subprocess.Popen(command.split(), env=environment_vars, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
@ -50,12 +54,12 @@ def run_and_stream_command_output(command, environment_vars, verbose):
print(line, end='')
build_result.wait()
if build_result.returncode != 0:
print(" ::: Error running {}".format(command))
print(" ::: Error running".format(command))
print(build_result.stderr)
sys.exit(build_resuilt.returncode)
return build_result.returncode == 0
def build(docker_repo: str, arch: str, debian_version: str, hub_tag: str, show_time: bool, no_cache: bool, verbose: bool):
def build(docker_repo: str, arch: str, debian_version: str, hub_tag: str, show_time: bool, no_cache: bool, verbose: bool) -> bool:
create_tag = f'{docker_repo}:{__version__}-{arch}-{debian_version}'
print(f' ::: Building {create_tag}')
time_arg = 'time' if show_time else ''
@ -65,15 +69,18 @@ def build(docker_repo: str, arch: str, debian_version: str, hub_tag: str, show_t
build_env['DEBIAN_VERSION'] = debian_version
build_command = f'{time_arg} docker-compose -f build.yml build {cache_arg} --pull {arch}'
print(f' ::: Building {arch} into {create_tag}')
run_and_stream_command_output(build_command, build_env, verbose)
success = run_and_stream_command_output(build_command, build_env, verbose)
if verbose:
print(build_command, '\n')
if hub_tag:
if success and hub_tag:
hub_tag_command = f'{time_arg} docker tag {create_tag} {hub_tag}'
print(f' ::: Tagging {create_tag} into {hub_tag}')
run_and_stream_command_output(hub_tag_command, build_env, verbose)
success = run_and_stream_command_output(hub_tag_command, build_env, verbose)
return success
if __name__ == '__main__':
args = docopt(__doc__, version='Dockerfile 1.1')
build_dockerfiles(args)
success = build_dockerfiles(args)
exit_code = 0 if success else 1
sys.exit(exit_code)