aboutsummaryrefslogtreecommitdiffstats
path: root/static-builds/netlify_build.py
diff options
context:
space:
mode:
authorGravatar Xithrius <[email protected]>2022-12-09 22:27:08 -0800
committerGravatar GitHub <[email protected]>2022-12-09 22:27:08 -0800
commitaeeec9d11307b75ed117dabf7c04698bd9186d77 (patch)
treea23267fa619b5f98187ec5e027d3701c6e34f231 /static-builds/netlify_build.py
parentAppeased the requests from reviews. (diff)
parentMerge pull request #811 from python-discord/dependabot/pip/flake8-bugbear-22.... (diff)
Merge branch 'main' into 695-setting-different-statuses-on-your-bot
Diffstat (limited to 'static-builds/netlify_build.py')
-rw-r--r--static-builds/netlify_build.py133
1 files changed, 41 insertions, 92 deletions
diff --git a/static-builds/netlify_build.py b/static-builds/netlify_build.py
index 4e1e6106..36520c28 100644
--- a/static-builds/netlify_build.py
+++ b/static-builds/netlify_build.py
@@ -4,106 +4,60 @@
# This script performs all the actions required to build and deploy our project on netlify
# It depends on the following packages, which are set in the netlify UI:
-# httpx == 0.19.0
+# httpx == 0.23.0
+import json
import os
import time
-import typing
import zipfile
from pathlib import Path
from urllib import parse
import httpx
-API_URL = "https://api.github.com"
-NIGHTLY_URL = "https://nightly.link"
-OWNER, REPO = parse.urlparse(os.getenv("REPOSITORY_URL")).path.lstrip("/").split("/")[0:2]
+def raise_response(response: httpx.Response) -> None:
+ """Raise an exception from a response if necessary."""
+ if response.status_code // 100 != 2:
+ try:
+ print(response.json())
+ except json.JSONDecodeError:
+ pass
-def get_build_artifact() -> typing.Tuple[int, str]:
- """
- Search for a build artifact, and return the result.
+ response.raise_for_status()
- The return is a tuple of the check suite ID, and the URL to the artifacts.
- """
- print("Fetching build URL.")
- if os.getenv("PULL_REQUEST").lower() == "true":
- print(f"Fetching data for PR #{os.getenv('REVIEW_ID')}")
-
- pull_url = f"{API_URL}/repos/{OWNER}/{REPO}/pulls/{os.getenv('REVIEW_ID')}"
- pull_request = httpx.get(pull_url)
- pull_request.raise_for_status()
-
- commit_sha = pull_request.json()["head"]["sha"]
-
- workflows_params = parse.urlencode({
- "event": "pull_request",
- "per_page": 100
- })
-
- else:
- commit_sha = os.getenv("COMMIT_REF")
-
- workflows_params = parse.urlencode({
- "event": "push",
- "per_page": 100
- })
-
- print(f"Fetching action data for commit {commit_sha}")
-
- workflows = httpx.get(f"{API_URL}/repos/{OWNER}/{REPO}/actions/runs?{workflows_params}")
- workflows.raise_for_status()
-
- for run in workflows.json()["workflow_runs"]:
- if run["name"] == "Build & Publish Static Preview" and commit_sha == run["head_sha"]:
- print(f"Found action for this commit: {run['id']}\n{run['html_url']}")
- break
- else:
- raise Exception("Could not find the workflow run for this event.")
-
- polls = 0
- while polls <= 20:
- if run["status"] != "completed":
- print("Action isn't ready, sleeping for 10 seconds.")
- polls += 1
- time.sleep(10)
-
- elif run["conclusion"] != "success":
- print("Aborting build due to a failure in a previous CI step.")
- exit(0)
-
- else:
- print(f"Found artifact URL:\n{run['artifacts_url']}")
- return run["check_suite_id"], run["artifacts_url"]
-
- _run = httpx.get(run["url"])
- _run.raise_for_status()
- run = _run.json()
-
- raise Exception("Polled for the artifact workflow, but it was not ready in time.")
-
-
-def download_artifact(suite_id: int, url: str) -> None:
- """Download a build artifact from `url`, and unzip the content."""
- print("Fetching artifact data.")
-
- artifacts = httpx.get(url)
- artifacts.raise_for_status()
- artifacts = artifacts.json()
-
- if artifacts["total_count"] == "0":
- raise Exception(f"No artifacts were found for this build, aborting.\n{url}")
-
- for artifact in artifacts["artifacts"]:
- if artifact["name"] == "static-build":
- print("Found artifact with build.")
- break
- else:
- raise Exception("Could not find an artifact with the expected name.")
-
- artifact_url = f"{NIGHTLY_URL}/{OWNER}/{REPO}/suites/{suite_id}/artifacts/{artifact['id']}"
- zipped_content = httpx.get(artifact_url)
+if __name__ == "__main__":
+ client = httpx.Client(
+ follow_redirects=True,
+ timeout=3 * 60,
+ )
+
+ owner, repo = parse.urlparse(os.getenv("REPOSITORY_URL")).path.lstrip("/").split("/")[0:2]
+
+ download_url = "/".join([
+ os.getenv("API_URL").rstrip("/"),
+ "api/github/artifact",
+ owner,
+ repo,
+ os.getenv("COMMIT_REF"),
+ parse.quote(os.getenv("ACTION_NAME")),
+ os.getenv("ARTIFACT_NAME"),
+ ])
+ print(f"Fetching download URL from {download_url}")
+ response = client.get(download_url)
+ raise_response(response)
+
+ # The workflow is still pending, retry in a bit
+ while response.status_code == 202:
+ print(f"{response.json()['error']}. Retrying in 10 seconds.")
+ time.sleep(10)
+ response = client.get(download_url)
+
+ raise_response(response)
+ url = response.json()["url"]
+ print(f"Downloading build from {url}")
+ zipped_content = client.get(url)
zipped_content.raise_for_status()
zip_file = Path("temp.zip")
@@ -115,8 +69,3 @@ def download_artifact(suite_id: int, url: str) -> None:
zip_file.unlink(missing_ok=True)
print("Wrote artifact content to target directory.")
-
-
-if __name__ == "__main__":
- print("Build started")
- download_artifact(*get_build_artifact())