Skip to content

Commit

Permalink
Add --build-args option to pass extra arguments to 'docker build' (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 authored Feb 1, 2024
1 parent 591d95e commit 10807c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
9 changes: 8 additions & 1 deletion src/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import pathlib
import shlex
import subprocess
from datetime import datetime
from typing import List, Optional
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion src/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -24,6 +28,7 @@ def build_image(
"-f",
dockerfile,
["--no-cache"] if no_cache else [],
extra_args,
]
)

Expand Down
9 changes: 7 additions & 2 deletions src/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,20 @@ 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}. "
f"Missing docker image specification."
)
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):
Expand Down

0 comments on commit 10807c1

Please sign in to comment.