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

Netlify to GCS migration for Portfolio site #1327

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
69 changes: 41 additions & 28 deletions portfolio/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
GOOGLE_ANALYTICS_TAG_ID = "G-JCX3Z8JZJC"
PORTFOLIO_DIR = Path("./portfolio/")
SITES_DIR = PORTFOLIO_DIR / Path("sites")
PROD_GCS_BUCKET = os.getenv("GCS_BUCKET", "calitp-data-analyses-portfolio")
DRAFT_GCS_BUCKET = os.getenv("GCS_BUCKET", "calitp-data-analyses-portfolio-draft")

SiteChoices = enum.Enum('SiteChoices', {
f.replace(".yml", ""): f.replace(".yml", "")
Expand All @@ -34,10 +36,10 @@

DEPLOY_OPTION = typer.Option(
False,
help="Actually deploy this component to netlify.",
help="Actually deploy this component to the static web server.",
)

app = typer.Typer(help="CLI to tie together papermill, jupyter book, and netlify")
app = typer.Typer(help="CLI to tie together papermill, jupyter book, and the static web server")

env = Environment(loader=FileSystemLoader("./portfolio/templates/"), autoescape=select_autoescape())

Expand Down Expand Up @@ -225,12 +227,12 @@ def __init__(self, **data):

@validator("name")
def convert_to_underscores(cls, v):
# netlify converts stuff to underscores so we should do it too
# some web servers (such as netlify) convert stuff to underscores so we should do it too
return v.replace("_", "-")

@validator('readme', pre=True, always=True)
def default_readme(cls, v, *, values, **kwargs):
if "./" in v:
if "./" in v:
return Path(v)
else:
return (values['directory'] / Path("README.md")) or (values['directory'] / Path(v))
Expand Down Expand Up @@ -281,24 +283,24 @@ def execute_managed_notebook(cls, nb_man, kernel_name, **kwargs):
# hide input (i.e. code) for all cells
if cell.cell_type == "code":
cell.metadata.tags.append("remove_input")

# Consider importing this name from calitp.magics
if '%%capture_parameters' in cell.source:
params = {**params, **json.loads(cell.outputs[0]['text'])}

if "%%capture" in cell.source:
cell.outputs = []

if no_stderr:
cell.outputs = [output for output in cell.outputs if 'name' not in output.keys() or output['name'] != 'stderr']
# right side widget to add "tags" (it reverts to "tags": ["tags"]),
if cell.metadata.get("tags"):

# right side widget to add "tags" (it reverts to "tags": ["tags"]),
if cell.metadata.get("tags"):
#"%%full_width" in cell.source doesn't pick up
# when Jupyterbook builds, it says
# when Jupyterbook builds, it says
# UsageError: Line magic function `%%full_width` not found.
cell.metadata.tags.append("full-width")

papermill_engines.register("markdown", EngineWithParameterizedMarkdown)
papermill_engines.register_entry_points()

Expand All @@ -315,31 +317,35 @@ def index(
name = site.replace(".yml", "")
site_output_dir = PORTFOLIO_DIR / Path(name)
sites.append(Site(output_dir=site_output_dir, name=name, **yaml.safe_load(f)))

Path("./portfolio/index").mkdir(parents=True, exist_ok=True)
for template in ["index.html", "_redirects"]:
fname = f"./portfolio/index/{template}"
with open(fname, "w") as f:
typer.echo(f"writing out to {fname}")
f.write(env.get_template(template).render(sites=sites, google_analytics_id=GOOGLE_ANALYTICS_TAG_ID))

args = [
"netlify",
"deploy",
"--site=cal-itp-data-analyses",
"--dir=portfolio/index",
]
bucket_path = PROD_GCS_BUCKET if prod else DRAFT_GCS_BUCKET

if alias:
args.append(f"--alias={alias}")
bucket_path += f"/{alias}"

if prod:
args.append("--prod")
args = [
"gcloud",
"storage",
"cp",
"--recursive",
str(PORTFOLIO_DIR / "index" / "*"),
f"gs://{bucket_path}",
]

if deploy:
typer.secho(f"deploying with args {args}", fg=typer.colors.GREEN)
subprocess.run(args).check_returncode()

https_root = f"https://storage.googleapis.com/{bucket_path}"
typer.secho(f"deployed {'to production' if prod else 'as a draft'}: {https_root}/index.html", fg=typer.colors.GREEN)


@app.command()
def clean(
Expand All @@ -356,6 +362,7 @@ def build(
site: SiteChoices,
continue_on_error: bool = False,
deploy: bool = DEPLOY_OPTION,
prod: bool = False,
execute_papermill: bool = typer.Option(
True,
help="If false, will skip calls to papermill.",
Expand Down Expand Up @@ -420,19 +427,25 @@ def build(
ans = input(f"{len(errors)} encountered during papermill; enter that number to continue: ")
assert int(ans) == len(errors)

bucket_path = PROD_GCS_BUCKET if prod else DRAFT_GCS_BUCKET

args = [
"netlify",
"deploy",
"--site=cal-itp-data-analyses",
f"--dir=portfolio/{site_yml_name}/_build/html/",
f"--alias={site.name}",
"gcloud",
"storage",
"cp",
"--recursive",
str(site_output_dir / "_build/html"),
f"gs://{bucket_path}/{site.name}",
]
typer.secho(f"Running deploy:\n{' '.join(args)}", fg=typer.colors.GREEN)
subprocess.run(args).check_returncode()

https_root = f"https://storage.googleapis.com/{bucket_path}"
typer.secho(f"Deployed {'to production' if prod else 'as a draft'}: {https_root}/{site.name}/index.html", fg=typer.colors.GREEN)

if errors:
typer.secho(f"{len(errors)} errors encountered during papermill execution", fg=typer.colors.RED)
sys.exit(1)

if __name__ == "__main__":
app()
2 changes: 1 addition & 1 deletion portfolio/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<body style>
<h1>Cal-ITP Data Analysis Portfolio</h1>
{% for site in sites -%}
<p><a href="/{{ site.name }}/">{{ site.title }}</a></p>
<p><a href="{{ site.name }}/index.html">{{ site.title }}</a></p>
{% endfor %}

<p>Source code can be found <a href="https://github.com/cal-itp/data-analyses/">on GitHub.</a></p>
Expand Down
Loading