1
0

publish_github_artifact.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. import requests
  3. import os
  4. import subprocess
  5. GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
  6. PUBLISH_TOKEN = os.environ["PUBLISH_TOKEN"]
  7. ARTIFACT_ID = os.environ["ARTIFACT_ID"]
  8. GITHUB_REPOSITORY = os.environ["GITHUB_REPOSITORY"]
  9. VERSION = os.environ['GITHUB_SHA']
  10. #
  11. # CONFIGURATION PARAMETERS
  12. # Forks should change these to publish to their own infrastructure.
  13. #
  14. ROBUST_CDN_URL = "https://wizards.cdn.spacestation14.com/"
  15. FORK_ID = "wizards"
  16. def main():
  17. print("Fetching artifact URL from API...")
  18. artifact_url = get_artifact_url()
  19. print(f"Artifact URL is {artifact_url}, publishing to Robust.Cdn")
  20. data = {
  21. "version": VERSION,
  22. "engineVersion": get_engine_version(),
  23. "archive": artifact_url
  24. }
  25. headers = {
  26. "Authorization": f"Bearer {PUBLISH_TOKEN}",
  27. "Content-Type": "application/json"
  28. }
  29. resp = requests.post(f"{ROBUST_CDN_URL}fork/{FORK_ID}/publish", json=data, headers=headers)
  30. resp.raise_for_status()
  31. print("Publish succeeded!")
  32. def get_artifact_url() -> str:
  33. headers = {
  34. "Authorization": f"Bearer {GITHUB_TOKEN}",
  35. "X-GitHub-Api-Version": "2022-11-28"
  36. }
  37. resp = requests.get(f"https://api.github.com/repos/{GITHUB_REPOSITORY}/actions/artifacts/{ARTIFACT_ID}/zip", allow_redirects=False, headers=headers)
  38. resp.raise_for_status()
  39. return resp.headers["Location"]
  40. def get_engine_version() -> str:
  41. proc = subprocess.run(["git", "describe","--tags", "--abbrev=0"], stdout=subprocess.PIPE, cwd="RobustToolbox", check=True, encoding="UTF-8")
  42. tag = proc.stdout.strip()
  43. assert tag.startswith("v")
  44. return tag[1:] # Cut off v prefix.
  45. if __name__ == '__main__':
  46. main()