Skip to content

Commit

Permalink
Merge pull request #388 from kalessin/build_args
Browse files Browse the repository at this point in the history
allow to pass build arguments to docker client
  • Loading branch information
vshlapakov authored Sep 15, 2020
2 parents 669aa1f + a2adf6a commit 700a02f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
10 changes: 7 additions & 3 deletions shub/image/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@
@click.option("-S", "--skip-tests", help="skip testing image", is_flag=True)
@click.option("-n", "--no-cache", is_flag=True,
help="Do not use cache when building the image")
@click.option("-b", "--build-arg", multiple=True,
help="Allow to pass build arguments to docker client.")
@click.option("-f", "--file", "filename", default='Dockerfile',
help="Name of the Dockerfile (Default is 'PATH/Dockerfile')")
def cli(target, debug, verbose, version, skip_tests, no_cache, filename):
build_cmd(target, version, skip_tests, no_cache, filename=filename)
def cli(target, debug, verbose, version, skip_tests, no_cache, build_arg, filename):
build_cmd(target, version, skip_tests, no_cache, build_arg, filename=filename)


def build_cmd(target, version, skip_tests, no_cache, filename='Dockerfile'):
def build_cmd(target, version, skip_tests, no_cache, build_arg, filename='Dockerfile'):
config = load_shub_config()
create_scrapinghub_yml_wizard(config, target=target, image=True)
client = utils.get_docker_client()
project_dir = utils.get_project_dir()
image = config.get_image(target)
image_name = utils.format_image_name(image, version)
build_args = dict(a.split('=', 1) for a in build_arg)
if not os.path.exists(os.path.join(project_dir, filename)):
raise shub_exceptions.NotFoundException(
"Dockerfile is not found and it is required because project '{}' is configured "
Expand All @@ -72,6 +75,7 @@ def build_cmd(target, version, skip_tests, no_cache, filename='Dockerfile'):
dockerfile=filename,
nocache=no_cache,
rm=True,
buildargs=build_args,
)
build_progress = build_progress_cls(events)
build_progress.show()
Expand Down
10 changes: 6 additions & 4 deletions shub/image/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@
help="re-authenticate to registry before pushing")
@click.option("-n", "--no-cache", is_flag=True,
help="Do not use cache when building the image")
@click.option("-b", "--build-arg", multiple=True,
help="Allow to pass build arguments to docker client.")
@click.option("-f", "--file", "filename", default='Dockerfile',
help="Name of the Dockerfile (Default is 'PATH/Dockerfile')")
def cli(target, debug, verbose, version, username, password, email,
apikey, insecure, async_, skip_tests, reauth, no_cache, filename):
apikey, insecure, async_, skip_tests, reauth, no_cache, build_arg, filename):
upload_cmd(target, version, username, password, email, apikey, insecure,
async_, skip_tests, reauth, no_cache, filename)
async_, skip_tests, reauth, no_cache, build_arg, filename)


def upload_cmd(target, version, username=None, password=None, email=None,
apikey=None, insecure=False, async_=False, skip_tests=False,
reauth=False, no_cache=False, filename='Dockerfile'):
build.build_cmd(target, version, skip_tests, no_cache, filename=filename)
reauth=False, no_cache=False, build_arg=(), filename='Dockerfile'):
build.build_cmd(target, version, skip_tests, no_cache, build_arg, filename=filename)
# skip tests for push command anyway because they run in build command if not skipped
push.push_cmd(target, version, username, password, email, apikey,
insecure, skip_tests=True, reauth=reauth)
Expand Down
36 changes: 31 additions & 5 deletions tests/image/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_cli(docker_client_mock, project_dir, test_mock):
tag='registry.io/user/project:1.0',
dockerfile='Dockerfile',
nocache=False,
rm=True
rm=True,
buildargs={}
)
test_mock.assert_called_with("dev", None)

Expand All @@ -50,7 +51,29 @@ def test_cli_with_nocache(docker_client_mock, project_dir, test_mock):
tag='registry.io/user/project:1.0',
dockerfile='Dockerfile',
nocache=True,
rm=True
rm=True,
buildargs={}
)
test_mock.assert_called_with("dev", None)


def test_cli_with_buildargs(docker_client_mock, project_dir, test_mock):
docker_client_mock.build.return_value = [
{"stream": "all is ok"},
{"stream": "Successfully built 12345"}
]
runner = CliRunner()
result = runner.invoke(cli, ["dev", "-v", "-b", "AWS_KEY=asdasdeg", "-b",
"AWS_SEC=ashthku", "-b", "PARAM=query=4"])
assert result.exit_code == 0
docker_client_mock.build.assert_called_with(
decode=True,
path=project_dir,
tag='registry.io/user/project:1.0',
dockerfile='Dockerfile',
nocache=False,
rm=True,
buildargs={'AWS_KEY': 'asdasdeg', 'AWS_SEC': 'ashthku', 'PARAM': 'query=4'}
)
test_mock.assert_called_with("dev", None)

Expand Down Expand Up @@ -90,7 +113,8 @@ def test_cli_custom_version(docker_client_mock, project_dir, test_mock):
tag='registry.io/user/project:test',
dockerfile='Dockerfile',
nocache=False,
rm=True
rm=True,
buildargs={}
)
test_mock.assert_called_with("dev", "test")

Expand Down Expand Up @@ -131,7 +155,8 @@ def test_cli_skip_tests(docker_client_mock, test_mock, project_dir, skip_tests_f
tag='registry.io/user/project:1.0',
dockerfile='Dockerfile',
nocache=False,
rm=True
rm=True,
buildargs={}
)
assert test_mock.call_count == 0

Expand All @@ -151,7 +176,8 @@ def test_cli_custom_dockerfile(docker_client_mock, project_dir, test_mock, file_
tag='registry.io/user/project:1.0',
dockerfile='Dockerfile',
nocache=False,
rm=True
rm=True,
buildargs={}
)
test_mock.assert_called_with("dev", None)

Expand Down
2 changes: 1 addition & 1 deletion tests/image/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_cli(self, build, push, deploy):
"--email", "mail", "--async", "--apikey", "apikey",
"--skip-tests", "--no-cache", "-f", "Dockerfile", "--reauth"])
assert result.exit_code == 0
build.assert_called_with('dev', 'test', True, True, filename='Dockerfile')
build.assert_called_with('dev', 'test', True, True, (), filename='Dockerfile')
push.assert_called_with(
'dev', 'test', 'user', 'pass', 'mail', "apikey", False, reauth=True,
skip_tests=True)
Expand Down

0 comments on commit 700a02f

Please sign in to comment.