Skip to content

Commit

Permalink
fix: publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 6, 2024
1 parent d3f9245 commit cbdb05f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
5 changes: 3 additions & 2 deletions sphinx_ape/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ def test(base_path):
@click.argument("base_path", type=Path)
@click.argument("repository")
@click.option("--cicd", is_flag=True)
def publish(base_path, repository, cicd):
@click.option("--skip-add-commit-push", is_flag=True)
def publish(base_path, repository, cicd, skip_add_commit_push):
"""
Publish docs
"""
builder = _create_builder(mode=BuildMode.RELEASE, base_path=base_path)
try:
builder.publish(repository, cicd=cicd)
builder.publish(repository, cicd=cicd, git_acp=not skip_add_commit_push)
except ApeDocsPublishError as err:
click.echo(f"ERROR: {err}", err=True)
sys.exit(1)
Expand Down
52 changes: 32 additions & 20 deletions sphinx_ape/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,43 +97,30 @@ def build(self):

self._setup_redirect()

def publish(self, repository: str, cicd: bool = False):
def publish(self, repository: str, cicd: bool = False, git_acp: bool = True):
"""
Publish the documentation to GitHub pages.
Meant to be run in CI/CD on releases.
Args:
repository (str): The repository name,
repository (str): The repository name.
cicd (bool): The action sets this to ``True``.
git_acp (bool): Set to ``False`` to skip git add, commit, and push.
Raises:
:class:`~sphinx_ape.exceptions.ApeDocsPublishError`: When
publishing fails.
"""
try:
self._publish(repository, cicd=cicd)
self._publish(repository, cicd=cicd, git_acp=git_acp)
except Exception as err:
raise ApeDocsPublishError(str(err)) from err

def _publish(self, repository: str, cicd: bool = False):
def _publish(self, repository: str, cicd: bool = False, git_acp=True):
if self.mode is not BuildMode.RELEASE:
# Nothing to do for "LATEST/"
return

repo_url = f"https://github.com/{repository}"
git(
"clone",
repo_url,
"--branch",
self._pages_branch_name,
"--single-branch",
self._pages_branch_name,
)
shutil.copytree(self.build_path, "gh-pages/")
os.chdir("gh-pages/")
no_jykell_file = Path(".nojekyll")
no_jykell_file.touch(exist_ok=True)

if cicd:
# Must configure the email / username.
git(
Expand All @@ -149,8 +136,33 @@ def _publish(self, repository: str, cicd: bool = False):
"GitHub Action",
)

git("add", ".")
git("commit", "-m", "Update documentation", "-a")
repo_url = f"https://github.com/{repository}"
gh_pages_path = Path.cwd() / "gh-pages"
git(
"clone",
repo_url,
"--branch",
self._pages_branch_name,
"--single-branch",
self._pages_branch_name,
)
try:
for path in self.build_path.iterdir():
if not path.is_dir() or path.name.startswith(".") or path.name == "doctest":
continue

shutil.copytree(path, gh_pages_path / path.name, dirs_exist_ok=True)

os.chdir(str(gh_pages_path))
no_jykell_file = Path(".nojekyll")
no_jykell_file.touch(exist_ok=True)
if git_acp:
git("add", ".")
git("commit", "-m", "Update documentation", "-a")
git("push")

finally:
shutil.rmtree(gh_pages_path, ignore_errors=True)

def _build_release(self):
if not (tag := git("describe", "--tag")):
Expand Down

0 comments on commit cbdb05f

Please sign in to comment.