diff --git a/readthedocs/doc_builder/director.py b/readthedocs/doc_builder/director.py index dc7646db99d..79388cc0f64 100644 --- a/readthedocs/doc_builder/director.py +++ b/readthedocs/doc_builder/director.py @@ -65,7 +65,30 @@ def setup_vcs(self): ), ) - environment = self.data.environment_class( + before_vcs.send( + sender=self.data.version, + environment=self.vcs_environment, + ) + + # Create the VCS repository where all the commands are going to be + # executed for a particular VCS type + self.vcs_repository = self.data.project.vcs_repo( + version=self.data.version.slug, + environment=self.vcs_environment, + verbose_name=self.data.version.verbose_name, + version_type=self.data.version.type, + ) + + self.pre_checkout() + self.checkout() + self.post_checkout() + + commit = self.data.build_commit or self.vcs_repository.commit + if commit: + self.data.build["commit"] = commit + + def create_vcs_environment(self): + self.vcs_environment = self.data.environment_class( project=self.data.project, version=self.data.version, build=self.data.build, @@ -74,28 +97,6 @@ def setup_vcs(self): # ca-certificate package which is compatible with Lets Encrypt container_image=settings.RTD_DOCKER_BUILD_SETTINGS["os"]["ubuntu-20.04"], ) - with environment: - before_vcs.send( - sender=self.data.version, - environment=environment, - ) - - # Create the VCS repository where all the commands are going to be - # executed for a particular VCS type - self.vcs_repository = self.data.project.vcs_repo( - version=self.data.version.slug, - environment=environment, - verbose_name=self.data.version.verbose_name, - version_type=self.data.version.type, - ) - - self.pre_checkout() - self.checkout() - self.post_checkout() - - commit = self.data.build_commit or self.vcs_repository.commit - if commit: - self.data.build["commit"] = commit def create_build_environment(self): self.build_environment = self.data.environment_class( diff --git a/readthedocs/projects/tasks/builds.py b/readthedocs/projects/tasks/builds.py index cb97fcb6bbc..24ecbab3052 100644 --- a/readthedocs/projects/tasks/builds.py +++ b/readthedocs/projects/tasks/builds.py @@ -543,11 +543,20 @@ def execute(self): # Clonning self.update_build(state=BUILD_STATE_CLONING) - self.data.build_director.setup_vcs() - # Sync tags/branches from VCS repository into Read the Docs' `Version` - # objects in the database - self.sync_versions(self.data.build_director.vcs_repository) + # TODO: remove the ``create_vcs_environment`` hack. Ideally, this should be + # handled inside the ``BuildDirector`` but we can't use ``with + # self.vcs_environment`` twice because it kills the container on + # ``__exit__`` + self.data.build_director.create_vcs_environment() + with self.data.build_director.vcs_environment: + self.data.build_director.setup_vcs() + + # Sync tags/branches from VCS repository into Read the Docs' + # `Version` objects in the database. This method runs commands + # (e.g. "hg tags") inside the VCS environment, so it requires to be + # inside the `with` statement + self.sync_versions(self.data.build_director.vcs_repository) # TODO: remove the ``create_build_environment`` hack. Ideally, this should be # handled inside the ``BuildDirector`` but we can't use ``with