Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the basepath for PR previews #1691

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ jobs:
ref: ${{ github.event.pull_request.head.ref }}

- name: Build static site
run: scripts/pr_previews/builder.py ${{ env.PR_PREVIEW_PATH }} --proof-of-concept
run: >
scripts/pr_previews/builder.py
${{ env.PR_PREVIEW_PATH }}
--basepath /documentation/${{ env.PR_PREVIEW_PATH }}
--proof-of-concept

- name: Deploy to GitHub Pages
run: scripts/pr_previews/deploy.py ${{ env.PR_PREVIEW_PATH }}
Expand Down
21 changes: 17 additions & 4 deletions scripts/pr_previews/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

import logging
import os
import shutil
from argparse import ArgumentParser
from contextlib import contextmanager
Expand All @@ -32,6 +33,11 @@
def create_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument("dest", help="The output folder", type=Path)
parser.add_argument(
"--basepath",
help="The subfolder for relative links, like `/documentation/pr-x`",
required=True,
)
parser.add_argument(
"--proof-of-concept",
help="Build a simple index.html, rather than actual docs app.",
Expand All @@ -47,7 +53,7 @@ def main() -> None:
return

with setup_dir() as dir:
yarn_build(dir)
yarn_build(dir, args.basepath)
save_output(dir, args.dest)


Expand All @@ -71,15 +77,22 @@ def write_proof_of_concept(dest: Path) -> None:
logger.info(f"Wrote proof-of-concept index.html to {dest}")


def yarn_build(root_dir: Path) -> None:
def yarn_build(root_dir: Path, base_path: str) -> None:
# This ensures that dependencies like Sharp are properly installed. Most
# dependencies, like the first-party deps, will already have been installed.
run_subprocess(["yarn", "install"], cwd=root_dir, stream_output=True)
run_subprocess(["yarn", "build"], cwd=root_dir, stream_output=True)
run_subprocess(
["yarn", "build"],
cwd=root_dir,
env={**os.environ, "NEXT_PUBLIC_BASE_PATH": base_path},
stream_output=True,
)


def save_output(root_dir: Path, dest: Path) -> None:
dest.mkdir(parents=True, exist_ok=True)
if dest.exists():
shutil.rmtree(dest)
dest.mkdir(parents=True)
for item in (root_dir / "packages/preview/out").iterdir():
if item.is_dir():
shutil.copytree(item, dest / item.name)
Expand Down
2 changes: 2 additions & 0 deletions scripts/pr_previews/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def run_subprocess(
cmd: list[str],
*,
cwd: Path | None = None,
env: dict[str, str] | None = None,
stream_output: bool = False,
) -> subprocess.CompletedProcess:
output_dest = None if stream_output else subprocess.PIPE
Expand All @@ -40,6 +41,7 @@ def run_subprocess(
result = subprocess.run(
cmd,
cwd=cwd,
env=env,
stdout=output_dest,
stderr=output_dest,
text=True,
Expand Down