From 7ad7b0824f469f69026f39c5398451245e60cfd4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 16 Jul 2020 14:59:59 -0400 Subject: [PATCH] Report docker build and tag exit code to halt the tests on failure to build Signed-off-by: Daniel --- Dockerfile.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Dockerfile.py b/Dockerfile.py index b4e36d8..44efac4 100755 --- a/Dockerfile.py +++ b/Dockerfile.py @@ -2,11 +2,12 @@ """ Dockerfile.py - generates and build dockerfiles Usage: - Dockerfile.py [--hub_tag=] [--arch= ...] [--debian= ...] [-v] [-t] [--no-build] [--no-cache] + Dockerfile.py [--hub_tag=] [--arch= ...] [--debian= ...] [-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= What the Docker Hub Image should be tagged as [default: None] --arch= What Architecture(s) to build [default: amd64 armel armhf arm64] --debian= 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)