Skip to content

Commit b4b6f24

Browse files
committed
add no-push option to pretext deploy
1 parent f2d6b86 commit b4b6f24

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

pretext/cli.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,11 +945,12 @@ def callback(actual_port: int) -> None:
945945
)
946946
@nice_errors
947947
@click.pass_context
948-
@click.option("-u", "--update-source", is_flag=True, required=False)
949-
@click.option("-s", "--stage-only", is_flag=True, required=False)
950-
@click.option("-p", "--preview", is_flag=True, required=False)
948+
@click.option("-u", "--update-source", is_flag=True, required=False, help="Commit and push changes tot he source files at the same time as deploying output.")
949+
@click.option("-s", "--stage-only", is_flag=True, required=False, help="Create a staged deployment, but do not deploy.")
950+
@click.option("-p", "--preview", is_flag=True, required=False, help="Preview the staged deployment, but do not actually deploy.")
951+
@click.option("--no-push", is_flag=True, required=False, help="Do not push to remote. Useful for CI/CD workflows or in case of authentication errors.")
951952
def deploy(
952-
ctx: click.Context, update_source: bool, stage_only: bool, preview: bool
953+
ctx: click.Context, update_source: bool, stage_only: bool, preview: bool, no_push: bool
953954
) -> None:
954955
"""
955956
Automatically deploys project to GitHub Pages,
@@ -963,11 +964,13 @@ def deploy(
963964
project = ctx.obj["project"]
964965
project.stage_deployment()
965966
if stage_only:
967+
# Stop here
966968
return
967969
if preview:
970+
# run view command
968971
ctx.invoke(view, stage=True)
969972
else:
970-
project.deploy(update_source=update_source, skip_staging=True)
973+
project.deploy(update_source=update_source, skip_staging=True, no_push=no_push)
971974

972975

973976
# pretext import

pretext/project/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,11 +1610,12 @@ def deploy(
16101610
update_source: bool = True,
16111611
stage_only: bool = False,
16121612
skip_staging: bool = False,
1613+
no_push: bool = False,
16131614
) -> None:
16141615
if not skip_staging:
16151616
self.stage_deployment()
16161617
if not stage_only:
1617-
utils.publish_to_ghpages(self.stage_abspath(), update_source)
1618+
utils.publish_to_ghpages(self.stage_abspath(), update_source, no_push=no_push)
16181619

16191620
def is_git_managed(self) -> bool:
16201621
return (self.abspath() / ".git").exists()

pretext/utils.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def parse_git_remote(string: str) -> t.List[str]:
630630
return repo_info[-2:]
631631

632632

633-
def publish_to_ghpages(directory: Path, update_source: bool) -> None:
633+
def publish_to_ghpages(directory: Path, update_source: bool, no_push: bool = False) -> None:
634634
"""
635635
Publish the current project to GitHub pages.
636636
"""
@@ -666,13 +666,11 @@ def publish_to_ghpages(directory: Path, update_source: bool) -> None:
666666
repo.active_branch.rename("main")
667667
log.info("Successfully initialized new Git repository!")
668668
log.info("")
669-
log.info(f"Preparing to deploy from active `{repo.active_branch.name}` git branch.")
670-
log.info("")
669+
log.info(f"Preparing to deploy from active `{repo.active_branch.name}` git branch.\n")
671670
if repo.bare or repo.is_dirty() or len(repo.untracked_files) > 0:
672671
log.info("Changes to project source since last commit detected.")
673672
if update_source:
674-
log.info("Add/committing these changes to local Git repository.")
675-
log.info("")
673+
log.info("Committing these changes to local Git repository.\n")
676674
repo.git.add(all=True)
677675
repo.git.commit(message="Update to PreTeXt project source.")
678676
# Should we push this commit?
@@ -684,12 +682,10 @@ def publish_to_ghpages(directory: Path, update_source: bool) -> None:
684682
"Just deploying your built project will not save changes to your source on GitHub.`"
685683
)
686684
# Note, we no longer return here; continue with deployment even though repo isn't clean.
687-
688685
try:
689686
origin = repo.remotes.origin
690687
except AttributeError:
691-
log.warning("Remote GitHub repository is not yet configured.")
692-
log.info("")
688+
log.warning("Remote GitHub repository is not yet configured.\n")
693689
log.info(
694690
"And if you haven't already, create a remote GitHub repository for this project at:"
695691
)
@@ -724,6 +720,7 @@ def publish_to_ghpages(directory: Path, update_source: bool) -> None:
724720
directory,
725721
mesg="Latest build deployed.",
726722
nojekyll=True,
723+
no_history=True,
727724
)
728725
log.info(f"Attempting to connect to remote repository at `{origin.url}`...")
729726
# log.info("(Your SSH password may be required.)")
@@ -736,25 +733,36 @@ def publish_to_ghpages(directory: Path, update_source: bool) -> None:
736733
pages_url = f"https://{repo_name}"
737734
else:
738735
pages_url = f"https://{repo_user}.github.io/{repo_name}/"
739-
except Exception:
736+
except Exception as e:
740737
log.error(f"Unable to parse GitHub URL from {origin.url}")
741738
log.error("Deploy unsuccessful")
739+
log.debug(e, exc_info=True)
742740
return
741+
# Stop here if we are not pushing to GitHub.
742+
if no_push:
743+
log.info(
744+
"Skipping push to GitHub. You can push manually with `git push origin gh-pages`."
745+
)
746+
log.info("")
747+
log.info("Output committed to the `gh-pages` branch.")
748+
return
749+
# Otherwise we try to push to GitHub.
743750
try:
744751
origin.push(refspec=f"{repo.active_branch.name}:{repo.active_branch.name}")
745752
origin.push(refspec="gh-pages:gh-pages")
746-
except git.exc.GitCommandError: # type: ignore
753+
except git.exc.GitCommandError as e: # type: ignore
747754
log.warning(
748-
f"There was an issue connecting to GitHub repository located at {repo_url}"
755+
f"There was an issue connecting to GitHub repository located at {repo_url}\n"
749756
)
750-
log.info("")
757+
log.debug(e, exc_info=True)
751758
log.info(
752759
"Make sure you have set up authentication for GitHub. For more information, visit:"
753760
)
754761
log.info(" https://docs.github.com/en/authentication")
755762
log.info(
756-
"Make sure you can push changes, either from the command line or in VS Code. Then try to deploy again."
763+
"Make sure you can push changes, either from the command line or in VS Code. Then try to deploy again.\n"
757764
)
765+
log.info("You can also try to run `pretext deploy --no-push` to skip this step, then switch to the `gh-pages` branch (using `git checkout gh-pages`) and push manually.")
758766
log.info("")
759767
log.info(f"(If `{origin.url}` doesn't match your GitHub repository,")
760768
log.info(

0 commit comments

Comments
 (0)