-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathnetlify_build.py
71 lines (53 loc) · 1.93 KB
/
netlify_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Build script to deploy project on netlify."""
# WARNING: This file must remain compatible with Python 3.8
# 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.23.0
import json
import os
import time
import zipfile
from pathlib import Path
from urllib import parse
import httpx
import contextlib
def raise_response(response: httpx.Response) -> None:
"""Raise an exception from a response if necessary."""
if response.status_code // 100 != 2:
with contextlib.suppress(json.JSONDecodeError):
print(response.json())
response.raise_for_status()
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")
zip_file.write_bytes(zipped_content.read())
with zipfile.ZipFile(zip_file, "r") as zip_ref:
zip_ref.extractall("build")
zip_file.unlink(missing_ok=True)
print("Wrote artifact content to target directory.")