From 8901ba8b10137d80f2c605a741012cdd8d59e158 Mon Sep 17 00:00:00 2001 From: "Jonny Browning (Datatonic)" <78856444+browningjp-datatonic@users.noreply.github.com> Date: Mon, 24 Oct 2022 21:35:42 +0100 Subject: [PATCH] feat(sdk): add --build-image option to 'kfp components build' to allow users to skip docker build. Fixes #8382 for 1.8 (#8383) --- sdk/python/kfp/cli/components.py | 11 ++++++++--- sdk/python/kfp/cli/components_test.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/sdk/python/kfp/cli/components.py b/sdk/python/kfp/cli/components.py index 2047ca8f68e..7a2723ffc03 100644 --- a/sdk/python/kfp/cli/components.py +++ b/sdk/python/kfp/cli/components.py @@ -369,6 +369,9 @@ def build(components_directory: pathlib.Path = typer.Argument( False, help="Set this to true to always generate a Dockerfile" " as part of the build process"), + build_image: bool = typer.Option( + True, help="Build the container image." + ), push_image: bool = typer.Option( True, help="Push the built image to its remote repository.")): """ @@ -380,11 +383,11 @@ def build(components_directory: pathlib.Path = typer.Argument( components_directory)) raise typer.Exit(1) - if engine != _Engine.DOCKER: + if build_image and (engine != _Engine.DOCKER): _error('Currently, only `docker` is supported for --engine.') raise typer.Exit(1) - if engine == _Engine.DOCKER: + if build_image and (engine == _Engine.DOCKER): if not _DOCKER_IS_PRESENT: _error( 'The `docker` Python package was not found in the current' @@ -404,7 +407,9 @@ def build(components_directory: pathlib.Path = typer.Argument( builder.maybe_generate_requirements_txt() builder.maybe_generate_dockerignore() builder.maybe_generate_dockerfile(overwrite_dockerfile=overwrite_dockerfile) - builder.build_image(push_image=push_image) + + if build_image: + builder.build_image(push_image=push_image) if __name__ == '__main__': diff --git a/sdk/python/kfp/cli/components_test.py b/sdk/python/kfp/cli/components_test.py index fca072ea3f6..9f5a70b914c 100644 --- a/sdk/python/kfp/cli/components_test.py +++ b/sdk/python/kfp/cli/components_test.py @@ -386,6 +386,20 @@ def testDockerClientIsCalledToBuildButSkipsPushing(self): self._docker_client.api.build.assert_called_once() self._docker_client.images.push.assert_not_called() + def testDockerClientIsNotCalledToBuild(self): + component = _make_component( + func_name='train', target_image='custom-image') + _write_components('components.py', component) + + result = self._runner.invoke( + self._app, + ['build', str(self._working_dir), '--no-build-image'], + ) + self.assertEqual(result.exit_code, 0) + + self._docker_client.api.build.assert_not_called() + self._docker_client.images.push.assert_not_called() + @mock.patch('kfp.__version__', '1.2.3') def testDockerfileIsCreatedCorrectly(self): component = _make_component(