Skip to content

Commit

Permalink
Allow deploying to draft or prod buckets separately
Browse files Browse the repository at this point in the history
  • Loading branch information
mjumbewu committed Dec 18, 2024
1 parent e759934 commit 04eae6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions portfolio/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
GOOGLE_ANALYTICS_TAG_ID = "G-JCX3Z8JZJC"
PORTFOLIO_DIR = Path("./portfolio/")
SITES_DIR = PORTFOLIO_DIR / Path("sites")
GCS_BUCKET = os.getenv("GCS_BUCKET", "calitp-data-analyses-portfolio")
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 Down Expand Up @@ -324,25 +325,27 @@ def index(
typer.echo(f"writing out to {fname}")
f.write(env.get_template(template).render(sites=sites, google_analytics_id=GOOGLE_ANALYTICS_TAG_ID))

bucket_path = PROD_GCS_BUCKET if prod else DRAFT_GCS_BUCKET

if alias:
bucket_path += f"/{alias}"

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

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

if prod:
args.append("--prod")

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 @@ -359,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 @@ -423,17 +427,22 @@ 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 = [
"gcloud",
"storage",
"cp",
"--recursive",
str(site_output_dir / "_build/html"),
f"gs://{GCS_BUCKET}/{site.name}",
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)
Expand Down
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

0 comments on commit 04eae6c

Please sign in to comment.