diff --git a/snapcraft/commands/remote.py b/snapcraft/commands/remote.py index cfbaf19d2a..3150208544 100644 --- a/snapcraft/commands/remote.py +++ b/snapcraft/commands/remote.py @@ -237,13 +237,11 @@ def _run(self, parsed_args: argparse.Namespace, **kwargs: Any) -> int | None: ) except RemoteBuildError: emit.progress("Starting build failed.", permanent=True) - emit.progress("Cleaning up") - builder.cleanup() + self._cleanup() raise except lazr.restfulclient.errors.Conflict: emit.progress("Remote repository already exists.", permanent=True) - emit.progress("Cleaning up") - builder.cleanup() + self._cleanup() return 75 try: @@ -254,10 +252,16 @@ def _run(self, parsed_args: argparse.Namespace, **kwargs: Any) -> int | None: builder.cancel_builds() returncode = 0 if returncode != 75: # TimeoutError - emit.progress("Cleaning up") - builder.cleanup() + self._cleanup() return returncode + def _cleanup(self) -> None: + if os.getenv("SNAPCRAFT_REMOTE_BUILD_DISABLE_CLEANUP"): + emit.progress("Not cleaning up remote build.") + else: + emit.progress("Cleaning up") + self._services.remote_build.cleanup() + def _monitor_and_complete( self, build_id: str | None, builds: Collection[Build] ) -> int: