From 10807c12c44d2c41ffcc0ea83bba73f4806844be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Srokosz?= Date: Thu, 1 Feb 2024 16:57:10 +0100 Subject: [PATCH] Add --build-args option to pass extra arguments to 'docker build' (#8) --- setup.py | 2 +- src/deploy.py | 9 ++++++++- src/docker.py | 7 ++++++- src/services.py | 9 +++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 70540ea..ca8961e 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name="python-deploy", - version="4.0.2", + version="4.1.0", author="msm, psrok1", author_email="info@cert.pl", description="Build, push and deploy k8s services with single " diff --git a/src/deploy.py b/src/deploy.py index 53626cc..00b6745 100755 --- a/src/deploy.py +++ b/src/deploy.py @@ -5,6 +5,7 @@ import logging import os import pathlib +import shlex import subprocess from datetime import datetime from typing import List, Optional @@ -77,7 +78,10 @@ def build(self) -> None: extra_tags = self.args.tag or [] for service in self.config.get_services(): logger.info(f"Building image for {service.service_name}") - service.build_docker([self.version_tag] + extra_tags, self.args.no_cache) + extra_args = shlex.split(self.args.build_args or "") + service.build_docker( + [self.version_tag] + extra_tags, self.args.no_cache, extra_args + ) def push(self) -> None: self.build() @@ -182,6 +186,9 @@ def main(): action="store_true", help="Pass --no-cache to docker build", ) + build_subparser.add_argument( + "--build-args", help="Extra arguments for 'docker build' command" + ) deploy_subparser = argparse.ArgumentParser(add_help=False) deploy_subparser.add_argument( diff --git a/src/docker.py b/src/docker.py index 3ccae20..eeca8f1 100644 --- a/src/docker.py +++ b/src/docker.py @@ -11,7 +11,11 @@ def tag_image(existing_image: str, new_tag: str, push: bool = False) -> None: def build_image( - dockerfile: str, context_dir: str, tags: List[str], no_cache: bool + dockerfile: str, + context_dir: str, + tags: List[str], + no_cache: bool, + extra_args: List[str], ) -> None: logger.info(f"Building {', '.join(tags)}") check_call( @@ -24,6 +28,7 @@ def build_image( "-f", dockerfile, ["--no-cache"] if no_cache else [], + extra_args, ] ) diff --git a/src/services.py b/src/services.py index b011b2e..22533d6 100644 --- a/src/services.py +++ b/src/services.py @@ -181,7 +181,9 @@ def get_docker_tags(self, tags: List[str]) -> List[str]: tag if ":" in tag else f'{self.docker_spec["image"]}:{tag}' for tag in tags ] - def build_docker(self, tags: List[str], no_cache: bool = False) -> None: + def build_docker( + self, tags: List[str], no_cache: bool = False, extra_args: List[str] = None + ) -> None: if not self.docker_spec: raise DeployError( f"Invalid specification of {self.service_name}. " @@ -189,7 +191,10 @@ def build_docker(self, tags: List[str], no_cache: bool = False) -> None: ) dockerfile = self.docker_spec.get("dockerfile", "./Dockerfile") context_dir = self.docker_spec.get("dir", ".") - build_image(dockerfile, context_dir, self.get_docker_tags(tags), no_cache) + extra_args = extra_args or [] + build_image( + dockerfile, context_dir, self.get_docker_tags(tags), no_cache, extra_args + ) def push_docker(self, tags: List[str]) -> None: for tag in self.get_docker_tags(tags):